forked from tanchou/Verilog
27 lines
775 B
Python
27 lines
775 B
Python
import serial
|
|
|
|
# Configuration du port série
|
|
ser = serial.Serial(
|
|
port='COM6', # Remplace par ton port (ex : '/dev/ttyUSB0' sur Linux)
|
|
baudrate=115200,
|
|
timeout=1 # 1 seconde d'attente max pour la lecture
|
|
)
|
|
|
|
print("Lecture série en cours (Ctrl+C pour arrêter)...")
|
|
|
|
try:
|
|
while True:
|
|
if ser.in_waiting >= 2:
|
|
# Lecture des 2 octets (MSB en premier)
|
|
high_byte = ser.read()
|
|
low_byte = ser.read()
|
|
|
|
if high_byte and low_byte:
|
|
# Conversion en entier 16 bits
|
|
distance = (high_byte[0] << 8) | low_byte[0]
|
|
print(f"Distance mesurée : {high_byte}, {low_byte} cm")
|
|
except KeyboardInterrupt:
|
|
print("\nArrêt manuel.")
|
|
finally:
|
|
ser.close()
|