Hello-
I am starting this topic to help users who are new to USB data transfers to avoid the same misunderstanding I have had for the last few months.
On the document page:
http://leaflabs.com/docs/lang/api/serialusb.html#lang-serialusb it says:
Warning: The SerialUSB functionality includes a 50 millisecond timeout for writes
For several months I believe I completely misinterpreted the word "timeout". I had assumed the word "timeout" indicated there would be some type of error generated if the Maple tried to communicate and the host did not respond within the 50 milliseconds.
A few months ago I learned that the Maple prefers to communicate in packets of 64 bytes (chars). This uses the USB buffer most efficiently.
However, it was not until yesterday that I finally understood that the Maple waits to accumulate 64 bytes (which may require several calls to the SerialUSB.print() functions), but if 50 milliseconds elapse before 64 bytes are reached then fewer than 64 bytes are sent (using a partially empty packet).
This explains why my SerialUSB.println(analogRead(0)) data transfers were so slow.
I had assumed each time I used the SerialUSB function data was sent down the USB line. However, now I understand that several calls to SerialUSB may be made before data is sent down the USB line.
For fast SerialUSB transfers, gbulmer recommends creating a 64 character (64 byte) array, using bitwise math to split data into character (one byte) size chunks, copying these chunks into the 64 character array, and then after the array is full to use SerialUSB.write(buffername, 64) to send all 64 bytes efficiently as a single USB packet.
http://forums.leaflabs.com/topic.php?id=639&page=2#post-3694
When this is done timeouts are minimized.
The 64 byte buffer can be filled with different data types (1 byte characters, 2-byte analogReads which uses only 12-bits, etc.). gbulmer's efficient approach assumes an application on the host system understands how to recombine the 64 characters of data into the original data types. If you are sending only 12-bit analogReads (which you will split each into two chars) you can send 32 datapoints per USB packet.
I do not have an accurate estimate yet of my SerialUSB.write() data transfer rates, but I believe I am reaching between 100K bytes/sec and 200K bytes/sec, which is at least 100X faster than I was getting before.
On the Teensyduino platform, there is a function called: Serial.send_now()
which does not exist on the Maple.
http://pjrc.com/teensy/td_serial.html
I do not need SerialUSB.send_now()
(because of gbulmer's help), but other users may find this function helpful.
Thanks again (especially to gbulmer and mbolivar) and good luck with fast USB transfers.
Stephen from NYC