Подготовка к переходу на pyserial-asyncio

This commit is contained in:
2026-01-18 22:33:10 +09:00
parent a6f8136b32
commit 19871d1234
3 changed files with 52 additions and 25 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,6 @@
test/
test.py
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/

View File

@@ -3,6 +3,7 @@ import time
import configparser
import logging
import socket
import asyncio
import lib.lomka as lomka
class caser:
@@ -47,8 +48,8 @@ class caser:
False: 0,
"INFO": 51,
"READ_HEADER": 128,
"RESPONSE_LENGHT": 5,
"MANIPULATE_HEADER": 138}
"MANIPULATE_HEADER": 138,
"RESPONSE_LENGHT": 5}
def find_comport(self, some_open_ports : list):
'''Находит и возвращает корректный comport (объект)'''
@@ -80,9 +81,9 @@ class caser:
self.controller_number = hex(controller_number)
def send_command(self, cell_number : int, command : int,
bytes_count=5, header=138):
active_comport = serial.Serial(self.comport, baudrate=self.baudrate)
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 }")
# 1 - header(8Ahex) (138dec), 2 - controller number(01), 3 - cell number(04), 4 - command(11), 5 - checksum(9E)
@@ -103,7 +104,7 @@ class caser:
return active_comport.read(bytes_count)
def read_controller(self, cell_number : int, command=51, header=128):
active_comport = serial.Serial(self.comport, baudrate=self.baudrate)
active_comport = serial.Serial(self.comport, baudrate=self.baudrate, timeout=2)
self.logger.info(f"Connected to {active_comport.name }")
data = [header.to_bytes(1, 'little'),
int(self.controller_number, 16).to_bytes(1, 'little'),
@@ -120,21 +121,37 @@ class caser:
active_comport.write(data_string)
return active_comport.read(5)
def READ_ALL_LOCKS(self):
# try:
return self.send_command(0, self.default_consts["INFO"],
5,
header=self.default_consts["READ_HEADER"])
# except Exception as error:
# return str(error)
def OPEN_ALL_LOCKS(self, confirm : bool):
def READ_ALL_CELLS(self):
'''и возвращает состояние всех замков'''
try:
result = []
for i in range(0, self.controller_capacity - 1):
answer = self.send_command(cell_number=i,
command=self.default_consts["INFO"],
bytes_count=self.default_consts["RESPONSE_LENGHT"],
header=self.default_consts["READ_HEADER"])
result.append({"CELL_NUMBER": i,
"CURRENT_STATUS": True if answer[-2] == 17 else False,
"FULL_ANSWER": answer})
return result
except Exception as error:
return str(error)
def OPEN_ALL_CELLS_LEGACY(self, confirm : bool):
'''Открывает все замки вручную и возвращает состояние всех замков
Confirm - подтверждение операции'''
if confirm:
try:
return self.send_command(0, self.default_consts[True],
self.default_consts["READ_HEADER"],
bytes_count=self.controller_capacity)
result = []
for i in range(1, self.controller_capacity + 1):
answer = self.send_command(cell_number=i,
command=self.default_consts[True],
bytes_count=self.default_consts["RESPONSE_LENGHT"],
header=self.default_consts["MANIPULATE_HEADER"])
result.append({"CELL_NUMBER": i,
"CURRENT_STATUS": True if answer[-2] == 17 else False,
"FULL_ANSWER": answer})
return result
except Exception as error:
return str(error)
else:
@@ -155,6 +172,13 @@ class caser:
# return str(error)
pass
#хочу асинхронности
def watchdog(self):
pass
'''Слушает контролер в реальном времени и возвращает 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 }")
except Exception as error:
self.logger.critical(f"{error}")

View File

@@ -30,7 +30,8 @@ if __name__ == "__main__":
zxc.set_port_device()
zxc.set_controller_capacity(18)
# добавь *aargs
zxc.READ_ALL_LOCKS()
#print(zxc.READ_ALL_CELLS())
print(zxc.OPEN_ALL_CELLS_LEGACY(True))
# print(zxc.read_controller(1))
# print(zxc.send_command(1, 17))
# print(zxc.read_controller(1))