lukestevens
Member
A strange time-sensitive bug in my application, running on Teensy++ 2.0 and Teensyduino 1.15, finally distilled down to this:
If I run this over the serial monitor, it works fine. When I run it for real reading the USB response on an Android device (I've tried several models), I get no response at all.
If I un-comment the first Serial.send_now(), I get the first 3-bytes, then nothing.
If I un-comment all three Serial.send_now(), everything works. Likewise if I instead un-comment all delay(5).
I noticed here that Teensy will flush the transmit buffer after 3 ms, which made me begin to suspect that without this delay, it attempts to collect the multiple writes but somehow botches it.
With other combinations of timing and buffer sizes, I've observed zero-byte packets, incorrect response sizes, and intermittently portions of the data getting zero-filled. I would expect all these are manifestations of one underlying problem.
For now I'm working around the problem with Serial.send_now() everywhere.
Code:
byte buf1[] = { 'a', 'b', 'c' };
byte buf2[] = { '>', '>', '-', '-', '0', '1', '0', '1' };
byte buf3[] = { 'x', 'y', 'z' };
Serial.write(buf1, 3);
// delay(5);
// Serial.send_now();
Serial.write(buf2, 8);
// delay(5);
// Serial.send_now();
Serial.write(buf3, 3);
// delay(5);
// Serial.send_now();
If I run this over the serial monitor, it works fine. When I run it for real reading the USB response on an Android device (I've tried several models), I get no response at all.
If I un-comment the first Serial.send_now(), I get the first 3-bytes, then nothing.
If I un-comment all three Serial.send_now(), everything works. Likewise if I instead un-comment all delay(5).
I noticed here that Teensy will flush the transmit buffer after 3 ms, which made me begin to suspect that without this delay, it attempts to collect the multiple writes but somehow botches it.
With other combinations of timing and buffer sizes, I've observed zero-byte packets, incorrect response sizes, and intermittently portions of the data getting zero-filled. I would expect all these are manifestations of one underlying problem.
For now I'm working around the problem with Serial.send_now() everywhere.