I'm trying to send many ints to my "masterArduino". Because the SerialPort object only send strings. I've tried many things including:
- Creating a string from ints (didn't work because the size of the string.length is dynamic).
- Then I tried to convert these ints to chars, this because all values are between 0-255, then put the char into a string and send it.
This sort of works. However, I think there is no value for 0 in char world. So the data is not right. But there must be a better way?
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 = "";
}
Any help would be greatly appreciated! Thanks!
Arduino Code:
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();
}
0in the "char world", namely,'\0'. It's an unprintable empty character, if you try to print it you'll simply get an empty space. Do you have a problem with sending zeroes?