From 427174fec416f976a5517af2c35178b3de95c037 Mon Sep 17 00:00:00 2001 From: valoka Date: Tue, 23 Dec 2025 00:39:03 +0900 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?/=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлена функ. чтения по ячейке --- main.py | 67 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index 8ec2096..9c68e84 100644 --- a/main.py +++ b/main.py @@ -74,45 +74,50 @@ class caser: self.controller_number = hex(controller_number) - def send_command(self, header : int, cell_number : int, command : int): - try: - active_comport = serial.Serial(self.comport, baudrate=self.baudrate) - logger.info(f"Connected to {active_comport.name }") + def send_command(self, cell_number : int, command : int, header=138): + active_comport = serial.Serial(self.comport, baudrate=self.baudrate) + logger.info(f"Connected to {active_comport.name }") - # 1 - header(8Ahex) (138dec), 2 - controller number(01), 3 - cell number(04), 4 - command(11), 5 - checksum(9E) - # *4 - (11) - open, (00) - close + # 1 - header(8Ahex) (138dec), 2 - controller number(01), 3 - cell number(04), 4 - command(11), 5 - checksum(9E) + # *4 - (11) - open, (00) - close + data = [int(header).to_bytes(1, 'little'), + int(self.controller_number, 16).to_bytes(1, 'little'), + cell_number.to_bytes(1, 'little'), + command.to_bytes(1, 'little')] + + data_string = b'' + for i in data: + data_string += i - data = [header.to_bytes(1, 'little'), - int(self.controller_number, 16).to_bytes(1, 'little'), - cell_number.to_bytes(1, 'little'), - command.to_bytes(1, 'little')] - - #проблемы тут - checksum = self.bc_calculator.xor_bcc(data) - exit() - packet = data + [checksum] - active_comport.write(b''.join(packet)) - # time.sleep(0.2) + checksum = self.bc_calculator.xor_bcc(data_string) - # добавь таймаут на чтение ответа от устройства - line = active_comport.read(10) + data_string += checksum.to_bytes(1, 'little') + active_comport.write(data_string) + return active_comport.read(5) - if line: - print("Received:", line) + def read_controller(self, cell_number : int, command=51, header=128): + active_comport = serial.Serial(self.comport, baudrate=self.baudrate) + logger.info(f"Connected to {active_comport.name }") + data = [header.to_bytes(1, 'little'), + int(self.controller_number, 16).to_bytes(1, 'little'), + cell_number.to_bytes(1, 'little'), + command.to_bytes(1, 'little')] + + data_string = b'' + for i in data: + data_string += i - except ValueError as ve: - logger.error(f"Invalid port or baudrate: {str(ve)}") + checksum = self.bc_calculator.xor_bcc(data_string) - except serial.SerialException as se: - logger.error(f"Serial port error: {str(se)}") - - except Exception as e: - logger.error(f"Unexpected error: {str(e)}") + data_string += checksum.to_bytes(1, 'little') + active_comport.write(data_string) + return active_comport.read(5) zxc = caser(device_hwid="PID=1A86:7523", baudrate=9600, config=config) zxc.set_controller_number(1) zxc.set_port_device() -# у тебя беды с хексами и типами данных -zxc.send_command(138, 1, 17) -exit() \ No newline at end of file +# добавь *aargs +print(zxc.read_controller(1)) +print(zxc.send_command(1, 17)) +print(zxc.read_controller(1)) \ No newline at end of file