import serial

def main():
    try:
        with serial.Serial('COM5', 115200, timeout=1) as ser:
            buffer = []
            while True:
                byte = ser.read(1)
                if byte:
                    value = byte[0]
                    buffer.append(value)
                    print(f"Reçu: {value:#04x}")

                    # Vérifie début de trame
                    if len(buffer) == 1 and buffer[0] != 0x02:
                        buffer.clear()

                    # Vérifie fin de trame
                    if len(buffer) >= 3 and buffer[-2] == 0x1B and buffer[-1] == 0x03:
                        print("\n=== Trame complète reçue ===")
                        print("Trame :", ' '.join(f"{b:#04x}" for b in buffer))
                        print("=============================\n")
                        buffer.clear()
    except serial.SerialException as e:
        print("Erreur de port série :", e)

if __name__ == "__main__":
    main()
