uart_serial_communication.py
Bidirectional communication example on the Xerxes Nano UART (TX/RX) port to interact with external serial devices.
Hardware: Serial device connected to Xerxes Nano UART pins (TX, RX, GND). Software: Python 3, pyserial library. Execution: pip install pyserial then python3 uart_comm.py. Check serial port permissions.
uart_serial_communication.py
#!/usr/bin/env python3
import serial
import time
# Port série typique sur Jetson/Turing (à vérifier avec 'dmesg' ou 'ls /dev/tty*')
SERIAL_PORT = '/dev/ttyTHS1' # Exemple pour Jetson Orin Nano
BAUD_RATE = 115200
try:
# Ouverture du port série
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1)
print(f"Connecté à {SERIAL_PORT}")
# Envoi d'une commande
ser.write(b'PING\n')
print("Commande PING envoyée")
# Lecture de la réponse
time.sleep(0.1)
if ser.in_waiting > 0:
response = ser.readline().decode('utf-8').strip()
print(f"Réponse reçue: {response}")
else:
print("Aucune réponse reçue")
except serial.SerialException as e:
print(f"Erreur série: {e}")
print("Vérifiez les permissions (sudo usermod -a -G dialout $USER) et le port.")
except Exception as e:
print(f"Erreur: {e}")
finally:
if 'ser' in locals():
ser.close()