forked from tanchou/Verilog
Semaine 6 init
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import serial
|
||||
import time
|
||||
import struct
|
||||
|
||||
# === Paramètres de communication ===
|
||||
SERIAL_PORT = "COM6" # Modifie selon ton système, ex. "/dev/ttyUSB0" sur Linux
|
||||
BAUDRATE = 115200 # Change si différent
|
||||
TIMEOUT = 2 # secondes
|
||||
|
||||
# === Commandes (doivent correspondre aux valeurs Verilog) ===
|
||||
CMD_STOP = 3
|
||||
CMD_ONE = 1
|
||||
CMD_CONTINUOUS = 2
|
||||
|
||||
def send_command(ser, command):
|
||||
"""Envoie une commande au FPGA"""
|
||||
ser.write(bytes([command]))
|
||||
|
||||
def read_distance(ser):
|
||||
"""Lit 2 octets et les convertit en distance (int16)"""
|
||||
data = ser.read(2)
|
||||
if len(data) != 2:
|
||||
return None
|
||||
# Interprétation little-endian (LSB, MSB)
|
||||
lsb, msb = data[0], data[1]
|
||||
distance = msb << 8 | lsb
|
||||
return distance
|
||||
|
||||
def main():
|
||||
with serial.Serial(SERIAL_PORT, BAUDRATE, timeout=TIMEOUT) as ser:
|
||||
print("Connexion ouverte sur", SERIAL_PORT)
|
||||
|
||||
mode = input("Mode (one / continuous / stop) ? ").strip().lower()
|
||||
|
||||
if mode == "one":
|
||||
send_command(ser, CMD_ONE)
|
||||
print("Mesure unique demandée. Attente résultat...")
|
||||
time.sleep(0.05)
|
||||
distance = read_distance(ser)
|
||||
if distance is not None:
|
||||
print(f"Distance mesurée : {distance} cm")
|
||||
else:
|
||||
print("Erreur : distance non reçue.")
|
||||
|
||||
elif mode == "continuous":
|
||||
send_command(ser, CMD_CONTINUOUS)
|
||||
print("Mesures continues (CTRL+C pour stopper) :")
|
||||
try:
|
||||
while True:
|
||||
distance = read_distance(ser)
|
||||
if distance is not None:
|
||||
print(f"Distance : {distance} cm")
|
||||
else:
|
||||
print("... (aucune donnée reçue)")
|
||||
time.sleep(0.1)
|
||||
except KeyboardInterrupt:
|
||||
send_command(ser, CMD_STOP)
|
||||
print("\nMesure continue arrêtée.")
|
||||
|
||||
elif mode == "stop":
|
||||
send_command(ser, CMD_STOP)
|
||||
print("Commande STOP envoyée.")
|
||||
|
||||
else:
|
||||
print("Commande invalide.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user