I'm sending a string via serial from Unity to an Arduino Mega with a baudRate of 115200. That string is parsed into an uInt_8 array and send to other arduino via i2c in packages of 12 bytes. This works great but only for the first 10 bytes (0-9) so it must have something to do with two decimals (10, 11). The string for 24 bytes looks like this ,0,255,0,055,0,025,0,255,0,etc. The values are always between 0/1, and 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();
}
}
bufferStringto see if it it's the length you expect ? If that's ok, double check the result of the toInt() conversion. How does the sending code snippet look like ? (e.g. are you writing one byte at a time, or writing to the Serial port once with 12 chars ? - George Profenza