uasyncio streamreader hanging on uart.readline

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

uasyncio streamreader hanging on uart.readline

Post by p_j » Fri Jun 17, 2022 2:17 pm

I'm seeing some strange behaviour when using streamreader. ESP32-S2, micropython latest from github.

When I call readline() on the streamreader it never returns, even though lines of text are being sent to the UART.

When I call read() I can read the bytes individually.

Example:

This code hangs

Code: Select all

async def _run(self):
    self._sreader = asyncio.StreamReader(self.uart)

    while True:
        res = await self._sreader.readline()
This works ok (but I have to manually process the buffer periodically)

Code: Select all

async def _run(self):
    self._sreader = asyncio.StreamReader(self.uart)

    while True:
        await asyncio.sleep_ms(50)
        ava = self.uart.any()
        res = await self._sreader.read(ava)
        self.rx_buffer += res.decode('utf8')
        
Any ideas?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio streamreader hanging on uart.readline

Post by pythoncoder » Fri Jun 17, 2022 5:52 pm

Hmm. I'm getting no joy at all. According to the docs for my Feather S2 the rx and tx pins are connected to UART 0. However a simple loopback test doesn't work. .any() returns some stupid number. .write(b'hello\n') returns nothing (should return 6). And .read() produces nothing.

I tried UART 1: .write and .any returned sensible numbers but .read never produced anything.

Which UART and I/O pins are you using?
Peter Hinch
Index to my micropython libraries.

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: uasyncio streamreader hanging on uart.readline

Post by p_j » Mon Jun 20, 2022 2:02 am

pythoncoder wrote:
Fri Jun 17, 2022 5:52 pm
Hmm. I'm getting no joy at all. According to the docs for my Feather S2 the rx and tx pins are connected to UART 0. However a simple loopback test doesn't work. .any() returns some stupid number. .write(b'hello\n') returns nothing (should return 6). And .read() produces nothing.

I tried UART 1: .write and .any returned sensible numbers but .read never produced anything.

Which UART and I/O pins are you using?
Thanks Peter, I'm using UART0 with pins 37/38
UART(0, baudrate=115200, tx=37, rx=38, timeout=0)
I did have to make a small change in machine_uart.c in order to use UART0 on the ESP32-S2 so let me revert my changes and see if the same behaviour happens in UART0 for me. There is probably a valid reason why UART0 isn't available on the S2 for general use.

I will report back!

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: uasyncio streamreader hanging on uart.readline

Post by p_j » Tue Jun 21, 2022 3:43 am

Further strangeness.

When I compile Micropython with TinyUSB disabled the uasyncio streamreader works as expected.

I imagine this is something to do with the differences between the ESP32 and ESP32-S2 possibly. I'm going to continue with usb disabled for now and come back to do some more debugging on this later.

Will raise an issue when I have more info.

Post Reply