UART timeout and timeout_char is not working in ESP32
Hi,
I noticed that UART timeout_char is not working. Regardless of the value of this parameter, UART always waits for around 8x the time of a character to identify that communication has ended.
With the help of a forum contributor, we were able to locate and fix the problem.
In the machine_uart.c we add the following command to line 208:
uart_set_rx_timeout(self->uart_num, self->timeout_char);
==================================================
Now that code snippet looked like this:
// set timeout_char
// make sure it is at least as long as a whole character (13 bits to be safe)
self->timeout_char = args[ARG_timeout_char].u_int;
uint32_t min_timeout_char = 13000 / baudrate + 1;
if (self->timeout_char < min_timeout_char) {
self->timeout_char = min_timeout_char;
}
uart_set_rx_timeout(self->uart_num, self->timeout_char);
==================================================
I don't know if timeouts have been disabled for a specific reason, but I would like to report that this minor fix resolves the issue, at least for timeout_char.
UART reaching timeout after first byte.
Port, board and/or hardware
Raspberry Pi Pico with RP2040
MicroPython version
MicroPython v1.25.0 on 2025-04-15;
Reproduction
When i set UART.timeout=200 and UART.timeout_char=50 and ask for 5 bytes and receive immediately just 3, then the reception takes 253ms.
from machine import Pin, UART
import time
uart = UART(1)
uart.init(38400,
bits=8,
parity=0, # even
stop=1,
tx=Pin(8),
rx=Pin(9, pull=Pin.PULL_UP),
txbuf=16,
rxbuf=16,
timeout=200, # ms
timeout_char=50) # ms
print(uart)
for addr in range(60):
t1 = time.ticks_ms()
uart.write(bytes((0xA5, addr))) # 0xA5, address
rx = uart.read(5)
t2 = time.ticks_ms()
print('RX "%s" %u ms' % (
''.join(["%02X" % b for b in rx]),
time.ticks_diff(t2, t1)))
UART(1, baudrate=38400, bits=8, parity=0, stop=1, tx=8, rx=9, txbuf=32, rxbuf=32, timeout=200, timeout_char=50, invert=None, irq=0)
RX "000054" 253 ms
RX "0053" 253 ms
RX "0052" 253 ms
RX "000051" 254 ms
RX "000050" 253 ms
RX "000037" 253 ms
RX "00455C95" 254 ms
RX "0000002F" 254 ms
RX "000000200E" 2 ms
RX "000000002D" 3 ms
RX "0000010B20" 2 ms
RX "18B32E32" 254 ms
RX "0000002A" 254 ms
It takes just 3ms with the exact number of bytes (5 in this example).
Expected behaviour
The total duration should be around timeout_char (not timeout + timeout_char) when the device respond immediately with less bytes than requested.
Observed behaviour
When receiving fewer bytes than asked, the observed duration of the reception is timeout + timeout_char.
Additional Information
I didn't find anything wrong in the implementation of the RP port:
https://github.com/micropython/micropython/blob/f498a16c7db6d4b2de200b3e0856528dfe0613c3/ports/rp2/machine_uart.c#L593
I thinks the issue lies in stream.c.
I guess one of the loop is calling again mp_machine_uart_read if the count of bytes is not reached.
Code of Conduct
Yes, I agree