Pages: [1]   Go Down
Author Topic: Arduino Mega ADK Serial.print stale data issue  (Read 2516 times)
0 Members and 1 Guest are viewing this topic.
Taipei, Taiwan
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Physicist, hacker, commander of a Zerg hive
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

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:

Code:
#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.
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code?

Does the Mega ADK  print out the header  "#HotJunction(C),ColdJunction(C)"  in your setup function?  Yet it is not re-printing it on the reset?
Logged

Taipei, Taiwan
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Physicist, hacker, commander of a Zerg hive
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The header is in the receiver code (so I can pipe it into a file just like that), and should signify the point where my PC is ready to receive the data.

The code on the Mega ADK is this one: https://github.com/imrehg/sketchbook/blob/master/Thermocouple5/Thermocouple5.ino

The receiver code is something like this in Python (with just the relevant parts shown):
Code:
dev = serial.Serial('/dev/ttyACM0', baud, timeout=1)
print "#HotJunction(C),ColdJunction(C)"
while True:
    print dev.readline()

I got around the problem a bit by throwing away the first 50ms data or so:
Code:
dev = serial.Serial('/dev/ttyACM0', baud, timeout=1)
delay = 0.05    # seconds
finish = time.time() + delay
while time.time() < finish:
    dev.readline()   # just reads but not prints
print "#HotJunction(C),ColdJunction(C)"
while True:
    print dev.readline()
In this case the buffer is received but not printed first, then later started to print the real (new) data.
Logged

Pages: [1]   Go Up
Jump to: