UART timeout_chars not working



  • I have a sensor which sends out a (newline terminated) data packet every two seconds via a asynchronous serial line. To capture those, I thought:

    • set UART timeout to 3 s
    • use uart.readline()

    The first readline might capture a partial message, but the following readline operations will return complete packets.
    As it turns out the timeout does not work properly. For example, if we set the baud rate to 9600, a timeout_chars value of 1200 (9600/8) should result in a 1 s timeout (well, actually it takes 10 bits to send 8 bits with 1 start/stop bits). I ran the following test with no input data at RX:

    import machine
    from machine import UART
    from machine import Timer
    
    chrono = Timer.Chrono()
    
    uart = UART(1)
    uart.init(baudrate=9600, timeout_chars=1200) #1s timeout
    
    chrono.start()
    i=0
    while i<20:
        chrono.reset()
        uart.readline()
        print(chrono.read())
        i=i+1
    

    But we get 0.3 s timeout only... Experimenting with different values, it seems that only values up to 255 work. 256 or 257 cause the program to freeze.
    A timeout_chars value of 255 results in a timeout of 0.44s, while I'd expect 255/9600 * 8 = 0.21 s (or 255/9600*10 = 0.26 s).
    So this doesn't really work. BTW I intended to rund the sensor communication at 115200 baud, which would need much bigger values.

    Suggestions?


 

Pycom on Twitter