I’ve seen documentation suggesting that interrupt handler code should be kept as short and simple as possible. Is this a safe and correct approach?
from machine import UART
import time
received_bytes = None
def irq_handler(uart):
global received_bytes
received_bytes = uart.readline()
uart = UART(3)
uart.init(
baudrate=460800,
bits=8,
parity=None,
stop=1,
)
irq = uart.irq(handler=irq_handler, trigger=UART.IRQ_RXIDLE)
while True:
time.sleep_ms(20)
print(received_bytes)
That would allocate memory, so, you don’t want to do that. You need to use the readinto(): class UART – duplex serial communication bus — MicroPython 1.25 documentation
What do you need an IRQ for receiving for?
I don’t understand the details of IRQ. I heard that IRQ makes communication fast and efficient.
My objective is to communicate safely and efficiently between OpenMV H7 Plus and other STM microcontrollers. For example, the OpenMV board may receive data like “1.0,2.0,3.0\n” and store it in a variable as a list of float values to be used later in the main loop.
I tried to use “if uart.any(): received_bytes = uart.readline()” but sometimes I see the received bytes missing some characters. But when I use IRQ, it looks like the communication isn’t having trouble.
Can you suggest what should I do generally for UART communication on OpenMV?
I just use uart.any() in a loop. If you are missing characters, the receive buffer might be too small. You can control it’s size when you create the UART object.
If you want to do an IRQ that’s fine too. Just note that the IRQ needs to exit quickly and cannot allocate memory.