Read from UART - Problem.

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
prem111
Posts: 33
Joined: Sun Feb 23, 2020 3:18 pm

Read from UART - Problem.

Post by prem111 » Mon Mar 02, 2020 9:19 pm

Hi. I'm using uasyncio.StreamReader to read data from uart, the problem is sending several packets at once and catching the delimiter which is e.g: "7E"

I thought about StreamReader.readexactly(1), i.e. reading a single character and catching the delimiter, but with large packet it works slowly.

Example packet "b'BE0003123123E6'" means: 7E: is "delimiter" 0003: is "lenght of packet" 123123: data E6: checksum.

When a single packet comes, it's OK. The problem is when I have several packages at once E.g:

Code: Select all

b'BE0005123123E67E0005123123E67E000512'
b'3123E6'
Please help! Thanks.
Last edited by prem111 on Wed Mar 04, 2020 5:45 am, edited 1 time in total.

kevinkk525
Posts: 648
Joined: Sat Feb 03, 2018 7:02 pm

Re: Read from UART - Problem.

Post by kevinkk525 » Mon Mar 02, 2020 10:03 pm

Check: https://github.com/peterhinch/micropyth ... as.py#L408
This method is basically what you are trying to do. 1st you read 1 byte until you hit the delimiter, then 4 bytes for your packet length and then the legth of the packet.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

prem111
Posts: 33
Joined: Sun Feb 23, 2020 3:18 pm

Re: Read from UART - Problem.

Post by prem111 » Tue Mar 03, 2020 9:44 am

reading a single byte is slow or just seems to me?

read 200 (singly) bytes = 3 seconds:
recv = await sreader.read(1)

read 200 (together) bytes = 1 seconds.
recv = await sreader.read(200)

kevinkk525
Posts: 648
Joined: Sat Feb 03, 2018 7:02 pm

Re: Read from UART - Problem.

Post by kevinkk525 » Tue Mar 03, 2020 11:25 am

That is why you only read a single byte until you get your delimiter. after that your read 4 bytes for your "header" and then the amount of bytes that your packet is according to your header.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

prem111
Posts: 33
Joined: Sun Feb 23, 2020 3:18 pm

Re: Read from UART - Problem.

Post by prem111 » Tue Mar 03, 2020 11:54 am

What if when the second part comes with the second packet ?

kevinkk525
Posts: 648
Joined: Sat Feb 03, 2018 7:02 pm

Re: Read from UART - Problem.

Post by kevinkk525 » Tue Mar 03, 2020 3:47 pm

Not sure what you mean?
There are no packets, you just put bytes onto the UART. It doesn't matter if they are sent byte by byte or two of your "packets" together as 100 consecutive bytes.
You still read the stream of bytes until you find your delimiter. After that you read your header and after that your message.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply