USB Serial write timeout after sending 13 bytes to Teensy 3.2

Status
Not open for further replies.

Adam Francey

New member
I want to send a command to the Teensy that is 25 bytes long, and I attempt to read it with the following simple code:

Code:
void setup() {
  Serial.begin(57600);
}

void loop() {
  if (Serial.available()>24){
    Serial.read();
  }
}

I send bytes one at a time to the Teensy with TyQt serial monitor. Upon sending the 13th byte, TyQt appears to try and upload new firmware but gets stuck at 0%. Shortly after I get the error message " Timed out while writing to '\\.\COM95' " at the bottom of TyQt's screen. Every byte after this has the same effect until I reset the Teensy. If I am using pyserial (python), then attempting to write the 13th byte locks up the write() function.

So in short, once there are 12 bytes sitting in the Teensy receiving buffer without being read, the 13th byte totally breaks the serial port until the Teensy is reset.

I have tested with multiple Teensies as well as Windows and Raspbian (Raspberry Pi).

What is happening? Is there a way to get around this without constantly reading the serial port?
 
What is happening?

The USB buffers are all getting allocated, starving the USB stack of memory to receive more data. USB has automatic end-to-end flow control, so your PC knows it can't send more data, which is the reason you see this error.

By default, Teensy allocates 12 buffers for USB Serial. This is configured in usb_desc.h.

When you transmit 1 byte at a time, your PC puts that 1 byte into a USB packet which has a maximum size of 64 bytes. It's quite inefficient.

Is there a way to get around this without constantly reading the serial port?

Sure. You could send all the bytes at the same time on the PC side. Generally your PC will put them all into the same packet.

Or you could increase the number of buffers by editing usb_desc.h. But the max is 31, so don't go crazy with a huge number.

But the best approach is to read the data as soon as it's available, so you don't tie up the USB buffers. Running out of buffers will also prevent you from being able to transmit anything with Serial.print().
 
Thanks for the informative response Paul! In my particular instance this was solved by sending all the bytes at the same time on the PC side.
 
Status
Not open for further replies.
Back
Top