Page 1 of 1
UART read data with a delay.
Posted: Wed Aug 11, 2021 8:14 am
by prem111
Hi, im have problem in my update library (link below).
In def write16 (272 line), The module echoes what it got, unfortunately my response data is delayed, what am I doing wrong? I would be very grateful for your help !
RuntimeError("Response doesn't match send data")
Sorry to write about it here, but the users here are my hope.
Re: UART read data with a delay.
Posted: Wed Aug 11, 2021 9:10 am
by Roberthh
You did not tell which board you are using, but the general principle is the same. When you call uart.write(), the device will start to send the data you have provided. There are various buffers in the send stack and the uart hardware itself. So the call to write() will return after it has put the data into these buffers. The transmitting can continue to happen even after the return. If you call then immediately read(), you will only get what has been sent (and received) so far, not the full message. uart.read((n) will return up to n bytes, but may return less if a timeout occurs.
One solution for you is to set a timeout for read matching the number of bytes sent and the data rate. Or you can simply wait a while before calling read().
Re: UART read data with a delay.
Posted: Wed Aug 11, 2021 11:59 am
by prem111
Roberthh wrote: ↑Wed Aug 11, 2021 9:10 am
You did not tell which board you are using, but the general principle is the same. When you call uart.write(), the device will start to send the data you have provided. There are various buffers in the send stack and the uart hardware itself. So the call to write() will return after it has put the data into these buffers. The transmitting can continue to happen even after the return. If you call then immediately read(), you will only get what has been sent (and received) so far, not the full message. uart.read((n) will return up to n bytes, but may return less if a timeout occurs.
One solution for you is to set a timeout for read matching the number of bytes sent and the data rate. Or you can simply wait a while before calling read().
Thanks Roberthh
Re: UART read data with a delay.
Posted: Wed Aug 11, 2021 1:28 pm
by prem111
Im have one another question, in def _write_data (544 line), I'm not sure if it does the right thing for the package following this example:
PACKET [1024 + 2] = DATE [1024] + CHKSUM [2]
I do not know if I calculate the checksum correctly, and i do not know data write correctly.
Of course my packet len is 256, in example 1024.
Re: UART read data with a delay.
Posted: Wed Aug 11, 2021 1:42 pm
by Roberthh
How could I tell if I do not know what the whole package is expected to do.
Re: UART read data with a delay.
Posted: Wed Aug 11, 2021 1:47 pm
by prem111
Thanks for help !