import serial BUTTONS = { "ok": [0x00, 0x00], "ok_hold": [0x00, 0x40], "source_r": [0x00, 0x01], "source_r_hold": [0x00, 0x81], "source_l": [0x00, 0x02], "source_l_hold": [0x00, 0x82], "volume_up": [0x00, 0x03], "volume_up_hold": [0x00, 0x43], "volume_down": [0x00, 0x04], "volume_down_hold": [0x00, 0x44], "pause": [0x00, 0x05], "wheel_up": [0x01, 0x01], "wheel_down": [0x01, 0x41], } class TunerList: def __init__(self, port): self.serial = serial.Serial(port) self.text = "" self.preset = None def send_button(self, btn): self.serial.write(BUTTONS[btn]) def run(self): while True: line = self.serial.readline()[:-2] if line[0] == 0x0F or line[0] == 0x0C: self.text = line[(8 if line[0] == 0x0F else 5):].decode("ascii") if (line[3] != 0x20): self.preset = line[3] & 0x0F else: self.preset = None elif line[0] == 0x39: self.preset = None self.text = "" for c in line: print(hex(c), " ", end="") print("[",self.text,"]", f" [P {self.preset}]") if __name__ == "__main__": TunerList("COM3").run()