From 2ff6e71f3f478c5f7820a56c44f73c406a1224a0 Mon Sep 17 00:00:00 2001 From: kyz Date: Fri, 12 Dec 2025 08:50:41 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C=E9=80=9A?= =?utf8?q?=E4=BF=A1=E8=B0=83=E8=AF=95=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../cpbs-202103-20240423/test/test.py" | 423 +++++++ .../cpbs-202103-20240423/test/test.ui" | 1083 +++++++++++++++++ 2 files changed, 1506 insertions(+) create mode 100644 "20240801\345\256\214\346\225\264\347\211\210/\346\211\263\346\211\213\344\270\213\344\275\215\346\234\272\350\275\257\344\273\266/cpbs-202103-20240423/test/test.py" create mode 100644 "20240801\345\256\214\346\225\264\347\211\210/\346\211\263\346\211\213\344\270\213\344\275\215\346\234\272\350\275\257\344\273\266/cpbs-202103-20240423/test/test.ui" diff --git "a/20240801\345\256\214\346\225\264\347\211\210/\346\211\263\346\211\213\344\270\213\344\275\215\346\234\272\350\275\257\344\273\266/cpbs-202103-20240423/test/test.py" "b/20240801\345\256\214\346\225\264\347\211\210/\346\211\263\346\211\213\344\270\213\344\275\215\346\234\272\350\275\257\344\273\266/cpbs-202103-20240423/test/test.py" new file mode 100644 index 0000000..f6978bf --- /dev/null +++ "b/20240801\345\256\214\346\225\264\347\211\210/\346\211\263\346\211\213\344\270\213\344\275\215\346\234\272\350\275\257\344\273\266/cpbs-202103-20240423/test/test.py" @@ -0,0 +1,423 @@ +import sys +import socket +import time +import threading +from PyQt5.QtWidgets import QApplication, QMainWindow +from PyQt5.QtCore import pyqtSignal +from PyQt5.QtGui import QTextCursor +from test_ui import Ui_WiFiClientApp + +class WiFiClientApp(QMainWindow, Ui_WiFiClientApp): + log_signal = pyqtSignal(str, str) # message, type: 'send'/'receive'/'info'/'error' + connect_signal = pyqtSignal(bool) + + def __init__(self): + super().__init__() + + # 初始化UI + self.setupUi(self) + self.setGeometry(100, 100, 1000, 550) # 调整窗口大小 + + # 设置日志文本框支持HTML + self.log_text.setAcceptRichText(True) + + # TCP连接状态 + self.socket = None + self.connected = False + + # 协议状态码映射 + self.status_codes = { + '0': 'OK', + '1': 'InvalidJob', + '2': 'InvalidPset', + '3': 'NoResult', + '4': 'NoGraph', + '5': 'PullResult', + '6': 'PushResult', + '7': 'CRC', + '8': 'InvalidData', + '9': 'CmdNotFounded', + '10': 'ReadError', + '11': 'WriteError', + '12': 'TimeOut' + } + + # 协议响应字段定义 + self.response_fields = { + 'ASVI': ['MID', 'VER', 'SN', 'TG', 'STATUS', 'ToolName', 'ToolType', 'FirmWare', 'Span', 'TighteingingID'], + 'ASTS': ['MID', 'VER', 'SN', 'TG', 'STATUS', 'WrenchStatus', 'WrenchEnable', 'HaveResult'], + 'ASTD': ['MID', 'VER', 'SN', 'TG', 'STATUS', 'Reserve', 'TighteningID', 'VIN', 'TimeStamp', 'BatchSize', + 'BatchCounter', 'PsetID', 'PsetName', 'TorqueMin', 'TorqueTarget', 'TorqueMax', 'AngleMin', + 'AngleTarget', 'AngleMax', 'Status', 'TorqueStatus', 'AngleStatus', 'Torque', 'Angle'], + 'ASSP': ['MID', 'VER', 'SN', 'TG', 'STATUS'], + 'ASEN': ['MID', 'VER', 'SN', 'TG', 'STATUS'], + 'ASTG': ['MID', 'VER', 'SN', 'TG', 'STATUS', 'Reserve', 'TighteningID', 'VIN', 'TimeStamp', 'BatchSize', + 'BatchCounter', 'PsetID', 'PsetName', 'TorqueMin', 'TorqueTarget', 'TorqueMax', 'AngleMin', + 'AngleTarget', 'AngleMax', 'Status', 'TorqueStatus', 'AngleStatus', 'Torque', 'Angle', + 'Mode', 'Samples', 'Frequency', 'Torque Point', 'Points'] + } + + # 连接信号 + self.log_signal.connect(self.add_log) + self.connect_signal.connect(self.update_connect_button) + + # 绑定按钮事件 + self.btn_connect.clicked.connect(self.toggle_connection) + self.btn_clear_log.clicked.connect(self.clear_log) + + # 绑定指令按钮事件 + self.btn_rsvi.clicked.connect(self.send_rsvi_command) + self.btn_rsts.clicked.connect(self.send_rsts_command) + self.btn_rstd.clicked.connect(self.send_rstd_command) + self.btn_rstg.clicked.connect(self.send_rstg_command) + self.btn_rsen_enable.clicked.connect(self.send_rsen_enable_command) + self.btn_rsen_disable.clicked.connect(self.send_rsen_disable_command) + self.btn_rssp.clicked.connect(self.send_rssp_command) + + # 设置默认值 + self.line_VER.setText("01") + self.line_SN.setText("123456") + self.line_TG.setText("01") + self.line_JobID.setText("SN1234") + self.line_JobName.setText("TEST_JOB") + self.server_ip.setText("192.168.4.1") + self.server_port.setText("8888") + + # 启动接收线程 + self.running = True + self.receive_thread = threading.Thread(target=self.receive_data) + self.receive_thread.daemon = True + self.receive_thread.start() + + def add_log(self, message, log_type='info'): + """记录日志,支持富文本显示""" + # 根据日志类型设置不同的颜色 + color_map = { + 'send': '#0000FF', # 蓝色:发送的数据 + 'receive': '#008000', # 绿色:接收的数据 + 'info': '#000000', # 黑色:普通信息 + 'error': '#FF0000' # 红色:错误信息 + } + + color = color_map.get(log_type, color_map['info']) + + # 检查是否需要解析协议数据 + if log_type == 'receive' and message.startswith('接收数据: '): + raw_data = message[len('接收数据: '):] + self.log_text.append(f'{message}') + self.parse_protocol_data(raw_data) + else: + # 普通日志消息 + self.log_text.append(f'{message}') + + self.log_text.ensureCursorVisible() + + def parse_protocol_data(self, data): + """解析协议数据并以表格形式展示""" + try: + # 分割数据 + fields = data.split(';') + if not fields: + return + + mid = fields[0] + if mid not in self.response_fields: + return + + # 获取字段名列表 + field_names = self.response_fields[mid] + + # 确保字段数量匹配 + num_fields = min(len(fields), len(field_names)) + + # 开始构建HTML表格 - 优化样式以支持长数据的多行显示 + table_html = '\n' + + # 对于长数据(RSTD和RSTG)使用分组显示 + if mid in ['ASTD', 'ASTG']: + # 分为基本信息、结果信息、参数信息等多个表格 + self.display_long_data(mid, fields, field_names, table_html) + else: + # 普通数据使用单行表格 + self.display_normal_data(fields, field_names, num_fields, table_html) + + except Exception as e: + self.log_signal.emit(f"解析协议数据失败: {e}", 'error') + + def display_normal_data(self, fields, field_names, num_fields, table_html): + """显示普通长度的数据""" + # 添加表头 + table_html += '\n' + for name in field_names[:num_fields]: + table_html += f'\n' + table_html += '\n' + + # 添加数据行 + table_html += '\n' + for i in range(num_fields): + field_value = fields[i] + + # 特殊处理STATUS字段,显示文本描述 + if field_names[i] == 'STATUS' and field_value in self.status_codes: + field_value = f"{field_value} ({self.status_codes[field_value]})" + + # 为不同类型的字段设置不同的颜色 + cell_color = '#FFFFFF' + if field_names[i] == 'STATUS': + if field_value.startswith('0'): + cell_color = '#CCFFCC' # 绿色背景表示成功 + else: + cell_color = '#FFCCCC' # 红色背景表示失败 + elif field_names[i] in ['Torque', 'Angle']: + cell_color = '#FFFFCC' # 黄色背景表示测量值 + + table_html += f'\n' + table_html += '\n' + table_html += '
{name}
{field_value}
' + + # 添加解析后的表格 + self.log_text.append(table_html) + + def display_long_data(self, mid, fields, field_names, table_html): + """显示长数据,使用多行表格布局""" + num_fields = min(len(fields), len(field_names)) + + # 对于长数据,我们将字段分组显示,每组显示在单独的行中 + # 基本信息分组 + basic_info = ['MID', 'VER', 'SN', 'TG', 'STATUS'] + + # 结果信息分组 + result_info = ['Reserve', 'TighteningID', 'VIN', 'TimeStamp'] + + # 批次信息分组 + batch_info = ['BatchSize', 'BatchCounter', 'PsetID', 'PsetName'] + + # 扭矩信息分组 + torque_info = ['TorqueMin', 'TorqueTarget', 'TorqueMax', 'Torque', 'TorqueStatus'] + + # 角度信息分组 + angle_info = ['AngleMin', 'AngleTarget', 'AngleMax', 'Angle', 'AngleStatus'] + + # 其他信息分组 + other_info = [] + + # 确定其他信息分组(针对RSTG命令) + if mid == 'ASTG': + other_info = ['Mode', 'Samples', 'Frequency', 'Torque Point', 'Points'] + + # 所有分组列表 + groups = [ + ('基本信息', basic_info), + ('结果信息', result_info), + ('批次信息', batch_info), + ('扭矩信息', torque_info), + ('角度信息', angle_info) + ] + + if other_info: + groups.append(('其他信息', other_info)) + + # 为每个分组创建一个小型表格 + for group_name, group_fields in groups: + self.log_text.append(f'

