From 345de6a942ce0b8f469ace58e95b689378adc2d4 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Tue, 8 Apr 2025 16:47:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BA=E4=BB=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mainwindow.py | 198 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 133 insertions(+), 65 deletions(-) diff --git a/mainwindow.py b/mainwindow.py index 6fb1b88..6df91a8 100644 --- a/mainwindow.py +++ b/mainwindow.py @@ -1,14 +1,16 @@ import sys import socket +import os import numpy as np from scipy.fft import fft, fftfreq import matplotlib.pyplot as plt from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas, NavigationToolbar2QT -from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QFrame, QGridLayout, QStatusBar, QSizePolicy +from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QFrame, QGridLayout, QStatusBar, QSizePolicy, QFileDialog from PyQt5.QtCore import Qt import matplotlib import struct import time +from typing import Tuple, Optional # 启用高DPI支持 QApplication.setAttribute(Qt.AA_EnableHighDpiScaling) QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps) @@ -19,6 +21,8 @@ matplotlib.use('Qt5Agg') plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows 中文字体 plt.rcParams['axes.unicode_minus'] = False # 解决负号 '-' 显示问题 +HEADER_MAGIC = bytes([0xAA, 0x55, 0xAA]) +HEADER_SIZE = 6 # PackgeHead除去data字段的大小(3+1+1+1) class MatplotlibCanvas(FigureCanvas): """ 用于在 Qt 界面中嵌入 Matplotlib 绘图 """ @@ -81,7 +85,9 @@ class SocketClientApp(QMainWindow): self.port_input = QLineEdit(self) self.connect_button = QPushButton("连接") self.get_data_button = QPushButton("获取数据") + self.upgrade_button = QPushButton("更新固件") self.get_data_button.setEnabled(False) # 初始状态不可点击 + self.upgrade_button.setEnabled(False) # 初始状态不可点击 self.ip_input.setText("192.168.0.200") self.port_input.setText("12345") @@ -114,6 +120,7 @@ class SocketClientApp(QMainWindow): button_layout = QHBoxLayout() button_layout.addWidget(self.connect_button) button_layout.addWidget(self.get_data_button) + button_layout.addWidget(self.upgrade_button) main_layout.addLayout(button_layout) # 添加分割线 @@ -153,10 +160,17 @@ class SocketClientApp(QMainWindow): # 事件绑定 self.connect_button.clicked.connect(self.connect_to_server) self.get_data_button.clicked.connect(self.on_button_clicked) + self.upgrade_button.clicked.connect(self.on_button_upgrade) # 设置布局策略,确保控件大小随窗口调整 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + def calculate_crc(self,data: bytes) -> int: + """计算数据的累加和CRC""" + crc = 0 + for byte in data: + crc += byte + return crc & 0xFF # 只保留最低字节 def connect_to_server(self): """ 连接到服务器 """ ip = self.ip_input.text() @@ -164,6 +178,7 @@ class SocketClientApp(QMainWindow): if not ip or not port: self.status_bar.showMessage("状态: 请输入有效的 IP 和端口号") + return try: @@ -175,86 +190,92 @@ class SocketClientApp(QMainWindow): self.socket.connect((ip, port)) self.get_data_button.setEnabled(True) # 连接成功,启用获取数据按钮 + self.upgrade_button.setEnabled(True) self.status_bar.showMessage(f"状态: 连接成功! 服务器 {ip}:{port}") except Exception as e: self.status_bar.showMessage(f"状态: 连接失败 - {str(e)}") - def receive_data(self): + def parse_package_head(self,data: bytes) -> Tuple[Optional[dict], Optional[bytes]]: + """ + 解析包头部 + 返回: (header_dict, remaining_data) + """ + if len(data) < HEADER_SIZE: + return None, None + + # 检查魔数 + if data[:3] != HEADER_MAGIC: + print(f"Invalid header magic: {data[:3].hex()}") + return None, None + + # 解析头部 + cmd, version, result_code = struct.unpack_from('