Arduino Due - Serial speed?

I just did a quick test on the native port, measuring approx 849 kbytes/sec.

Yes, that's kbytes/sec, not kbits/sec. On the native port, the baud rate setting is ignored. Data is always transferred as fast as both sides can go.

I ran this code on Due, using Arduino 1.5.1.

void setup() {
  SerialUSB.begin(115200); // baud rate ignored for usb virtual serial
  pinMode(2, OUTPUT); // measure pin 2 with frequency counter
}
void loop() {
  digitalWrite(2, HIGH);
  SerialUSB.println("Output is HIGH");
  digitalWrite(2, LOW);
  SerialUSB.println("Output is Low");
}

I ran this Python script (from Claudio Indellicati) on my Ubuntu 12.04 64 bit Linux machine:

# This script flushes every byte received on the serial port

import serial
import sys

ser = serial.Serial(
   port = '/dev/ttyACM0',
   baudrate = 115200,
   parity = serial.PARITY_NONE,
   stopbits = serial.STOPBITS_ONE,
   bytesize = serial.EIGHTBITS
)

if ser.isOpen() == False :
   ser.open()
   if ser.isOpen() == False :
      print "Can't open serial port"
      sys.exit(1)

while 1 :
   if ser.inWaiting() > 0 :
      ser.read(16384);

I connected a Fluke multimeter to pin 2 and measured approx 27.4 kHz (it fluctuates slightly). Each each cycle is 31 bytes, so the data rate is about 849 kbytes/sec.

I repeated this test on Leonardo, Teensy 2.0 and Teensy 3.0 (changing "SerialUSB" to "Serial"), using Arduino 1.0.2 and the same Ubuntu 12.04 system running the python script. Here are the results:

Leonardo: 1.842 kHz - 57 kbytes/sec
Teensy 2.0: 14.16 kHz - 439 kbytes/sec
Teensy 3.0: 35.09 kHz - 1088 kbytes/sec

It's worth mentioning the speed changes dramatically depending on the software receiving the data. The Arduino Serial Monitor gives much lower speeds. Even the seyon terminal emulator gave speeds slower than the python script.

At least with Linux, after a program closes the serial port and the kernel drivers' buffers are filled, data transmission stops. In that case, Serial.print() returns very quickly and a much higher frequency appears on pin 2. If anyone tries to repeat this test, please be sure to measure the frequency while the python script or some other program is actually receiving the data.

It's probably also worth mentioning the USB spec says the maximum theoretical speed on 12 Mbit/sec USB is 1216 kbytes/sec, which accounts for protocol overhead, except the bit stuffing algorithm (which is data dependent).