does uart.read(1) flush rx buffer

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
etc3
Posts: 2
Joined: Sat Feb 20, 2021 1:32 pm

does uart.read(1) flush rx buffer

Post by etc3 » Sat Feb 20, 2021 4:43 pm

We are using esp32 and trying to read 900 characters from the uart at 9600 baud. A typical way to do this is to read one character at a tim e as they arrive in the uart and store it. We seem to need to increase the uart rxbuffer size to at least 900 for this to work reliably. Example is below:

Code: Select all

    while True:
        if port.any():
            x = port.read(1)
            xstr = x.decode('utf-8')
            if xstr == "\n":
                break
            response += xstr
I would think that each port.read(1) call would clear 1 character from the uart rxbuffer FIFO and we should be able to reliably read in characters continuously and for much longer than the rxbuf length as long as we do it fast enough. This doesn't appear to be the case with esp32. In one test with a 917 character message, we receive the first 360 characters, and the last 41, but are missing everything in between, using the default uart.init() call, which for the esp32 has default rxbuf=256. If we call uart.init(.... rxbuf=1024) it all works reliably. Is there something we are doing wrong here?

User avatar
Roberthh
Posts: 2462
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: does uart.read(1) flush rx buffer

Post by Roberthh » Sat Feb 20, 2021 5:38 pm

What seems to happen, is that reading in the characters byte by byte in your script is too slow. The problem is most likely the "response += xstr", which creates a new object on every call, with all the overhead of object allocation and eventually garbage collection. . If then the receive buffer is full, the remaining characters get lost.
You can try to read larger chunks, or pre-allocate a sufficiently large bytearray to store the incoming characters by index.

etc3
Posts: 2
Joined: Sat Feb 20, 2021 1:32 pm

Re: does uart.read(1) flush rx buffer

Post by etc3 » Sat Feb 20, 2021 7:40 pm

Thank you for the insight. Did a quick test to measure the max execution time of the while True loop and indeed it measured 11mSec max. So, if characters are arriving every 1.04mSec (9600 baud) and coming out slower than that at some point the rxbuffer will run out of space, just as you expected.

Post Reply