Serial Port losing 16th digit

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Trikkitt
Posts: 10
Joined: Mon Aug 13, 2018 8:50 am

Serial Port losing 16th digit

Post by Trikkitt » Wed Jul 28, 2021 11:33 pm

I'm running the latest 1.16 version of Micropython on an ESP8266. I'm capturing the serial port away from REPL and directing it to my code. This code is something I wrote a few years ago that should just work, but I have change the version of Micropython so while I can't rule out my code it does look suspiciously like a Micropython issue. For some reason the 16th character being read from the serial port goes missing!

Diags: I've connected a serial port from my PC to the serial port receive of the ESP8266 so I can see what data it is actually receiving, and I can see the string arrive perfectly formed. It is just some text (in this case a hex string). I tried lowering the connection speed from 115200 to 38400 in case something was going wrong with the framing, but this made no difference. I tried changing the onward delivery format in case there was something at the network end that cause it, but still the same digit is missing. I tried changing the uart.read command to specify only 5 digits and concatenate them together, but unfortunately that didn't change the results.

The following is a slimmed down piece of the code:

uart = UART(0,38400)
uart.init(38400, bits=8, parity=None, stop=1, timeout=1)
serveraddr = usocket.getaddrinfo(servername,40001,0, usocket.SOCK_STREAM)[0][-1]
s=usocket.socket()
s.connect(serveraddr)
while True:
if (uart.any()>0):
serbuf=uart.read()
s.setblocking(True)
s.write(serbuf)
s.setblocked(false)

The serial data arrives minus the 16th digit over my TCP connection. With larger amounts of data every 16th digit is missing.

Am I doing something wrong? Or is there an issue with this version of Micropython?

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

Re: Serial Port losing 16th digit

Post by Trikkitt » Thu Jul 29, 2021 8:07 am

Well I've now found the issue. Looks like a bug in Micropython introduced somewhere between 1.9 and 1.16. The serial receive buffer needs settings to 16 because the default is 15 and the underlying code had a bug when using 15 as the buffer size. Please will someone change the default buffer size to 16, even if the underlying bug remains. It'll solve the issue for most people.

Post Reply