Page 1 of 1

When is UART.write finished?

Posted: Sat Sep 11, 2021 1:58 pm
by cubbieco
I'm working with a max485 board on a Raspberry Pi Pico. I'm fairly new to python. Anyway I'm really wondering if there is any way to tell when the uart.write is finished? I've been searching all over and I'm not finding anything that I understand (one example redid the UART class but I didn't understand the code or how to implement it). The code below works but I'd need a longer delay for longer messages / lower baudrates and I'd like to avoid using sleep is possible.

Thanks

Code: Select all

from machine import Pin, UART
import time

uart = UART(0,115200,tx=Pin(0),rx=Pin(1))
txpin = Pin(3,Pin.OUT)
txpin.value(1)
writeme = "Testing 1 2 3\r\n"
writeme = writeme.encode()
writeme = bytearray(writeme)
uart.write(writeme)
time.sleep(0.002)
txpin.value(0)

Re: When is UART.write finished?

Posted: Sat Sep 11, 2021 2:29 pm
by Roberthh
Actually there is no API or method checking whether all UART data has been sent. As far as I recall, it was discussed but not implemented. So I see two ways of doing it as of now:
a) Calculate a delay based on the UART baud rate and message size and wait for that time ( like time.sleep_us((1_000_000 * 10 * msg_len) // baud_rate) )
b) Connect the UARt output to another UART input on that module and wait until you have all symbols received, that you have sent.

a) is not overly precise. You would have to add some guard time.
b) Is precise, but waste of resources.

So yes, a method for telling if all has been sent (the opposite to uart.any()) would be helpful.