[En-Nut-Discussion] UsartRead and timeout

Ole Reinhardt ole.reinhardt at embedded-it.de
Tue Apr 25 09:04:36 CEST 2023

Hi Uwe,

yes I would expect this behavior to be correct.

The UsartRead() routine will normally be called by read() from the 

read() is defined to return the currently available number of bytes, 
which may be lower than the requested amount.

See e.g. https://man7.org/linux/man-pages/man2/read.2.html

When writing a parser, that reads data from a device, I would normally 
use a loop, that calls read() until the expected amount of data is 
received. If you do not know the number of bytes that shall be received, 
just implement an timeout, until which the first character shall be 
received and then an interbyte timeout, until which the next character) 
shall be received.

I would use the select() routine to implement these timeouts.

Best regards,

Ole Reinhardt

Am 24.04.23 um 19:28 schrieb Uwe Bonnes:
> Hello,
> ../../dev/usart.c in UsartRead reads:
>      228     /*
>      229      * Wait until at least one character is buffered or until a read
>      230      * timeout occured.
>      231      */
> Is this really the intended behaviour? I would expect to read like
> * Wait until the requested number of character is buffered or until a read
> * timeout occured.
> This would be in line with e.g.
> https://learn.microsoft.com/en-us/windows-hardware/drivers/serports/setting-read-and-write-timeouts-for-a-serial-device"
> "Serial.sys continues to transfer bytes until the requested number of
> bytes are transferred or a time-out event occurs."
> Returning after the first received byte makes it hard to wait for the
> full multi-byte answer of an external device.
> What is your opinion?
> Thanks

More information about the En-Nut-Discussion mailing list