Подготовка к переходу на pyserial-asyncio
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,6 @@
|
|||||||
test/
|
test/
|
||||||
|
test.py
|
||||||
|
|
||||||
# ---> Python
|
# ---> Python
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
62
lib/OCer.py
62
lib/OCer.py
@@ -3,6 +3,7 @@ import time
|
|||||||
import configparser
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
|
import asyncio
|
||||||
import lib.lomka as lomka
|
import lib.lomka as lomka
|
||||||
|
|
||||||
class caser:
|
class caser:
|
||||||
@@ -47,8 +48,8 @@ class caser:
|
|||||||
False: 0,
|
False: 0,
|
||||||
"INFO": 51,
|
"INFO": 51,
|
||||||
"READ_HEADER": 128,
|
"READ_HEADER": 128,
|
||||||
"RESPONSE_LENGHT": 5,
|
"MANIPULATE_HEADER": 138,
|
||||||
"MANIPULATE_HEADER": 138}
|
"RESPONSE_LENGHT": 5}
|
||||||
|
|
||||||
def find_comport(self, some_open_ports : list):
|
def find_comport(self, some_open_ports : list):
|
||||||
'''Находит и возвращает корректный comport (объект)'''
|
'''Находит и возвращает корректный comport (объект)'''
|
||||||
@@ -80,9 +81,9 @@ class caser:
|
|||||||
|
|
||||||
self.controller_number = hex(controller_number)
|
self.controller_number = hex(controller_number)
|
||||||
|
|
||||||
def send_command(self, cell_number : int, command : int,
|
def send_command(self, cell_number : int, command: int,
|
||||||
bytes_count=5, header=138):
|
bytes_count=15, header=138):
|
||||||
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 }")
|
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)
|
# 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)
|
return active_comport.read(bytes_count)
|
||||||
|
|
||||||
def read_controller(self, cell_number : int, command=51, header=128):
|
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 }")
|
self.logger.info(f"Connected to {active_comport.name }")
|
||||||
data = [header.to_bytes(1, 'little'),
|
data = [header.to_bytes(1, 'little'),
|
||||||
int(self.controller_number, 16).to_bytes(1, 'little'),
|
int(self.controller_number, 16).to_bytes(1, 'little'),
|
||||||
@@ -120,21 +121,37 @@ class caser:
|
|||||||
active_comport.write(data_string)
|
active_comport.write(data_string)
|
||||||
return active_comport.read(5)
|
return active_comport.read(5)
|
||||||
|
|
||||||
def READ_ALL_LOCKS(self):
|
def READ_ALL_CELLS(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):
|
|
||||||
'''и возвращает состояние всех замков'''
|
'''и возвращает состояние всех замков'''
|
||||||
|
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:
|
if confirm:
|
||||||
try:
|
try:
|
||||||
return self.send_command(0, self.default_consts[True],
|
result = []
|
||||||
self.default_consts["READ_HEADER"],
|
for i in range(1, self.controller_capacity + 1):
|
||||||
bytes_count=self.controller_capacity)
|
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:
|
except Exception as error:
|
||||||
return str(error)
|
return str(error)
|
||||||
else:
|
else:
|
||||||
@@ -155,6 +172,13 @@ class caser:
|
|||||||
# return str(error)
|
# return str(error)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
#хочу асинхронности
|
||||||
def watchdog(self):
|
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}")
|
||||||
|
|
||||||
|
|||||||
3
main.py
3
main.py
@@ -30,7 +30,8 @@ if __name__ == "__main__":
|
|||||||
zxc.set_port_device()
|
zxc.set_port_device()
|
||||||
zxc.set_controller_capacity(18)
|
zxc.set_controller_capacity(18)
|
||||||
# добавь *aargs
|
# добавь *aargs
|
||||||
zxc.READ_ALL_LOCKS()
|
#print(zxc.READ_ALL_CELLS())
|
||||||
|
print(zxc.OPEN_ALL_CELLS_LEGACY(True))
|
||||||
# print(zxc.read_controller(1))
|
# print(zxc.read_controller(1))
|
||||||
# print(zxc.send_command(1, 17))
|
# print(zxc.send_command(1, 17))
|
||||||
# print(zxc.read_controller(1))
|
# print(zxc.read_controller(1))
|
||||||
Reference in New Issue
Block a user