import serial import threading class stm32_distance(): data: int = 0 def __init__(self, serial_port: str) -> None: self.serial_port = serial_port self.ser = serial.Serial(self.serial_port, 115200) self.thread = threading.Thread(target=self.__read_data) self.thread.start() def __read_data(self): while True: data = b'' while True: byte = self.ser.read() if byte == b'\x03': break data += byte data_str = data.decode('utf-8') try: self.data = int(data_str) except: pass def get_distance(self): return self.data