diff --git a/.gitignore b/.gitignore index 36b13f1..0dbf2f2 100644 --- a/.gitignore +++ b/.gitignore @@ -168,9 +168,3 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ -# Ruff stuff: -.ruff_cache/ - -# PyPI configuration file -.pypirc - diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..5181ffb --- /dev/null +++ b/config.ini @@ -0,0 +1,4 @@ +[DEFAULT] +device_hwid = PID=1A86:7523 +baudrate = 9600 +debug = false \ No newline at end of file diff --git a/controller_documentation/tb_image_share_1764480328773.jpg.png b/controller_documentation/tb_image_share_1764480328773.jpg.png new file mode 100644 index 0000000..ae354c7 Binary files /dev/null and b/controller_documentation/tb_image_share_1764480328773.jpg.png differ diff --git a/controller_documentation/tb_image_share_1764480332719.jpg.png b/controller_documentation/tb_image_share_1764480332719.jpg.png new file mode 100644 index 0000000..2b17f48 Binary files /dev/null and b/controller_documentation/tb_image_share_1764480332719.jpg.png differ diff --git a/controller_documentation/tb_image_share_1764480337016.jpg.png b/controller_documentation/tb_image_share_1764480337016.jpg.png new file mode 100644 index 0000000..76079fe Binary files /dev/null and b/controller_documentation/tb_image_share_1764480337016.jpg.png differ diff --git a/controller_documentation/tb_image_share_1764480354964.jpg.png b/controller_documentation/tb_image_share_1764480354964.jpg.png new file mode 100644 index 0000000..c28cc87 Binary files /dev/null and b/controller_documentation/tb_image_share_1764480354964.jpg.png differ diff --git a/controller_documentation/tb_image_share_1764480364555.jpg.png b/controller_documentation/tb_image_share_1764480364555.jpg.png new file mode 100644 index 0000000..c4c75e3 Binary files /dev/null and b/controller_documentation/tb_image_share_1764480364555.jpg.png differ diff --git a/controller_documentation/tb_image_share_1764480369368.jpg.png b/controller_documentation/tb_image_share_1764480369368.jpg.png new file mode 100644 index 0000000..981943b Binary files /dev/null and b/controller_documentation/tb_image_share_1764480369368.jpg.png differ diff --git a/controller_documentation/tb_image_share_1764480507137.jpg.png b/controller_documentation/tb_image_share_1764480507137.jpg.png new file mode 100644 index 0000000..d359782 Binary files /dev/null and b/controller_documentation/tb_image_share_1764480507137.jpg.png differ diff --git a/controller_documentation/tb_image_share_1764480513025.jpg.png b/controller_documentation/tb_image_share_1764480513025.jpg.png new file mode 100644 index 0000000..35fba56 Binary files /dev/null and b/controller_documentation/tb_image_share_1764480513025.jpg.png differ diff --git a/controller_documentation/tb_image_share_1764480522483.jpg.png b/controller_documentation/tb_image_share_1764480522483.jpg.png new file mode 100644 index 0000000..f61efde Binary files /dev/null and b/controller_documentation/tb_image_share_1764480522483.jpg.png differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..7214308 --- /dev/null +++ b/main.py @@ -0,0 +1,103 @@ +import serial.tools.list_ports +import time +import configparser +import logging +import socket +import lib.lomka as lomka + +#consts&confs +config = configparser.ConfigParser() +#bc_calculator = lomka.BCCCalculator() +some_open_ports = serial.tools.list_ports.comports() + +#load config file +config.read('config.ini') +# device_hwid = config['DEFAULT']['device_hwid'] +# baudrate = config['DEFAULT']['baudrate'] + +#log section +logging.basicConfig( + level=logging.INFO if config['DEFAULT']['debug'] == "false" else logging.DEBUG, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + handlers=[ + logging.FileHandler('lastest.log'), + logging.StreamHandler()]) +logger = logging.getLogger(socket.gethostname()) + +class caser: + #добавить по умолчанию с конфигом + def __init__(self, device_hwid, config, baudrate=9600, + bc_calculator=lomka.BCCCalculator, config_name="config.ini"): + ''' + Создает объект контроллера, к которому прикрепленна информация о существующем + локере. + + device_hwid - pid USB конвертера(необязательно USB) + пример: "PID=1A86:7523" + baudrate - скорость соединения + bc_calculator - метод вычисления хеш-суммы (должен возвращать один байт в int) + ''' + + try: + config.read(config_name) + self.device_hwid = config['DEFAULT']['device_hwid'] + self.baudrate = config['DEFAULT']['baudrate'] + + except: + self.device_hwid = device_hwid + self.baudrate = baudrate + + self.bc_calculator = bc_calculator # xor_bcc method(returns int) + self.comport = None # 'serial.tools.list_ports.comports' port object + self.active_comport = None # serial objectW + self.controller_number = hex(0) + + def get_comport(self, some_open_ports : list): + '''Находит и возвращает корректный comport (объект)''' + for port in some_open_ports: + logger.debug(f'{port.location}, {port.description}, {port.hwid}') + if self.device_hwid in port.hwid: + logger.info(f"Found the correct port: {port.device}") + return port + + def set_port_device(self, port_device): + '''Устанавливает comport (объект)''' + self.comport = port_device + + def set_controller_number(self, controller_number : int): + '''Определяет номер контроллера. Значения хранятся в виде bytes''' + self.controller_number = hex(controller_number) + + def send_command(self, header : hex, cell_number : int, command : bool): + try: + active_comport = serial.Serial(active_comport.device, baudrate=self.baudrate) + logger.info(f"Connected to {active_comport.device}") + + # 1 - header(8A), 2 - controller number(01), 3 - cell number(04), 4 - command(11), 5 - checksum(9E) + # *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')) + # time.sleep(0.2) + + # добавь таймаут на чтение ответа от устройства + line = active_comport.read(10) + + if line: + print("Received:", line) + + 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, + config=config) +zxc.set_controller_number(1) +# у тебя беды с хексами и типами данных +zxc.send_command(hex(138), 1, True) \ No newline at end of file