【问题标题】:Send many variables from Unity To Arduino从 Unity 向 Arduino 发送许多变量
【发布时间】:2024-01-12 15:34:02
【问题描述】:

我正在尝试向我的“masterArduino”发送许多整数。 因为 SerialPort 对象只发送字符串。我尝试了很多方法,包括:

  • 从整数创建字符串(不起作用,因为 string.length 的大小是动态的)。
  • 然后我尝试将这些整数转换为字符,这是因为所有值都在 0-255 之间,然后将字符放入字符串并发送。

这类作品。但是,我认为 char 世界中的 0 没有任何价值。所以数据不对。但一定有更好的办法吗?

void sendInfo() {
    for (var i = 0; i < peltiers.Length; i++) {
        char tempHot = (char) peltiers[i].GetComponent<Peltier>().hot;
        char charTemp = (char) peltiers[i].GetComponent<Peltier>().temp;
        peltierInfo += tempHot.ToString();           
        peltierInfo += charTemp.ToString();                  
    } 
    sp.WriteLine(peltierInfo);
    Debug.Log(peltierInfo);
    sp.BaseStream.Flush();
    peltierInfo = "";         
}

任何帮助将不胜感激!谢谢!

Arduino 代码:

void loop() {
    int serialIndex = 0;  
    int i2cIndex = 0; 
    while (0 < Serial.available()) { // loop through all the received bytes 
    char bufferByte = 0;     
    bufferByte = Serial.read();
    serialBuffer[serialIndex] = (byte) bufferByte;  // put current index byte in array      
    serialIndex ++;                    // add index. 
    if (serialIndex%12==0 && serialIndex != 0) {
          sendBytes(0);      
       }
    } 
     //sendBytes(0);  
     delay(50);
}

void sendBytes(int slave) {
    byte i2cBuffer[12];
    int bufferIndex = slave * 12;
    for (int i = 0; i < 12; i++) {
        i2cBuffer[i] = serialBuffer[i + bufferIndex];
    }
    Wire.beginTransmission(slave+1);
    Wire.write(i2cBuffer, 12);
    Wire.endTransmission();  
}

【问题讨论】:

  • “字符世界”中有0,即'\0'。这是一个无法打印的空字符,如果您尝试打印它,您只会得到一个空白空间。你有发送零的问题吗?

标签: c# string unity3d arduino serial-port


【解决方案1】:

为了能够发送任何整数,首先将它们编码成一个字符串,用一些东西(例如'\0' char)分隔它们,然后解码该字符串。

void sendInfo() {
    ...
    peltierInfo += peltiers[i].GetComponent<Peltier>().hot.ToString();
    peltierInfo += '\0';
    peltierInfo += peltiers[i].GetComponent<Peltier>().temp.ToString();
    peltierInfo += '\0';
    ...
}

void loop() {
    int serialIndex = 0;  
    int i2cIndex = 0;
    // set to how many digits there can be in an incoming number plus 1
    int maxNumberLen = 20; 
    char buffer[20];
    // position at which we now put a char that makes up our number
    char* currentCharPtr = buffer;
    while (0 < Serial.available()) { // loop through all the received bytes 
       char bufferByte = 0;     
       bufferByte = Serial.read();
       *currentCharPtr = bufferByte;
       // move pointer forward
       currentCharPtr ++;
       // end of a number in string
       if (bufferByte == '\0') {
           printf("Got number %s\n", buffer);
           // atoi parses string to int
           serialBuffer[serialIndex] = atoi(buffer); 
           serialIndex ++;                    
           if(serialIndex%12==0 && serialIndex != 0){
              sendBytes(0);
           }
           // fill buffer with zeros after we found a number
           memset(buffer, 0, 20);
           currentCharPtr = buffer;
       }
    } 
     //sendBytes(0);  
     delay(50);

 }

【讨论】:

    【解决方案2】:

    感谢您的回复,您的回答正是我的工作方式。我只是做的略有不同,没有时间上传这篇文章。哪种方式更好,还是只是口味问题?

    团结

     void sendInfo()
    {
        for (int i = 0; i < peltiers.Length; i++)
        {
            peltierInfo += ",";
            peltierInfo += peltiers[i].GetComponent<Peltier>().hot.ToString();
            peltierInfo += ",";
            peltierInfo += peltiers[i].GetComponent<Peltier>().temp.ToString("D3");
        }
        //Debug.Log(peltierInfo);
        sp.WriteLine(peltierInfo);
        sp.BaseStream.Flush();
        peltierInfo = "";
    }
    

    阿杜诺

    void loop() {
       int serialIndex = 0;
       if(Serial.available() > 0){     
         while (0 < Serial.available()) {            // loop through all the received bytes 
            String bufferString;
            uint8_t bufferInt;
            bufferString = Serial.readStringUntil(','); 
            bufferInt = bufferString.toInt();      
            serialBuffer[serialIndex] = bufferInt;  // put current index byte in array      
            serialIndex ++;                          // add index. 
         }     
         sendBytes(0); 
       }
       delay(50);
    }
    

    感谢您的帮助!

    【讨论】:

    • 哦,抱歉,您的版本当然更好——我假设您使用纯 C 语言编写 Arduino 代码。如果您使用的是 C++,那么一定要使用 String 而不是 char 数组。我选择'\0' 作为分隔符,只是因为在纯C 中每个字符串都必须以'\0' 结尾,所以它使我的代码更简单。