I'm using 2 Unos each with an XBee radio to communicate in one direction. However, I am not happy with the results I am getting. It is working to the extent that I am receiving data from the transmitter to the receiver, but the data is not always how I would expect it.
I have read lots of posts here and other places Google has led me, and I have borrowed very heavily from those posts attempting to get this to work.
Here's the code for my transmitter:
String outgoingString = "AA0123 BB0456 CC0789 Z\n";
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(outgoingString);
}
In the Serial Monitor, this results in the expected:
...
AA0123 BB0456 CC0789 Z
AA0123 BB0456 CC0789 Z
AA0123 BB0456 CC0789 Z
AA0123 BB0456 CC0789 Z
AA0123 BB0456 CC0789 Z
AA0123 BB0456 CC0789 Z
AA0123 BB0456 CC0789 Z
...
Here's the code for my receiver:
char incomingByte;
String incomingString = "";
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
while (incomingByte != '\n') {
incomingString += incomingByte;
incomingByte = Serial.read();
}
Serial.print(incomingString.length());
Serial.print("\t"); Serial.println(incomingString);
incomingString = "";
}
}
The output of this code isn't exactly what I would expect:
22 AA0123 BB0456 CC0789 Z
19 AA6 CC0CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
19 AA6 CC0CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
19 AA237 CÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿCÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ7ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿZÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
26 AA0123 BB0456 CC0 CC0789 Z
19 AA0123 BB04560789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
25 AA0123 23 BB0456 CC0789 Z
21 AA0123 BB0456 C0789 Z
18 AA0123 BB0456 C9 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
486 AA0123 BB0456ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿCÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿCÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ7ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ8ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ9ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿZÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
10 AA0C0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
10 AA012389 Z
18 AA0123456 CC0789 Z
18 AA0123 BB0CC0789 Z
18 AA0123 BB0456 89 Z
19 AA01230456 CC0789 Z
24 AA0123 BBBB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
286 AA0123 BB0456 CC078ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ9ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿZÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
32 AA0123 BB0456 23 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
29 AA0123 AA0123 BB0456 CC0789 Z
18 AA0123 BB0CC0789 Z
18 AA0123 BB0456 89 Z
22 AA0123 BB0456 CC0789 Z
23 AA0123 BB0456 CC0789 Z
22 AA0123 BB0456 CC0789 Z
...
As you can see, I'm getting random data from the Serial buffer. Eventually, the bad data continues to fill incomingString to a point where the Uno locks up. I'm assuming that buffers are overflowing at that point.
If I added coded that validated the data, I could just ignore the bad data and empty the value of incomingString to hopefully avoid buffer overflows. Eventually, I will have the incoming data constantly changing with values read from various input methods on the transmitter. I already have code to parse the values into useable variables, but right now, I'm trying to keep it as simple as possible to improve the quality of the read data.
If I'm just "doing it wrong, bro", then fine. Any suggestions on how to do it correctly or improve what is there would be very much appreciated.