{group_name}

') + + # 创建分组表格 + group_table = '\n' + + # 找出当前分组的字段在总字段列表中的索引 + for field_name in group_fields: + if field_name in field_names: + index = field_names.index(field_name) + if index < num_fields: + field_value = fields[index] + + # 特殊处理STATUS字段 + if field_name == 'STATUS' and field_value in self.status_codes: + field_value = f"{field_value} ({self.status_codes[field_value]})" + + # 设置单元格颜色 + cell_color = '#FFFFFF' + if field_name == 'STATUS': + if field_value.startswith('0'): + cell_color = '#CCFFCC' # 绿色背景表示成功 + else: + cell_color = '#FFCCCC' # 红色背景表示失败 + elif field_name in ['Torque', 'Angle']: + cell_color = '#FFFFCC' # 黄色背景表示测量值 + + # 添加数据行 + group_table += '\n' + group_table += f'\n' + group_table += f'\n' + group_table += '\n' + + group_table += '
{field_name}{field_value}
' + self.log_text.append(group_table) + + def clear_log(self): + """清除日志""" + self.log_text.clear() + + def toggle_connection(self): + """切换连接状态""" + if not self.connected: + # 连接服务器 + try: + ip = self.server_ip.text() + port = int(self.server_port.text()) + self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.socket.connect((ip, port)) + self.connected = True + self.connect_signal.emit(True) + self.log_signal.emit(f"已连接到服务器: {ip}:{port}", 'info') + except Exception as e: + self.log_signal.emit(f"连接失败: {e}", 'error') + else: + # 断开连接 + try: + self.socket.close() + self.connected = False + self.connect_signal.emit(False) + self.log_signal.emit("已断开与服务器的连接", 'info') + except Exception as e: + self.log_signal.emit(f"断开连接失败: {e}", 'error') + + def update_connect_button(self, connected): + """更新连接按钮状态""" + if connected: + self.btn_connect.setText("断开连接") + self.btn_connect.setStyleSheet("background-color: #FF6B6B; color: white;") + else: + self.btn_connect.setText("连接服务器") + self.btn_connect.setStyleSheet("background-color: #4CAF50; color: white;") + + def send_command(self, mid, ver, sn, tg, params): + """发送指令""" + if not self.connected: + self.log_signal.emit("请先连接服务器", 'info') + return + + # 构建指令 + cmd_parts = [mid, ver, sn, tg] + params + cmd = ";".join(cmd_parts) + "\r\n" + + try: + self.socket.sendall(cmd.encode()) + self.log_signal.emit(f"发送指令: {cmd.strip()}", 'send') + except Exception as e: + self.log_signal.emit(f"发送指令失败: {e}", 'error') + self.connected = False + self.connect_signal.emit(False) + + def receive_data(self): + """接收数据线程""" + while self.running: + if self.connected: + try: + data = self.socket.recv(1024) + if data: + self.log_signal.emit(f"接收数据: {data.decode().strip()}", 'receive') + except Exception as e: + if self.connected: + self.log_signal.emit(f"接收数据失败: {e}", 'error') + self.connected = False + self.connect_signal.emit(False) + else: + time.sleep(0.5) + + def send_rsvi_command(self): + """发送RSVI指令""" + mid = "RSVI" + ver = self.line_VER.text() + sn = self.line_SN.text() + tg = self.line_TG.text() + params = [] + self.send_command(mid, ver, sn, tg, params) + + def send_rsts_command(self): + """发送RSTS指令""" + mid = "RSTS" + ver = self.line_VER.text() + sn = self.line_SN.text() + tg = self.line_TG.text() + params = [] + self.send_command(mid, ver, sn, tg, params) + + def send_rstd_command(self): + """发送RSTD指令""" + mid = "RSTD" + ver = self.line_VER.text() + sn = self.line_SN.text() + tg = self.line_TG.text() + params = [] + self.send_command(mid, ver, sn, tg, params) + + def send_rstg_command(self): + """发送RSTG指令""" + mid = "RSTG" + ver = self.line_VER.text() + sn = self.line_SN.text() + tg = self.line_TG.text() + params = [] + self.send_command(mid, ver, sn, tg, params) + + def send_rsen_enable_command(self): + """发送RSEN Enable指令""" + mid = "RSEN" + ver = self.line_VER.text() + sn = self.line_SN.text() + tg = self.line_TG.text() + params = [self.line_JobID.text(), "00", "1"] + self.send_command(mid, ver, sn, tg, params) + + def send_rsen_disable_command(self): + """发送RSEN Disable指令""" + mid = "RSEN" + ver = self.line_VER.text() + sn = self.line_SN.text() + tg = self.line_TG.text() + params = [self.line_JobID.text(), "00", "0"] + self.send_command(mid, ver, sn, tg, params) + + def send_rssp_command(self): + """发送RSSP指令""" + mid = "RSSP" + ver = self.line_VER.text() + sn = self.line_SN.text() + tg = self.line_TG.text() + server = self.line_JobID.text() + job = self.line_JobName.text() + + # 获取参数值 + strategy = str(self.combo_Strategy.currentIndex()) + rotation = str(self.combo_Rotation.currentIndex()) + cycle_end_time = str(self.combo_CycleEndTime.currentIndex()) + repeat = str(self.combo_Repeat.currentIndex()) + + param1 = self.line_StartTorque.text() + param2 = self.line_TorqueMin.text() + param3 = self.line_TorqueTarget.text() + param4 = self.line_TorqueMax.text() + param5 = self.line_StartAngle.text() + param6 = self.line_AngleMin.text() + param7 = self.line_AngleTarget.text() + param8 = self.line_AngleMax.text() + param9 = self.line_BatchSize.text() + param10 = self.line_Repair.text() + + params = [server, job, strategy, rotation, cycle_end_time, repeat, + param1, param2, param3, param4, param5, param6, param7, param8, param9, param10] + self.send_command(mid, ver, sn, tg, params) + + def closeEvent(self, event): + """窗口关闭事件""" + self.running = False + if self.connected: + self.socket.close() + event.accept() + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = WiFiClientApp() + window.show() + sys.exit(app.exec_()) diff --git "a/20240801\345\256\214\346\225\264\347\211\210/\346\211\263\346\211\213\344\270\213\344\275\215\346\234\272\350\275\257\344\273\266/cpbs-202103-20240423/test/test.ui" "b/20240801\345\256\214\346\225\264\347\211\210/\346\211\263\346\211\213\344\270\213\344\275\215\346\234\272\350\275\257\344\273\266/cpbs-202103-20240423/test/test.ui" new file mode 100644 index 0000000..e00cfe6 --- /dev/null +++ "b/20240801\345\256\214\346\225\264\347\211\210/\346\211\263\346\211\213\344\270\213\344\275\215\346\234\272\350\275\257\344\273\266/cpbs-202103-20240423/test/test.ui" @@ -0,0 +1,1083 @@ + + + WiFiClientApp + + + + 0 + 0 + 1007 + 683 + + + + WiFi扳手下位机客户端 + + + + + + 410 + 20 + 591 + 651 + + + + + + + + + 10 + 0 + 571 + 641 + + + + true + + + + + + + 10 + 10 + 301 + 91 + + + + 服务器配置 + + + + + 220 + 20 + 71 + 61 + + + + 连接 + + + + + + 50 + 50 + 30 + 21 + + + + 端口: + + + + + + 130 + 50 + 60 + 20 + + + + + 60 + 0 + + + + 8888 + + + + + + 50 + 20 + 42 + 21 + + + + IP地址: + + + + + + 110 + 20 + 81 + 20 + + + + + 0 + 0 + + + + 192.168.4.1 + + + + + + + 320 + 10 + 91 + 441 + + + + 发送命令 + + + + + 10 + 20 + 61 + 51 + + + + 读信息 + + + + + + 10 + 80 + 61 + 51 + + + + 读状态 + + + + + + 10 + 140 + 61 + 51 + + + + 读数据 + + + + + + 10 + 200 + 61 + 51 + + + + 读曲线 + + + + + + 10 + 260 + 61 + 51 + + + + 启用 + + + + + + 10 + 320 + 61 + 51 + + + + 禁用 + + + + + + 10 + 380 + 61 + 51 + + + + 选JOB + + + + + + + 10 + 110 + 301 + 361 + + + + true + + + + + 0 + 0 + 299 + 359 + + + + + + 10 + 10 + 281 + 41 + + + + + + + + + 130 + 10 + 30 + 21 + + + + 版本 + + + + + + 160 + 10 + 31 + 20 + + + + + 0 + 0 + + + + 1 + + + + + + 11 + 13 + 21 + 16 + + + + SN + + + + + + 40 + 10 + 81 + 20 + + + + + 0 + 0 + + + + SN1234 + + + + + + 240 + 10 + 31 + 20 + + + + + 0 + 0 + + + + 1 + + + + + + 210 + 10 + 30 + 21 + + + + 组别 + + + + + + + 10 + 60 + 281 + 291 + + + + JOB + + + + + 10 + 20 + 42 + 16 + + + + Job编号 + + + + + + 60 + 20 + 71 + 20 + + + + + 0 + 0 + + + + 123456 + + + + + + 70 + 110 + 60 + 20 + + + + + 60 + 0 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + + + 10 + 80 + 51 + 16 + + + + + 0 + 0 + + + + + 0 + 0 + + + + 拧紧策略 + + + + + + 20 + 106 + 31 + 20 + + + + + 0 + 0 + + + + 方向 + + + + + + 70 + 80 + 60 + 20 + + + + + 60 + 0 + + + + + 0 + + + + + 1 + + + + + 2 + + + + + + + 60 + 50 + 71 + 20 + + + + + 0 + 0 + + + + TEST_JOB + + + + + + 10 + 50 + 42 + 16 + + + + Job名字 + + + + + + 70 + 140 + 61 + 20 + + + + + 0 + 0 + + + + 100 + + + + + + 10 + 140 + 51 + 16 + + + + 起始扭矩 + + + + + + 70 + 170 + 61 + 20 + + + + + 0 + 0 + + + + 200 + + + + + + 10 + 170 + 51 + 16 + + + + 扭矩下限 + + + + + + 70 + 200 + 61 + 20 + + + + + 0 + 0 + + + + 300 + + + + + + 10 + 200 + 51 + 16 + + + + 目标扭矩 + + + + + + 70 + 230 + 61 + 20 + + + + + 0 + 0 + + + + 400 + + + + + + 10 + 230 + 51 + 16 + + + + 扭矩上限 + + + + + + 150 + 110 + 51 + 16 + + + + 角度上限 + + + + + + 210 + 20 + 61 + 20 + + + + + 0 + 0 + + + + 20 + + + + + + 210 + 110 + 61 + 20 + + + + + 0 + 0 + + + + 100 + + + + + + 150 + 80 + 51 + 16 + + + + 目标角度 + + + + + + 150 + 20 + 51 + 16 + + + + 起始角度 + + + + + + 150 + 50 + 51 + 16 + + + + 角度下限 + + + + + + 210 + 80 + 61 + 20 + + + + + 0 + 0 + + + + 90 + + + + + + 210 + 50 + 61 + 20 + + + + + 0 + 0 + + + + 0 + + + + + + 210 + 140 + 61 + 20 + + + + + 0 + 0 + + + + 1 + + + + + + 150 + 140 + 51 + 16 + + + + 次数 + + + + + + 150 + 170 + 51 + 16 + + + + 等待时间 + + + + + + 150 + 200 + 51 + 16 + + + + 补偿长度 + + + + + + 210 + 200 + 61 + 20 + + + + + 0 + 0 + + + + 1 + + + + + + 210 + 230 + 61 + 20 + + + + + 0 + 0 + + + + 1 + + + + + + 150 + 230 + 51 + 16 + + + + 计数模式 + + + + + + 150 + 260 + 51 + 16 + + + + + 0 + 0 + + + + + 0 + 0 + + + + 模式 + + + + + + 210 + 260 + 60 + 20 + + + + + 60 + 0 + + + + + 0 - Lock + + + + + 1 - Repeat + + + + + + + 10 + 260 + 51 + 16 + + + + 返修次数 + + + + + + 70 + 260 + 61 + 20 + + + + + 0 + 0 + + + + 0 + + + + + + 210 + 170 + 60 + 20 + + + + + 60 + 0 + + + + + 0 - 0.1 s + + + + + 1 - 0.2 s + + + + + 2 - 0.5 s + + + + + 3 - 1 s + + + + + 4 - 2 s + + + + + 5 - 5 s + + + + + 6 - 10 s + + + + + 7 - 20 s + + + + + 8 - 30 s + + + + + 9 - 60 s + + + + + + + + + + 330 + 630 + 61 + 41 + + + + 清除日志 + + + + + + + -- 2.34.1