首页 文章

发出串行接收字符串 . 团结 - > Arduino

提问于
浏览
0

我通过串口从Unity发送一个字符串到一个Ardino Mega,其baudRate为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();  
    }
}

2 回答

  • 0

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

  • 1

    感谢George Profenza以正确的方式轻推 .

    我改变了这个:

    if(Serial.available() > 0){
    

    对此:

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

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

相关问题