import serial
from time import sleep
from dataclasses import dataclass
# Set up the serial port
ser = serial.Serial('/dev/ttyUSB0', 115200)  # Replace with the appropriate port name and baud rate

# Define a list of hexadecimal numbers to send
error = bytes.fromhex('ff e3')
numbers = [i for i in range(255)]

@dataclass
class Result:
    sent: bytes = b''
    result: bytes = b''

results = []
for num in numbers:
    byt = num.to_bytes(num, "big")
    print("Writing: " + hex(num))
    ser.write(byt)
    sleep(0.01)

    ser.write(bytes.fromhex('f9'))

    data = ser.read(ser.in_waiting)

    print(data)
    print(error)
    if data not in error and data != b'':
        result = Result(sent=num, result=hex(int.from_bytes(data, "big")))
        results.append(result)
    sleep(0.1)




for res in results:
    print(res)

# Close the serial port
ser.close()
