forked from tanchou/Verilog
Loopback ne fonctionne pas
This commit is contained in:
43
Semaine_4/UART/tests/Python/uart_loopback_test.py
Normal file
43
Semaine_4/UART/tests/Python/uart_loopback_test.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import serial
|
||||
import time
|
||||
|
||||
# À adapter selon ton système
|
||||
PORT = 'COM7' # ex: COM3 sur Windows ou /dev/ttyUSB0 sur Linux
|
||||
BAUDRATE = 115200
|
||||
TIMEOUT = 3 # en secondes
|
||||
|
||||
def main():
|
||||
try:
|
||||
with serial.Serial(PORT, BAUDRATE, timeout=TIMEOUT) as ser:
|
||||
print(f"[INFO] Connecté à {PORT} à {BAUDRATE} bauds.")
|
||||
print("Tape un nombre entre 0 et 255. Ctrl+C pour quitter.\n")
|
||||
|
||||
while True:
|
||||
user_input = input("Nombre à envoyer (0-255) : ")
|
||||
|
||||
if not user_input.isdigit():
|
||||
print("⚠️ Entrée invalide. Tape un entier entre 0 et 255.")
|
||||
continue
|
||||
|
||||
value = int(user_input)
|
||||
if value < 0 or value >= 255:
|
||||
print("⚠️ Valeur hors limites.")
|
||||
continue
|
||||
|
||||
byte = bytes([value])
|
||||
ser.write(byte)
|
||||
print(f"[TX] Envoyé : {value} (0x{value:02X})")
|
||||
|
||||
time.sleep(0.01) # petite pause si nécessaire
|
||||
|
||||
rx = ser.read(1)
|
||||
if rx:
|
||||
print(f"[RX] Reçu : {int.from_bytes(rx, 'little')} (0x{rx.hex()})\n")
|
||||
else:
|
||||
print("⚠️ Aucun octet reçu (timeout ?)\n")
|
||||
|
||||
except serial.SerialException as e:
|
||||
print(f"[ERREUR] Port série : {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user