From 25e04889c2bd7c1f562741176bb2eacc80cf2bf4 Mon Sep 17 00:00:00 2001 From: valoka Date: Thu, 22 Jan 2026 00:37:52 +0900 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BA=20=D0=B0=D1=81=D0=B8=D0=BD=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/OCer.py | 86 +++++++++++++++++++++++++++++++++++++---------------- main.py | 18 +++++++++-- 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/lib/OCer.py b/lib/OCer.py index 977d1a6..24e2fc3 100644 --- a/lib/OCer.py +++ b/lib/OCer.py @@ -3,8 +3,8 @@ import time import configparser import logging import socket -import asyncio import lib.lomka as lomka +import serial_asyncio class caser: #добавить по умолчанию с конфигом @@ -42,14 +42,16 @@ class caser: self.active_comport = serial.tools.list_ports.comports()[0] # объект порта (dummy) self.controller_number: bytes = 1 self.connection : serial.Serial + self.async_connection : tuple # Определение базовых констант/команд self.default_consts = {True: 17, - False: 0, - "INFO": 51, - "READ_HEADER": 128, - "MANIPULATE_HEADER": 138, - "RESPONSE_LENGHT": 5} + False: 0, + "INFO": 51, + "READ_HEADER": 128, + "DEFAULT_MESSAGE_LENGTH": 5, + "MANIPULATE_HEADER": 138, + "RESPONSE_LENGHT": 5} def find_comport(self, some_open_ports : list): '''Находит и возвращает корректный comport (объект)''' @@ -81,27 +83,36 @@ class caser: self.controller_number = hex(controller_number) - def send_command(self, cell_number : int, command: int, + async def send_command(self, cell_number : int, command: int, bytes_count=15, header=138): - active_comport = serial.Serial(self.comport, baudrate=self.baudrate, timeout=2) - self.logger.info(f"Connected to {active_comport.name }") + try: + # active_comport = serial.Serial(self.comport, baudrate=self.baudrate, timeout=2) + reader, writer = await serial_asyncio.open_serial_connection( + url=self.comport, + baudrate=self.baudrate + ) + + self.logger.info(f"Connected to {self.comport}") - # 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')] + # 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_string = b'' + for i in data: + data_string += i - checksum = self.bc_calculator.xor_bcc(data_string) + checksum = self.bc_calculator.xor_bcc(data_string) - data_string += checksum.to_bytes(1, 'little') - active_comport.write(data_string) - return active_comport.read(bytes_count) + data_string += checksum.to_bytes(1, 'little') + writer.write(data_string) + return await reader.read(bytes_count) + + except Exception as error: + self.logger.error(f"{error}") def read_controller(self, cell_number : int, command=51, header=128): active_comport = serial.Serial(self.comport, baudrate=self.baudrate, timeout=2) @@ -172,12 +183,37 @@ class caser: # return str(error) pass + # async def create_async_connection(self): + + # try: + # self.async_connection = await serial_asyncio.open_serial_connection( + # url=self.comport, + # baudrate=self.baudrate + # ) + # self.logger.info(f"Async connection to {self.comport}") + # except Exception as error: + # self.logger.error(f"Async connection error: {error}") + # pass + #хочу асинхронности - def watchdog(self): + async def watchdog(self): '''Слушает контролер в реальном времени и возвращает bytearray с событием''' try: - active_comport = serial.Serial(self.comport, baudrate=self.baudrate, timeout=2) - self.logger.info(f" |WD| Connected to (as Watchdog) {active_comport.name }") + reader, writer = await serial_asyncio.open_serial_connection( + url=self.comport, + baudrate=self.baudrate + ) + try: + while True: + data = await reader.read(self.default_consts["DEFAULT_MESSAGE_LENGTH"]) + if data: + self.logger.debug(f"Received: {data}") + return data + + except Exception as error: + self.logger.error(f"{error}") + writer.close() + await writer.wait_closed() except Exception as error: self.logger.critical(f"{error}") diff --git a/main.py b/main.py index 83bc36b..8a47527 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,8 @@ import logging import configparser import serial import socket +import asyncio +import time if __name__ == "__main__": #consts&confs @@ -29,9 +31,19 @@ if __name__ == "__main__": zxc.set_controller_number(1) zxc.set_port_device() zxc.set_controller_capacity(18) + # добавь *aargs #print(zxc.READ_ALL_CELLS()) - print(zxc.OPEN_ALL_CELLS_LEGACY(True)) + #print(zxc.OPEN_ALL_CELLS_LEGACY(True)) + # print(zxc.read_controller(1)) - # print(zxc.send_command(1, 17)) - # print(zxc.read_controller(1)) \ No newline at end of file + # print(zxc.read_controller(1)) + + async def main(zxc): + + print(await zxc.send_command(1, 17)) + exit() + + + while True: + asyncio.run(main(zxc)) \ No newline at end of file