I discovered a problem in the Arduino Due libraries (I am still at 1.5.4 due to the analogRead problems in 1.5.5). Using an Arduino Due board with following setup:
- Tx1 connected with Rx1
- native USB connected to a computer running a terminal (I will name it "USB Terminal" below)
- programming USB port connected to a computer running a terminal (I will name it "Monitoring Terminal" below)
- Demo Program using to the code below loaded.
Do the following. Open both Terminals with 115200baud. Reset the Arduino board. You will see some text on the Monitoring terminal, cyclically printing the current status. Without data to the native USB port, you will see no fails. Then with the USB Terminal send a string (I used ca. 150 characters) repeatedly to the Arduino Due native USB port. The Demo program shows also the amount of received USB data. Eventually you will see Fails counting up. In my setup, I had 5 Fails after sending 16kByte of data to the native USB port.
The nature of the error is that one of the transmitted bytes is not received on Serial1. The Demo program prints the received bytes to the terminal if an error occurs.
Using an oscilloscope and further debug code hints that the reception is the problem, not the transmission. In some way, the data reception on the native USB port influences the correct reception of bytes on Serial1.
#include <Arduino.h>
long serialcounter = 0;
long failcounter = 0;
long usbcounter = 0;
int cyclecounter = 0;
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
Serial.println("SerialErrorDemo");
}
void loop() {
unsigned char testbytes[] = {
1,2,3,4,5,6,7,8
};
char serial_buffer[1000];
char usb_buffer[16000];
int n;
Serial1.write(testbytes, sizeof(testbytes));
serialcounter += sizeof(testbytes);
cyclecounter++;
if (cyclecounter == 1000) {
cyclecounter = 0;
Serial.print("Tx Bytes: ");
Serial.print(serialcounter, DEC);
Serial.print(" Fails: ");
Serial.print(failcounter, DEC);
Serial.print(" USB received: ");
Serial.println(usbcounter, DEC);
}
delayMicroseconds(6000);
n = Serial1.available();
if (n > sizeof(serial_buffer)) n = sizeof(serial_buffer);
Serial1.readBytes(serial_buffer, n);
if (n != sizeof(testbytes)) {
for (int i = 0; i < n; i++) {
Serial.print(serial_buffer[i], DEC);
Serial.print(",");
}
Serial.println();
failcounter++;
}
n = SerialUSB.available();
if (n > sizeof(usb_buffer)) n = sizeof(usb_buffer);
SerialUSB.readBytes(usb_buffer, n);
usbcounter += n;
}