uart_read_bytes

shelladdicted
Posts: 3
Joined: Fri Dec 29, 2017 11:32 pm

uart_read_bytes

Postby shelladdicted » Fri Dec 29, 2017 11:42 pm

Hi,

Code: Select all

int uart_read_bytes(uart_port_t uart_num, uint8_t *buf, uint32_t length, TickType_t ticks_to_wait)
Docs is not clear enough, this function return only after that ticks_to_wait (aka 20 / portTICK_RATE_MS)[/code] are passed?
or can return as soon length bytes are available (before that ticks_to_wait expire)?

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: uart_read_bytes

Postby tele_player » Sat Dec 30, 2017 7:22 pm

I haven’t tried it, but functions like this usually return immediately if the requested data is available. The alternative doesn’t make sense.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: uart_read_bytes

Postby kolban » Sun Dec 31, 2017 1:26 am

In your original post you asked if we waited or returned immediately when the "length" bytes were available. I think you shouldn't discount a third option which I believe may also exist ... that it returns immediately if LESS than "length" bytes are available but at least 1 byte. For example, if the UART has 5 bytes waiting to be read and you pass in a buffer of 100 bytes into which the data will be stored, my belief is that it will return immediately having populated 5 bytes and telling you that it read those 5 bytes into the buffer. This is not the same as "waiting for 100 bytes to be read".
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

shelladdicted
Posts: 3
Joined: Fri Dec 29, 2017 11:32 pm

Re: uart_read_bytes

Postby shelladdicted » Sun Dec 31, 2017 7:26 pm

kolban wrote:In your original post you asked if we waited or returned immediately when the "length" bytes were available. I think you shouldn't discount a third option which I believe may also exist ... that it returns immediately if LESS than "length" bytes are available but at least 1 byte. For example, if the UART has 5 bytes waiting to be read and you pass in a buffer of 100 bytes into which the data will be stored, my belief is that it will return immediately having populated 5 bytes and telling you that it read those 5 bytes into the buffer. This is not the same as "waiting for 100 bytes to be read".
my belief is that it will return immediately having populated 5 bytes and telling you that it read those 5 bytes into the buffer
this^ is exactly what I need,
maybe i haven't been clear enough in my question,
with length I mean the bytes that i expect to read (always < than *buf length) not the length of *buf

my code is

Code: Select all

uint8_t cmd[4];
//obviously, here i populate *cmd
//then Send
uart_write_bytes(_uartPort, (const char*) cmd, 4); // Send Command (composed by 4 bytes), and i expect 4bytes to read (ACK)
// some other code here
// check for ACK
int rxBytes = uart_read_bytes(_uartPort, data, 4, 20 / portTICK_RATE_MS); // read 4 bytes OR timeout
if (rxBytes > 0) {
// DO SOMETHING
}
else{
	// Err :-(
}

with this code uart_read_bytes() will return as soon 4 bytes are ready or will ALWAYS wait for 20ms?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: uart_read_bytes

Postby kolban » Tue Jun 05, 2018 4:57 am

You will likely be able to return immediately (without blocking) is you specify a timeout of 0. You will be returned what ever set of UART bytes are immediately available but no more than the length you requested. You may get 0 bytes if no UART data is immediately available.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Giuseppe1412
Posts: 1
Joined: Tue Nov 22, 2022 9:24 am

Re: uart_read_bytes

Postby Giuseppe1412 » Thu Dec 01, 2022 2:15 pm

Hi,
I don't understand the difference between :
uart_set_rx_timeout and the TickType_t ticks_to_wait in uart_read_bytes.
I think that if in :
uart_read_bytes(uart_num, buf, length, ticks_to_wait)

I set:
length=5
and rx timeout=1, (the time to receive a byte to the baud setted)
If I don't receive 5 byte before the timeout expired, in the buf I have the bytes that I received;
if I received 3 bytes: 0xA, 0xB, 0xC,
I have: len=3 buf[0]=0xA, buf[1]=0xb,....
but what is ticks_to_wait for?
I tried to print a messange exchange between two peripherals:
1) I write from periph 1 to periph 2 with: uart_write_bytes, 5 bytes
2) In periph 1 read the responce of periph 2 with uart_read_bytes. (the responce is 9 byte)
3) send an ack from periph 1 to periph 2 (3 byte)

If I set rx timeout=1 and ticks_to_wait=100, I can send the ack after 100 ms, if I set ticks_to_wait=50, I can send the ack after 50 ms,
If I set the ticks_to_wait=10 ms I receive corrupted byte in 2) and also the ack that I send in 3) appears to be corrupted.

Thanks.
Best regards.

Who is online

Users browsing this forum: No registered users and 88 guests