UART read makes OpenMV IDE hang


does anyone know a way to clear the UART buffer when starting a script in micropython?
A few weeks ago I was trying to write data received on my UART to an mjpeg video (trying to see what kind of latency I could expect). Problem was as soon as I tried to read from the UART buffer, the OpenMV IDE would just stop working, it never finished recording the video (I was using a modified version of the example video record script). Plus the frame rate was awful (at most 1 fps). After a few hours (trying to figure out a minimum working example of this failure to post here) I solved the problem by increasing the size of my receiving bytearray for UART data from 300 to 10000. It turns out it also worked if I simply turned on the OpenMV cam before turning on the system sending data over UART.

Now I’m implementing on my real system, and I’m encountering the same problem. My system is receiving 270 bytes per second. Even with my bytearray set to 10000 it doesn’t work (and trying 20000 gives me a memory allocation error). If I power on the OpenMV cam before I power on the transmitting UART device, it seems to work just fine, but if I power them both up at the same time the OpenMV IDE will fail. The first time I hit the “play” button it simply disconnects, the second time it will hang. Any ideas for a more robust solution to this than simply telling my transmitting system to wait a bit after power up before sending?

Hi, what firmware version are you using? Is it the latest? Also, can you post your code? Preferably something that just shows the problem and nothing else.

Good point actually, I’m using my own firmware since I’m using some custom functions. I’ll see if I can get a minimal example using the stock firmware for you and post that.

If you didn’t change the serial stuff that’s okay. I just need a minimal example. Serial seems to work the the best on the system so I’m not sure what’s wrong.

Hey Kwabena,

Of course now that I’ve posted this I’m having trouble reproducing - I had worked around it by having my uart sender wait 10s before starting up. I’ll see if I can reproduce tomorrow or Tuesday and try to get you something then. I’ve been at this for too long today, not able to think properly.

Hey Kwabena,

I think this was a false alarm. I probably was sometimes forgetting to put the SD card in the OpenMV, because that seems to be the main way of replicating the symptoms I was seeing. Might also have been my problem a few weeks back, because even that code seems to work just fine now. Sorry about that.

Hey Kwabena,
I reproduced this error. I’ve tried both and uart.readall(), and playing around with the parameters a bit. The code is simple as can be:

import time
from pyb import UART

uart = pyb.UART(3, 115200, timeout_char = 2000)

print(“in the True loop”)
if uart.any():
print(“data on the uart:”)

I’m sure it’s something dumb, but having trouble figuring out what. Anyways the behaviour I see is it will print “in the True loop” every second until data starts being received on the uart.
It will then usually hang and disconnect. If I try to reconnect, it doesn’t detect an OpenMV cam. Is this something you can reproduce on your end? What obvious thing am I missing?

Hi, don’t block on reading the UART. Interrupts are disabled when doing that… We use MP calls which call the STM Hal. It should get fixed but… For now, can available () in a loop and just pull a byte out as you get one.

Also, the current IDE has way to small of a serial timeout. We’re about to release v1.7.0 which fixes that.