Загрузить файлы в «/»

Добавлена функ. чтения по ячейке
This commit is contained in:
2025-12-23 00:39:03 +09:00
parent 794e5b3cac
commit 427174fec4

53
main.py
View File

@@ -74,45 +74,50 @@ class caser:
self.controller_number = hex(controller_number) self.controller_number = hex(controller_number)
def send_command(self, header : int, cell_number : int, command : int): def send_command(self, cell_number : int, command : int, header=138):
try:
active_comport = serial.Serial(self.comport, baudrate=self.baudrate) active_comport = serial.Serial(self.comport, baudrate=self.baudrate)
logger.info(f"Connected to {active_comport.name }") 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)
# *4 - (11) - open, (00) - close # *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
checksum = self.bc_calculator.xor_bcc(data_string)
data_string += checksum.to_bytes(1, 'little')
active_comport.write(data_string)
return active_comport.read(5)
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'), data = [header.to_bytes(1, 'little'),
int(self.controller_number, 16).to_bytes(1, 'little'), int(self.controller_number, 16).to_bytes(1, 'little'),
cell_number.to_bytes(1, 'little'), cell_number.to_bytes(1, 'little'),
command.to_bytes(1, 'little')] command.to_bytes(1, 'little')]
#проблемы тут data_string = b''
checksum = self.bc_calculator.xor_bcc(data) for i in data:
exit() data_string += i
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)
if line: data_string += checksum.to_bytes(1, 'little')
print("Received:", line) active_comport.write(data_string)
return active_comport.read(5)
except ValueError as ve:
logger.error(f"Invalid port or baudrate: {str(ve)}")
except serial.SerialException as se:
logger.error(f"Serial port error: {str(se)}")
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
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.set_port_device()
# у тебя беды с хексами и типами данных # добавь *aargs
zxc.send_command(138, 1, 17) print(zxc.read_controller(1))
exit() print(zxc.send_command(1, 17))
print(zxc.read_controller(1))