Page 1 of 1

how many bytes are read with uart_read_bytes()

Posted: Sat Dec 05, 2020 7:22 pm
by Gardin
Hello everyone,

I'm working on a project that uses uart to receives lots of data(~45KB), the data is initialized with SOT and ended with EOT.

So in order to read the data I wait for the starting SOT and then I increment a buffer pointer accordingly to the data comming through.(I don't receive all the data at once, the sender uses 512 Bytes chunks).

To increment my pointer buffer I check how many bytes are available using:

Code: Select all

uart_get_buffered_data_len(uart_num, &length);
and if I still have space in my buffer I call

Code: Select all

length = uart_read_bytes(uart_num, buffer+length2, length, 0);
What is weird is that once in a while the buffer overflow even though I'm constantly checking if there is still space in it, so this makes me think that maybe uart_read_bytes() is returning more bytes then I thought it was going to. Does that makes sense? uart_read_bytes() shouldn't only read the number of bytes I pass as parameter?

Re: how many bytes are read with uart_read_bytes()

Posted: Sun Dec 06, 2020 2:18 pm
by dastoned
Where do you see the overflow, in your own data buffer or UART FIFO buffer?

If it's your own buffer overflowing, your UART handling is probably buggy. Can't tell without seeing your code, but I notice that you re-use the variable length to hold both number of bytes available and number of bytes read out. You also don't check uart_get_buffered_data_len() return code for failure (can't really imagine why it would fail, but shit happens...) If uart_get_buffered_data_len() does fail, variable length will not be changed and it could be left holding a stale value from previous read.

Regarding UART FIFO buffers, have a look at this thread:

viewtopic.php?f=2&t=18069

Re: how many bytes are read with uart_read_bytes()

Posted: Sun Dec 13, 2020 12:10 am
by Gardin
Yes, it was my own UART buffer that was overflowing, and indeed it was my fault.

Anyway thank's a lot for your help!

Gabriel Gardin