emulate Kramer VS66
[veternica] / ser-kramer.py
1 #!/usr/bin/env python3
2
3 import serial
4
5 # Kramer VS66
6
7 def read_and_respond():
8     port = '/dev/ttyUSB1'
9     try:
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)
14         return
15
16     while True:
17         # Read 4 bytes from the serial port
18         read_bytes = ser.read(size=4)
19
20         if len(read_bytes) == 0:
21             continue
22
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"
27         else:
28             response = b"\xDE\xAD\xBE\xEF"
29
30         # Send the response back
31         ser.write(response)
32
33         print("Received:", read_bytes, read_bytes.hex())
34         print("Response:", response, response.hex())
35
36 if __name__ == "__main__":
37     read_and_respond()