import platform import serial import time def invia_e_leggi_frase(porta_seriale): try: porta_seriale.write(b'Connection OK') time.sleep(1) received_data = porta_seriale.read(15) if not received_data: print("Nessun dato ricevuto") else: print(f'Frase ricevuta: {received_data.decode("utf-8")}') except Exception as e: print(f"Errore: {e}") is_win = platform.system().lower() == "windows" if is_win: dev="COM5" else: dev = '/dev/ttyUSB0' try: ser = serial.Serial(dev, 9600, timeout=1, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS) if ser.is_open: print(f'Connesso alla porta COM5') try: while True: invia_e_leggi_frase(ser) time.sleep(1) except KeyboardInterrupt: pass finally: ser.close() else: print(f'Impossibile aprire la porta COM5') except OSError as e: print(f"Errore nell'apertura della porta COM5: {e}") except Exception as e: print(f"Errore generico: {e}")