serialport-rs icon indicating copy to clipboard operation
serialport-rs copied to clipboard

Read not reading all the data off a TTY port

Open Austin-Crabtree opened this issue 2 years ago • 7 comments

Using:

  • Beaglebone Black Rev C
  • Linux Debian 10
  • Rust 1.68.2
  • Serialport 4.2.0

when reading off a Beaglebone Black UART connection I am running into an issue when I have a message larger than 52 bytes not being pulled off the TTY buffer in one read, typically takes two reads. I am creating a sufficiently large enough buffer and the read isn't filling the vector. I have tried both open() and open_native() on the port with no help. I also have narrowed it down to the read being the issue becuase I have put a clear(ClearBuffer::All) after reading and parsing the data and I can't get the second half of the data anymore.

Austin-Crabtree avatar May 04 '23 16:05 Austin-Crabtree

Does bytes_to_read indicate that there are 52 bytes available when you read?

Do you always get the same number of bytes from the first read request, or does it vary?

I'm wondering if the OS driver is doing some sort of buffering here...

mlsvrts avatar May 04 '23 16:05 mlsvrts

I haven't check the bytes_to_read function I can do that. But the amount of bytes to read is is variable based on the type of message that is being sent. All other messages are requests and don't have a data payload so each other message is the same size about 14 bytes in size.

Austin-Crabtree avatar May 04 '23 17:05 Austin-Crabtree

I will also say that I checked against my rust application by creating a quick python script using pyserial and was able to read all the data in one read.

Austin-Crabtree avatar May 04 '23 17:05 Austin-Crabtree

Raspberry PI Uart is 16 bytes. PySerial probably caches internally to another buffer transparently to read by other processes.

You're gonna need to read in a loop until there are no more bytes to read, stuffing them into a bigger buffer

DanielJoyce avatar May 05 '23 23:05 DanielJoyce

https://github.com/pyserial/pyserial/blob/31fa4807d73ed4eb9891a88a15817b439c4eea2d/serial/serialposix.py#L570

Here, it reads into a buffer in a loop, only returning when the buffer is full, or timeout.

You will need to imple the same logic as serialport-rs is pretty low level.

DanielJoyce avatar May 05 '23 23:05 DanielJoyce

This seems like a bug not a feature.. I'd expect a serial interface reader to read until the buffer is full or the transmission has timed-out if I can configure buffer size and timeout duration..

Is there a use-case where this is desired behaviour?

newcomertv avatar Jul 21 '23 15:07 newcomertv

Its a feature since folks may want to buffer, some don't. Since you want to buffer, you need to buffer yourself.

DanielJoyce avatar Apr 10 '24 21:04 DanielJoyce