Random UART data loss - 16 byte issue

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Trikkitt
Posts: 10
Joined: Mon Aug 13, 2018 8:50 am

Random UART data loss - 16 byte issue

Post by Trikkitt » Thu Mar 24, 2022 2:21 am

I'm using 1.18 on an ESP8285 and I'm seeing the classic buggy 16 byte character limit being hit when reading from the UART. The two symptoms are:
- Sometimes the read process ends at the 16 byte limit for no apparent reason
- Sometimes the 16th digit gets lost

It can do this no matter the UART settings I'm using regarding rxbuf size. I've tried it with 15, 16, 100 and 256 buffer limits and get the same random behaviour with all of them. I know the data is correct going in to the ESP8285 because i've watched it on the logic analyser. So clearly this is an issue within the UART implementation of Micropython.

The following code is called to setup the serial port.

Code: Select all

uos.dupterm(None,1)
uart = UART(0,115200)
uart.init(115200, bits=8, parity=None, stop=1, timeout=20, timeout_char=20, rxbuf=256)
The following code is called in a tight loop.

Code: Select all

if (uart.any()):
    cmd=""
    serialline=uart.readline()
    print("Received serial cmd: " + str(serialline))
    if len(serialline)>1:
        try:
          cmd=serialline.decode("utf-8")
          cmd=cmd.replace(chr(10),'').replace(chr(13),'')
        except:
          cmd=serialline
	if (cmd[0]=='C' and mqttclient!=None):
            payloaddict={"cardtoken": cmd[1:], "timestamp": utime.time()}
        
I've tried substituting the uart.readline() for uart.read() and it makes no difference. The behaviour is seemingly random, sometimes it loses the 16th digit but gets the whole message except that one missing digit, sometimes it truncates at 16 digits and thats all I get.

This behaviour is driving me nuts! How can I work around the issue, or is this simply broken in Micropython and I need to look elsewhere?

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

Re: Random UART data loss - 16 byte issue

Post by Roberthh » Thu Mar 24, 2022 8:39 am

Just a side note:
you do not need a separate uart.init() call. You can put all the settings into the UART instantiation. UART(...) calls internally uart.init() will all the provided arguments expect for the first one.

Trikkitt
Posts: 10
Joined: Mon Aug 13, 2018 8:50 am

Re: Random UART data loss - 16 byte issue

Post by Trikkitt » Thu Mar 24, 2022 9:11 am

Roberthh wrote:
Thu Mar 24, 2022 8:39 am
Just a side note:
you do not need a separate uart.init() call. You can put all the settings into the UART instantiation. UART(...) calls internally uart.init() will all the provided arguments expect for the first one.
Thanks - I noticed this on some other code and tried changing it last night. Yup - that works, but sadly doesn't solve my issue. Any ideas what I can do about the serial data loss that seems worse than earlier versions of Micropython?

Post Reply