stm32-fmt-code/access_control_python/stm32.py

31 lines
741 B
Python
Raw Normal View History

2023-11-17 13:46:16 +00:00
import serial
2023-11-17 15:43:10 +00:00
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
2023-11-17 13:46:16 +00:00