【问题标题】:Receiving string in Serial, issue. Unity -> Arduino在序列中接收字符串,问题。统一 -> Arduino
【发布时间】:2017-03-24 10:46:21
【问题描述】:

我通过串行从 Unity 向 Arduino Mega 发送一个字符串,波特率为 115200。该字符串被解析为一个 uInt_8 数组,并通过 i2c 以 12 字节的包发送到其他 arduino。这很好用,但仅适用于前 10 个字节 (0-9),因此它必须与两位小数 (10, 11) 有关。 24 字节的字符串看起来像这样,0,255,0,055,0,025,0,255,0 等。值始终介于 0/1 和 0/255 之间。

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(); 
   }
   delay(50);
}

void sendBytes(){
    for(int i = 0; i<boards; i++){         
//        int i2cIndex = i*12;
//        for(int j = 0; j <12; j++){
//          i2cBuffer[j] = serialBuffer[j+i2cIndex];
//        }
        Wire.beginTransmission(i+1);
        Wire.write(serialBuffer, 12);
        Wire.endTransmission();  
    }
}

【问题讨论】:

  • 你能检查一下bufferString的长度,看看它是不是你期望的长度?如果没问题,请仔细检查 toInt() 转换的结果。发送代码 sn-p 是什么样子的? (例如,您是一次写入一个字节,还是一次写入 12 个字符的串行端口?

标签: string arduino serial-port i2c


【解决方案1】:

我构建了这个:https://github.com/relativty/wrmhl 使用它,您可以创建无延迟的 Unity3D 和 Arduino 体验。

【讨论】:

  • 这看起来是我的评论
【解决方案2】:

感谢 George Profenza 以正确的方式推动。

我已经改变了这个:

if(Serial.available() > 0){     

到这里:

if(Serial.available() == 30){     

我还降低了 Unity 中的发送频率,但我现在正在 arduino en unity 之间实现握手,以提高速度和效率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多