7 def read_and_respond():
10 ser = serial.Serial(port, baudrate=9600, timeout=0.1)
11 print("Serial port",port,"opened successfully.")
12 except serial.SerialException as e:
13 print("Error opening the serial port ",port,":", e)
17 # Read 4 bytes from the serial port
18 read_bytes = ser.read(size=4)
20 if len(read_bytes) == 0:
23 if read_bytes == b"\x14\x82\x80\x81":
24 response = b"\x54\x82\x80\x81"
25 elif read_bytes == b"\x14\x81\x80\x81":
26 response = b"\x54\x81\x80\x81"
28 response = b"\xDE\xAD\xBE\xEF"
30 # Send the response back
33 print("Received:", read_bytes, read_bytes.hex())
34 print("Response:", response, response.hex())
36 if __name__ == "__main__":