forked from tanchou/Verilog
uart v3
This commit is contained in:
26
Semaine_3/UARTV3/uartread.py
Normal file
26
Semaine_3/UARTV3/uartread.py
Normal file
@@ -0,0 +1,26 @@
|
||||
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()
|
Reference in New Issue
Block a user