Reading UART Values In Sequence One Byte At A Time

Hello,

I am trying to get an OpenMV H7 Plus board to read values off of the LD06 lidar. Since the lidar can only communicate with the OpenMV H7 Plus board through UART, I followed the instructions of the documentation and connected the lidar to the P4 and P5 pins of the OpenMV H7 Plus board. While I am able to read the correct values off of the LD06 lidar, I am not reading all of the data that I expected. More specifically, I am expecting that the lidar writes data into a buffer, and the OpenMV H7 Plus reads data off of the buffer one byte at a time (the buffer would be flushed under certain conditions). However, the OpenMV H7 Plus seems to be flushing the buffer after reading one byte of data every time serial.read(1) is called. I have written the same code (but using CPython) on a Raspberry Pi Zero 2 and was able to read the values one byte at a time in sequence as I expected. Since being able to read all the data and having all the data in sequence are important for my project, please provide any possible solutions to the problem. My code is as follows.

from pyb import UART

serial = UART(3)
serial.init(baudrate=230400, bits=8, parity=None, stop=1)

while True:
      if serial.any():
          read_byte = serial.read(1)
      else:
          continue
      print(read_byte)

Thank you!

Thomas

Hi, you might want to specify timeout and timeout_char in the arguments of the serial port init. class UART – duplex serial communication bus — MicroPython 1.19 documentation The default arguments shipping with MicroPython kinda make the serial port hard to use.

Anyway, I’m able to receive single characters with this script… and I don’t get any character drops.

Not sure how to help you debug. This works:

from pyb import UART
import time

serial = UART(3)
serial.init(baudrate=230400, bits=8, parity=None, stop=1)

while True:
      if serial.any():
          read_byte = serial.read(1)
      else:
          time.sleep(5)
          continue
      print(read_byte)

If I send 10 chars between the read then I get all bytes as expected.