8 years, 8 months ago.

serial TX blocks thread. How avoid it ?

Hi, forks While LED blinking by thread, the load of serial TX influences LED timing. I think Serial TX action inhibits the system timer interrupt for a long time. Or, priority of interrupt make it overrides. The influence is more large for Slow speed TX baudrate. Even, Rawserial makes same phenomena. And, TX buffer flushing is not enough for high baud rate. It left some characters. Is there some problem in Serial TX handling ? Is it thread-safe ? I doubt it is not safe.... I wanna exchange the view. Thank you.

Question relating to:

1 Answer

8 years, 8 months ago.

Yes, serial transmits will block until there is sufficient space in the Tx buffer for the remaining message. The Tx buffer will normally be 1 or 16 bytes depending on the hardware.

If you want to avoid this then you can use something like MODSERIAL ( https://developer.mbed.org/users/Sissors/code/MODSERIAL/ )

It implements a software buffer on both the serial receive and transmit and uses the serial interrupts to move data to/from the hardware buffers. Set the buffer sizes to be the largest message you expect to see plus a few bytes and it should fix the problem. It should be a drop in replacement for the mbed Serial class.

Accepted Answer

Thank you a lot. Thread missing is gone with MODSERIAL even slow baudrate (1200). I hope standard official mbed will includes int driven serial lib.

posted by hiro . 16 Jul 2015

Adding a buffer takes up CPU cycles and memory and isn't always needed, it should remain outside the standard libraries. The libraries are a big enough performance hit as it is.

posted by Andy A 16 Jul 2015