how many bytes are read with uart_read_bytes()

Gardin
Posts: 31
Joined: Fri Dec 13, 2019 4:53 pm

how many bytes are read with uart_read_bytes()

Postby Gardin » Sat Dec 05, 2020 7:22 pm

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?

dastoned
Posts: 50
Joined: Fri May 29, 2020 2:52 pm

Re: how many bytes are read with uart_read_bytes()

Postby dastoned » Sun Dec 06, 2020 2:18 pm

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

Gardin
Posts: 31
Joined: Fri Dec 13, 2019 4:53 pm

Re: how many bytes are read with uart_read_bytes()

Postby Gardin » Sun Dec 13, 2020 12:10 am

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

Who is online

Users browsing this forum: Bing [Bot], chegewara and 76 guests