c# - 将许多变量从 Unity 发送到 Arduino

标签 c# string unity3d arduino serial-port

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

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

这类作品。但是,我认为 0 在 char 世界中没有值(value)。所以数据不对。但一定有更好的方法吗?

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' 字符),然后解码该字符串。

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);

 }

关于c# - 将许多变量从 Unity 发送到 Arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42952497/

相关文章:

c# - 在 C# for Unity 中非阻塞加载和复制大型 Texture2D

android - Google Play 商店中不支持的 API 警告

c# - 从 GridView 构建报告或发票

c# - HttpClient HttpRequestException

java - 根据传入方法的参数之一创建包含 switch 语句的方法

python - 在 Python 中将变量更改为输出文件的名称

c# - 如何将框的边界转换为屏幕坐标

c# - 获取 'DateTime.ToString()' 以输出与 'DateTime' 的 XML 序列化相同的字符串

c# - Expression.ToString() 有效吗?

c - 有没有更好的方法来编写这个函数?