When is UART.write finished?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
cubbieco
Posts: 4
Joined: Sat Sep 11, 2021 1:46 pm

When is UART.write finished?

Post by cubbieco » Sat Sep 11, 2021 1:58 pm

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)

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

Re: When is UART.write finished?

Post by Roberthh » Sat Sep 11, 2021 2:29 pm

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.

Post Reply