8 years, 8 months ago.

Do the various Serial read/write routines block or will characters be thrown away?

Sending: if I send data too fast will characters be thrown away?

Receiving: if the sender to me is too fast, will some of his characters be thrown away?

Will printf data be thrown away?

I don't know if I understood your question complitely. But, I believe those functions wait for the receiver to get all data before sending more...

posted by Thiago . 03 Mar 2016

2 Answers

8 years, 8 months ago.

Send - No, your program will block until enough of the message has been sent that remaining message will fit in the hardware transmit buffer. This means that a printf that transmits of a long message at a low baud rate can take a significant amount of time.

Receive - Yes. Once the hardware receive buffer is full any new data will be lost. For messages longer than the hardware buffer you must read the data and either process it or store it in a software buffer as it arrives.

On most boards the hardware buffer is 16 bytes, on some it could be smaller.

To get around these restrictions you can create buffers in memory to store the transmit and receive data and then transfer it to and from the hardware based on the serial port transmit and receive interrupts, that is how the BufferedSerial and MODSERIAL libraries work.

Accepted Answer

So, if I quickly sent 1000 lines using printf from a simple main.cpp using the serial class, none of those lines would be lost, correct, at least not lost on the sending end?

When you say "block" how does that play out in the rtos versus non-rtos environments. I can see the "readable" and "writeable" calls here and there. Looks like the serial class doesn't use rtos.

posted by Alfred Hume 03 Mar 2016
8 years, 8 months ago.

You can use BufferedSerial lib, this extends the hardware buffers of the different platforms to also have a much larger software buffer. Of course it uses interrupts for this, and if your interrupts are blocking those interrupts for long enough it still will throw received characters away.

Sent characters are not thrown away (also not with regular one), it will block until there is space in the transmit buffer. But if you use normal Serial, and you don't sufficiently often read the receive buffer (using getc for example), they will be thrown away when the buffer is full. You have an LPC1768, and this one has one of the largest hardware buffers of 16 characters, so it is not as critical for you to read it with getc for example really often.

Have you seen CBuffer.h, included in Wifly.h. Is something like that used in the BufferedSerial lib.

I just want to use a good, solid serial library and there are various flavors out there. What's your recommendation.

posted by Alfred Hume 03 Mar 2016

To also answer your question above: Indeed, 1000 printf in a row will always work properly. There can be gaps in sending when the device would be busy with other things, but there will never be missing characters.

If you just want a solid serial lib, then go for regular Serial (or RawSerial if you use RTOS) if you don't have requirements regarding buffering, BufferedSerial if you do, or MODSERIAL if you want the extra functions MODSERIAL offers.

posted by Erik - 03 Mar 2016

My uncertainty with Serial's blocking or not blocking was because I didn't know its behavior as standalone versus under RTOS. Erik, you cleared that up by mentioning Serial versus RawSerial.

posted by Alfred Hume 04 Mar 2016