Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 串行接收字符串,发出。统一->;阿杜伊诺_String_Arduino_Serial Port_I2c - Fatal编程技术网

String 串行接收字符串,发出。统一->;阿杜伊诺

String 串行接收字符串,发出。统一->;阿杜伊诺,string,arduino,serial-port,i2c,String,Arduino,Serial Port,I2c,我通过串口将一个字符串从Unity发送到一个波特率为115200的Arduino Mega。该字符串被解析为uInt_8数组,并通过i2c以12字节的包发送给其他arduino。这非常有效,但只适用于前10个字节(0-9),因此它必须与两个小数(10,11)有关。24字节的字符串如下所示:0255,0055,0025,0255,0等。值始终在0/1和0/255之间 void loop() { int serialIndex = 0; if(Serial.available() >

我通过串口将一个字符串从Unity发送到一个波特率为115200的Arduino Mega。该字符串被解析为uInt_8数组,并通过i2c以12字节的包发送给其他arduino。这非常有效,但只适用于前10个字节(0-9),因此它必须与两个小数(10,11)有关。24字节的字符串如下所示:0255,0055,0025,0255,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();  
    }
}
void循环(){
int serialIndex=0;
如果(Serial.available()>0){
而(0因为(inti=0;我感谢乔治·普洛芬扎以正确的方式推动

我改变了这一点:

if(Serial.available() > 0){     
为此:

if(Serial.available() == 30){     
我还降低了Unity中的发送频率,但我现在正在实施arduino和Unity之间的握手,以提高速度和效率。

我构建了这个:
使用此选项,您可以创建无延迟Unity3D和Arduino体验。

您可以检查
bufferString
的长度检查,看看它是否是您期望的长度吗?如果可以,请仔细检查toInt()转换的结果。发送的代码段看起来如何?(例如,您是一次写入一个字节,还是用12个字符向串行端口写入一次?在我看来,这是一个注释