I was using a sketch that sends some sensor data over Serial to the computer, and had a python script to read it. It was running on a Diecimila-equivalent board. Now I have an Arduino Mega ADK, and run the sketch on that one. The problem I have, that first when read the serial on the computer, all I receive is a lot of stale buffer, then the board resets, then starts to read correctly.
To illustrate it, one example output reading the data received in on the computer side:
#HotJunction(C),ColdJunction(C) 31.25,0.69,9046 31.25,30.69,9548 31.25,30.69,10050 31.25,30.69,10553 31.25,30.69,11056 31.25,30.69,11558 31.00,30.69,12061 25,30.69,9046 31.25,30.69,9548 31.25,30.69,10050 31.25,30.69,10553 31.25,30.69,11056 31.25,30.69,11558 31.00,30.69,12061 0.00,0.00,0 30.25,30.25,501 30.25,30.31,1004 30.50,30.37,1506 30.50,30.44,2009
The first line is the header, printed after I connected to the board, but haven't read anything yet. Every line then is 3 comma-separated values: sensor reading, another sensor reading, finally a timestep in milliseconds since bootup. E.g the data line
31.25,30.69,9548 would be Sensor1=31.25, Sensor2=30.69, timestamp=9.548s since bootup.
Looking at the data, what I actually received:
- 7 lines of stale data (lines 1-7)
- A broken repeat of that stale data (lines 8-14)
- Correct data after reboot (lines 15 up)
The amount of stale data is different run by run.
Where does this behaviour come from? I feel it's some buffering issue. Is there a way to avoid it? I guess it's a Mega ADK issue, because with the old board I haven't seen this. Might have to do something with the fact that there are so many serial lines on that board, and I guess the firmware control is different?
What I do now, is that I throw away whatver I read in the first 10ms of starting the computer-side script. It fixes things in a hackish way, but I'd love to understand what is actually going on there, and have a better way to do that.