там чепуха какая-то на 91 стр
This commit is contained in:
47
main.py
47
main.py
@@ -48,37 +48,50 @@ class caser:
|
|||||||
self.baudrate = baudrate
|
self.baudrate = baudrate
|
||||||
|
|
||||||
self.bc_calculator = bc_calculator # xor_bcc method(returns int)
|
self.bc_calculator = bc_calculator # xor_bcc method(returns int)
|
||||||
self.comport = None # 'serial.tools.list_ports.comports' port object
|
self.comport: str # только адрес
|
||||||
self.active_comport = None # serial objectW
|
self.active_comport = serial.tools.list_ports.comports()[0] # объект порта (dummy)
|
||||||
self.controller_number = hex(0)
|
self.controller_number: bytes = 1
|
||||||
|
|
||||||
def get_comport(self, some_open_ports : list):
|
def find_comport(self, some_open_ports : list):
|
||||||
'''Находит и возвращает корректный comport (объект)'''
|
'''Находит и возвращает корректный comport (объект)'''
|
||||||
|
|
||||||
for port in some_open_ports:
|
for port in some_open_ports:
|
||||||
logger.debug(f'{port.location}, {port.description}, {port.hwid}')
|
logger.debug(f'{port.location}, {port.description}, {port.hwid}')
|
||||||
if self.device_hwid in port.hwid:
|
if self.device_hwid in port.hwid:
|
||||||
logger.info(f"Found the correct port: {port.device}")
|
logger.info(f"Found the correct port: {port.device}")
|
||||||
return port
|
return str(port.device)
|
||||||
|
|
||||||
def set_port_device(self, port_device):
|
def set_port_device(self, port_device=None):
|
||||||
'''Устанавливает comport (объект)'''
|
'''Устанавливает com port'''
|
||||||
self.comport = port_device
|
|
||||||
|
if port_device == None:
|
||||||
|
self.comport = self.find_comport(some_open_ports)
|
||||||
|
else:
|
||||||
|
self.comport = port_device
|
||||||
|
|
||||||
def set_controller_number(self, controller_number : int):
|
def set_controller_number(self, controller_number : int):
|
||||||
'''Определяет номер контроллера. Значения хранятся в виде bytes'''
|
'''Определяет номер контроллера. Значения хранятся в виде bytes'''
|
||||||
|
|
||||||
self.controller_number = hex(controller_number)
|
self.controller_number = hex(controller_number)
|
||||||
|
|
||||||
def send_command(self, header : hex, cell_number : int, command : bool):
|
def send_command(self, header : int, cell_number : int, command : int):
|
||||||
try:
|
try:
|
||||||
active_comport = serial.Serial(active_comport.device, baudrate=self.baudrate)
|
active_comport = serial.Serial(self.comport, baudrate=self.baudrate)
|
||||||
logger.info(f"Connected to {active_comport.device}")
|
logger.info(f"Connected to {active_comport.name }")
|
||||||
|
|
||||||
# 1 - header(8A), 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)
|
||||||
# *4 - (11) - open, (00) - close
|
# *4 - (11) - open, (00) - close
|
||||||
data = f"{header}{self.controller_number}{cell_number}{command}"
|
|
||||||
checksum = hex(self.bc_calculator.xor_bcc(data))
|
|
||||||
|
|
||||||
active_comport.write(str(data + checksum).encode('utf-8'))
|
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)
|
# time.sleep(0.2)
|
||||||
|
|
||||||
# добавь таймаут на чтение ответа от устройства
|
# добавь таймаут на чтение ответа от устройства
|
||||||
@@ -99,5 +112,7 @@ class caser:
|
|||||||
zxc = caser(device_hwid="PID=1A86:7523", baudrate=9600,
|
zxc = caser(device_hwid="PID=1A86:7523", baudrate=9600,
|
||||||
config=config)
|
config=config)
|
||||||
zxc.set_controller_number(1)
|
zxc.set_controller_number(1)
|
||||||
|
zxc.set_port_device()
|
||||||
# у тебя беды с хексами и типами данных
|
# у тебя беды с хексами и типами данных
|
||||||
zxc.send_command(hex(138), 1, True)
|
zxc.send_command(138, 1, 17)
|
||||||
|
exit()
|
||||||
Reference in New Issue
Block a user