Hi all,
I've had this problem for a few months now and haven't been able to figure it out. Every once and a while my Arduino Due sends data out in the wrong order over SerialUSB (native USB -> SAM3X). I have seen this when compiling with Arduino 1.5.2 and Arduino 1.5.6-r2.
To try to catch the problem I have reduced the code to this sample sketch:
/*
ArduinoDue SerialUSB bug test
*/
long counter = 0;
void setup() {
while(!SerialUSB); // wait for the serialUSB to come up
delay(100);
SerialUSB.println("Arduino DUE SerialUSB (SAM3X native USB)");
SerialUSB.println("This demonstrates how SerialUSB sends messages in the wrong order sometimes");
SerialUSB.println("Just start the Arduino serial monitor while this sketch runs on an Arduino DUE");
SerialUSB.println("If the serial messages arrive correctly, just close and open the serial monitor again");
SerialUSB.println("After a few iterations you'll see the messages get mixed up");
SerialUSB.println(" --- ");
SerialUSB.println("Tested with Arduino 1.5.6-r2");
}
void loop() {
if(SerialUSB){
counter ++;
SerialUSB.print(counter);
SerialUSB.print(" - I have sent ");
SerialUSB.print(counter);
SerialUSB.println(" messages so far.");
delay(200);
}
}
After uploading this to the Arduino Due I just open the Serial Monitor. The expected feedback is something like this:
61 - I have sent 61 messages so far.
62 - I have sent 62 messages so far.
63 - I have sent 63 messages so far.
64 - I have sent 64 messages so far.
65 - I have sent 65 messages so far.
66 - I have sent 66 messages so far.
67 - I have sent 67 messages so far.
But when i restart the serial monitor I sometimes get something like this:
m - I have sent 16 messages so far.
17 - I have sent 17 messages so far.
18 - I have sent 18 messages so far.
m - I have sent 19 messages so far.
20 - I have sent 20 messages so far.
21 - I have sent 21 messages so far.
m - I have sent 22 messages so far.
or
16 - I have sent 16 messages so far.
17 - I have sent m messages so far.
18 - I have sent 18 messages so far.
19 - I have sent 19 messages so far.
20 - I have sent m messages so far.
21 - I have sent 21 messages so far.
22 - I have sent 22 messages so far.
The problem sometimes happens right from the start, without re-starting the serial connection.
I have created an OpenFrameworks app that feeds me back the serial messages and see the same problem appear, so it's not related to the Arduino IDE
I have tried different BAUD rates, and have seen it every time.
I have tested this with different Arduino Due (all R3) boards, they all do it.
Does this ring a bell for anyone? I'm out of ideas..