show_hex.cpp

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> #include <iostream.h>

void main(void)
 {
   cout.setf(ios::hex);
   cout.setf(ios::showbase);
   cout << 100;
 }

 

帮我用qtc++做一个跟这个效果差不多的import sys import serial import serial.tools.list_ports from PyQt5.QtWidgets import ( QApplication, QWidget, QLabel, QLineEdit, QPushButton, QTextEdit, QVBoxLayout, QHBoxLayout, QComboBox, QCheckBox, QGroupBox, QFormLayout, QGridLayout, QSpacerItem, QSizePolicy ) from PyQt5.QtCore import QTimer, QThread, pyqtSignal, Qt from PyQt5.QtGui import QFont def crc16_modbus(data: bytes) -> int: crc = 0xFFFF for byte in data: crc ^= byte for _ in range(8): if crc & 0x0001: crc >>= 1 crc ^= 0xA001 # 异或反向多项式 else: crc >>= 1 return crc class SerialReader(QThread): data_received = pyqtSignal(bytes) def __init__(self, port, baudrate): super().__init__() self.port = port self.baudrate = baudrate self.serial_port = None self.running = True def run(self): try: self.serial_port = serial.Serial(self.port, self.baudrate, timeout=1) while self.running: data = self.serial_port.read(256) if data: self.data_received.emit(data) except Exception as e: print("串口读取错误:", e) def stop(self): self.running = False if self.serial_port and self.serial_port.is_open: self.serial_port.close() self.wait() class SerialDataDisplay(QWidget): def __init__(self): super().__init__() self.setWindowTitle("串口数据显示界面") self.resize(1500, 1000) self.serial_reader = None self.buffer = bytearray() self.max_lines = 100 self.band_data_cache = { "band1": {}, "band2": {}, "band3": {}, "band4": {} } self.loop_timer = QTimer() self.loop_timer.timeout.connect(self.do_send_data) # 新增:保存上一次发送的预设帧 self.last_preset_data = None self.last_combined_frame = None # 保存最后一次发送的组合帧 # 新增:缓存原始数据用于显示 self.raw_data_lines = [] self.init_ui() self.refresh_ports() # 设置定时器刷新界面 self.update_timer = QTimer() self.update_timer.timeout.connect(self.flush_display) self.update_timer.start(100) def init_ui(self): main_layout = QVBoxLayout() # 版本查询按钮组 version_group = QGroupBox("版本查询") version_group.setFont(QFont("Arial", 12)) version_layout = QHBoxLayout() self.version_button = QPushButton("查询版本") self.version_button.setFont(QFont("Arial", 12)) self.version_button.clicked.connect(self.send_version_query) version_layout.addWidget(self.version_button) version_group.setLayout(version_layout) main_layout.addWidget(version_group) # 串口设置区域 port_config_layout = QHBoxLayout() self.port_combo = QComboBox() self.port_combo.setEditable(True) self.port_combo.setFont(QFont("Arial", 12)) self.port_combo.setFixedWidth(200) self.baud_combo = QComboBox() self.baud_combo.addItems(["9600", "19200", "38400", "57600", "115200"]) self.baud_combo.setCurrentText("115200") self.baud_combo.setFont(QFont("Arial", 12)) self.baud_combo.setFixedWidth(100) self.refresh_button = QPushButton("刷新串口") self.refresh_button.setFont(QFont("Arial", 12)) self.connect_button = QPushButton("连接串口") self.connect_button.setFont(QFont("Arial", 12)) self.status_label = QLabel("未连接") self.status_label.setFont(QFont("Arial", 12)) self.status_label.setStyleSheet("color: red;") port_config_layout.addWidget(QLabel("串口号:")) port_config_layout.addWidget(self.port_combo) port_config_layout.addWidget(QLabel("波特率:")) port_config_layout.addWidget(self.baud_combo) port_config_layout.addWidget(self.refresh_button) port_config_layout.addWidget(self.connect_button) port_config_layout.addWidget(self.status_label) port_config_layout.addSpacerItem(QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) main_layout.addLayout(port_config_layout) # 频段信息显示区域 band_layout = QGridLayout() self.band_widgets = {} for i in range(4): group = QGroupBox(f"频段 {i + 1}") group.setFont(QFont("Arial", 12)) form = QFormLayout() form.setLabelAlignment(Qt.AlignLeft) enabled = QCheckBox() enabled.setEnabled(False) temp = QLineEdit(" ") temp.setReadOnly(True) temp.setFont(QFont("Arial", 11)) current = QLineEdit(" ") current.setReadOnly(True) current.setFont(QFont("Arial", 11)) voltage = QLineEdit(" ") voltage.setReadOnly(True) voltage.setFont(QFont("Arial", 11)) form.addRow("开启状态:", enabled) form.addRow("温度:", temp) form.addRow("电流:", current) form.addRow("电压:", voltage) self.band_widgets[f"band{i + 1}"] = { "enabled": enabled, "temp": temp, "current": current, "voltage": voltage } group.setLayout(form) band_layout.addWidget(group, i // 2, i % 2) main_layout.addLayout(band_layout) # 发送数据帧输入框 send_layout = QHBoxLayout() self.send_input = QLineEdit() self.send_input.setFont(QFont("Arial", 12)) self.send_input.setPlaceholderText("请输入Hex数据,如:5A 5A 5A 5A 01 02 03") self.send_button = QPushButton("发送") self.send_button.setFont(QFont("Arial", 12)) self.send_status = QLabel("") self.send_status.setFont(QFont("Arial", 11)) self.send_status.setStyleSheet("color: blue;") self.loop_checkbox = QCheckBox("循环发送") self.loop_checkbox.setFont(QFont("Arial", 12)) self.interval_input = QLineEdit("1000") # 默认间隔1000ms self.interval_input.setFixedWidth(80) self.interval_input.setFont(QFont("Arial", 12)) self.interval_input.setToolTip("发送间隔(毫秒)") send_layout.addWidget(QLabel("发送数据帧 (Hex):")) send_layout.addWidget(self.send_input) send_layout.addWidget(self.send_button) send_layout.addWidget(self.send_status) send_layout.addWidget(self.loop_checkbox) send_layout.addWidget(QLabel("间隔(ms):")) send_layout.addWidget(self.interval_input) main_layout.addLayout(send_layout) # 频段组合发送组 preset_group = QGroupBox("频段组合发送") preset_group.setFont(QFont("Arial", 12)) preset_layout = QGridLayout() self.band_checkboxes = [] # 每个频段的位掩码 self.band_masks = { "频段1": 0x01, "频段2": 0x02, "频段3": 0x04, "频段4": 0x08, } # 原始帧模板(去掉CRC部分) self.preset_frame_template = bytes.fromhex( "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 02 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", ) row = 0 for i, (name, mask) in enumerate(self.band_masks.items()): checkbox = QCheckBox(name) checkbox.setFont(QFont("Arial", 11)) checkbox.stateChanged.connect(self.update_combined_frame) self.band_checkboxes.append(checkbox) preset_layout.addWidget(checkbox, row // 2, row % 2) row += 1 self.send_combined_button = QPushButton("发送组合帧") self.send_combined_button.setFont(QFont("Arial", 12)) self.send_combined_button.clicked.connect(self.send_combined_frame) preset_layout.addWidget(self.send_combined_button, row // 2 + 1, 0, 1, 2) preset_group.setLayout(preset_layout) main_layout.addWidget(preset_group) # 自定义帧按钮组 custom_frame_group = QGroupBox("自定义帧") custom_frame_group.setFont(QFont("Arial", 12)) custom_frame_layout = QHBoxLayout() self.custom_frame_button = QPushButton("持续时间") self.custom_frame_button.setFont(QFont("Arial", 12)) self.custom_frame_button.clicked.connect(self.send_custom_frame) self.duration_input = QLineEdit("0") self.duration_input.setFixedWidth(80) self.duration_input.setToolTip("输入0~65535的持续时间") custom_frame_layout.addWidget(self.custom_frame_button) custom_frame_layout.addWidget(QLabel("持续时间 (0~65535):")) custom_frame_layout.addWidget(self.duration_input) custom_frame_layout.addSpacerItem(QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) custom_frame_group.setLayout(custom_frame_layout) main_layout.addWidget(custom_frame_group) # 干扰码切换模块 jammer_group = QGroupBox("干扰码切换") jammer_group.setFont(QFont("Arial", 12)) jammer_layout = QGridLayout() # 按钮列表 self.jammer_buttons = { "900M-1": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "900M-2": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 0A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "1600M-1": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 0B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "1600M-2": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "2400M-1": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 0D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "2400M-2": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 0F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "5800M-1": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "5800M-2": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", } row = 0 col = 0 for name, hex_data in self.jammer_buttons.items(): btn = QPushButton(name) btn.setFont(QFont("Arial", 11)) btn.clicked.connect(lambda checked, data=hex_data: self.send_jammer_frame(data)) jammer_layout.addWidget(btn, row, col) col += 1 if col > 2: col = 0 row += 1 jammer_group.setLayout(jammer_layout) main_layout.addWidget(jammer_group) # 新增:功率控制模块 power_group = QGroupBox("功率控制") power_group.setFont(QFont("Arial", 12)) power_layout = QHBoxLayout() # 功率帧模板(A1 12 / A1 13 / A1 15) self.power_frames = { "低功率": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 12 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "中功率": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", "高功率": "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 15 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" } for name, frame_hex in self.power_frames.items(): btn = QPushButton(name) btn.setFont(QFont("Arial", 11)) btn.clicked.connect(lambda checked, data=frame_hex: self.send_power_frame(data)) power_layout.addWidget(btn) power_group.setLayout(power_layout) main_layout.addWidget(power_group) # 原始数据帧显示框 raw_data_group = QGroupBox("原始数据帧显示") raw_data_group.setFont(QFont("Arial", 12)) self.raw_data_display = QTextEdit() self.raw_data_display.setReadOnly(True) self.raw_data_display.setFont(QFont("Courier", 10)) layout = QVBoxLayout() layout.addWidget(self.raw_data_display) raw_data_group.setLayout(layout) main_layout.addWidget(raw_data_group) # 信号连接 self.refresh_button.clicked.connect(self.refresh_ports) self.connect_button.clicked.connect(self.toggle_serial_connection) self.send_button.clicked.connect(self.handle_send_button_click) self.send_input.returnPressed.connect(self.handle_send_button_click) self.setLayout(main_layout) self.update_timer = QTimer() self.update_timer.timeout.connect(self.flush_display) self.update_timer.start(100) #新增方法:发送功率帧 def send_power_frame(self, hex_data): try: data_bytes = bytes.fromhex(hex_data.replace(' ', '')) except ValueError: self.send_status.setText("功率帧数据格式错误") self.send_status.setStyleSheet("color: red;") return try: # 取第12字节之后的数据计算 CRC crc_data = data_bytes[12:] crc_value = crc16_modbus(crc_data) # 转为可变列表插入 CRC data_list = list(data_bytes) data_list[8] = (crc_value >> 8) & 0xFF # CRC 高位 → 第8字节 data_list[9] = crc_value & 0xFF # CRC 低位 → 第9字节 final_bytes = bytes(data_list) # 格式化为带空格的 Hex 字符串 hex_with_crc = ''.join(f"{b:02X}" for b in final_bytes) formatted_hex = ' '.join(hex_with_crc[i:i+2] for i in range(0, len(hex_with_crc), 2)) self.send_input.setText(formatted_hex) # 发送 if self.serial_reader and self.serial_reader.serial_port and self.serial_reader.serial_port.is_open: self.serial_reader.serial_port.write(final_bytes) cmd_code = hex_data.split()[13] self.send_status.setText(f"功率设置({cmd_code})发送成功") self.send_status.setStyleSheet("color: green;") else: self.send_status.setText("串口未连接") self.send_status.setStyleSheet("color: red;") except Exception as e: self.send_status.setText(f"功率帧发送失败: {str(e)}") self.send_status.setStyleSheet("color: red;") def send_jammer_frame(self, hex_data): try: data_bytes = bytes.fromhex(hex_data.replace(' ', '')) except ValueError: self.send_status.setText("干扰码数据格式错误") self.send_status.setStyleSheet("color: red;") return try: crc_data = data_bytes[12:] crc_value = crc16_modbus(crc_data) data_list = list(data_bytes) data_list[8] = (crc_value >> 8) & 0xFF # 高位 data_list[9] = crc_value & 0xFF # 低位 hex_with_crc = ''.join(f"{b:02X}" for b in data_list) formatted_hex = ' '.join(hex_with_crc[i:i+2] for i in range(0, len(hex_with_crc), 2)) self.send_input.setText(formatted_hex) except Exception as e: self.send_status.setText("CRC计算失败:" + str(e)) self.send_status.setStyleSheet("color: red;") return if self.serial_reader and self.serial_reader.serial_port and self.serial_reader.serial_port.is_open: try: self.serial_reader.serial_port.write(data_bytes) #self.send_status.setText(f"干扰码 {hex_data} 发送成功") self.send_status.setStyleSheet("color: green;") except Exception as e: self.send_status.setText("发送失败:" + str(e)) self.send_status.setStyleSheet("color: red;") else: self.send_status.setText("串口未连接") self.send_status.setStyleSheet("color: red;") def handle_send_button_click(self): if self.loop_checkbox.isChecked(): try: interval = int(self.interval_input.text()) if interval < 100: self.send_status.setText("间隔太小(至少100ms)") self.send_status.setStyleSheet("color: red;") return self.loop_timer.start(interval) self.send_status.setText("循环发送中...") self.send_status.setStyleSheet("color: green;") except ValueError: self.send_status.setText("请输入有效的数字作为间隔") self.send_status.setStyleSheet("color: red;") else: self.loop_timer.stop() self.send_hex_data() def send_custom_frame(self): if not hasattr(self, 'last_combined_frame') or self.last_combined_frame is None: self.send_status.setText("请先发送一次组合帧") self.send_status.setStyleSheet("color: red;") return # 获取用户输入的持续时间 duration_str = self.duration_input.text().strip() try: duration = int(duration_str) if not (0 <= duration <= 65535): raise ValueError("范围必须在 0 ~ 65535 之间") except ValueError as e: self.send_status.setText(f"输入错误:{e}") self.send_status.setStyleSheet("color: red;") return # 将整数转换为两个字节(高位在前) byte16 = (duration >> 8) & 0xFF # 高位字节 byte17 = duration & 0xFF # 低位字节 # 拷贝上一次的组合帧 data_bytes = bytearray(self.last_combined_frame) # 插入到帧的第16和17个字节位置(索引16、17) data_bytes[16] = byte16 data_bytes[17] = byte17 try: # 计算 CRC(从第12字节开始) crc_data = bytes(data_bytes[12:]) crc_value = crc16_modbus(crc_data) # 插入 CRC 到第8、9字节 data_bytes[8] = (crc_value >> 8) & 0xFF data_bytes[9] = crc_value & 0xFF hex_with_crc = ''.join(f"{b:02X}" for b in data_bytes) self.send_input.setText(' '.join(hex_with_crc[i:i+2] for i in range(0, len(hex_with_crc), 2))) except Exception as e: self.send_status.setText("CRC计算失败:" + str(e)) self.send_status.setStyleSheet("color: red;") return if self.serial_reader and self.serial_reader.serial_port and self.serial_reader.serial_port.is_open: self.send_hex_data() else: self.send_status.setText("请先连接串口") self.send_status.setStyleSheet("color: red;") def send_version_query(self): hex_data = "5A 5A 5A 5A 43 00 42 4E 7A 0E 4C 47 A1 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" try: data_bytes = bytes.fromhex(hex_data.replace(' ', '')) except ValueError: self.send_status.setText("版本查询数据格式错误") self.send_status.setStyleSheet("color: red;") return try: crc_data = data_bytes[12:] crc_value = crc16_modbus(crc_data) data_list = list(data_bytes) data_list[8] = (crc_value >> 8) & 0xFF # 高位 data_list[9] = crc_value & 0xFF # 低位 hex_with_crc = ''.join(f"{b:02X}" for b in data_list) self.send_input.setText(' '.join(hex_with_crc[i:i+2] for i in range(0, len(hex_with_crc), 2))) except Exception as e: self.send_status.setText("CRC计算失败:" + str(e)) self.send_status.setStyleSheet("color: red;") return if self.serial_reader and self.serial_reader.serial_port and self.serial_reader.serial_port.is_open: self.send_hex_data() else: self.send_status.setText("请先连接串口") self.send_status.setStyleSheet("color: red;") def do_send_data(self): hex_str = self.send_input.text().strip() if not hex_str: return try: hex_bytes = bytes.fromhex(hex_str.replace(' ', '')) if self.serial_reader and self.serial_reader.serial_port and self.serial_reader.serial_port.is_open: self.serial_reader.serial_port.write(hex_bytes) else: self.loop_timer.stop() self.send_status.setText("串口已断开,停止循环发送") self.send_status.setStyleSheet("color: red;") except Exception as e: self.loop_timer.stop() self.send_status.setText("发送失败:" + str(e)) self.send_status.setStyleSheet("color: red;") def send_hex_data(self): hex_str = self.send_input.text().strip() if not hex_str: self.send_status.setText("请输入有效的Hex数据") self.send_status.setStyleSheet("color: red;") return try: hex_bytes = bytes.fromhex(hex_str.replace(' ', '')) if self.serial_reader and self.serial_reader.serial_port and self.serial_reader.serial_port.is_open: self.serial_reader.serial_port.write(hex_bytes) self.send_status.setText("发送成功") self.send_status.setStyleSheet("color: green;") else: self.send_status.setText("串口未连接") self.send_status.setStyleSheet("color: red;") except Exception as e: self.send_status.setText("发送失败:" + str(e)) self.send_status.setStyleSheet("color: red;") def refresh_ports(self): current_text = self.port_combo.currentText() self.port_combo.clear() ports = serial.tools.list_ports.comports() for port in ports: self.port_combo.addItem(port.device) if current_text and current_text not in [self.port_combo.itemText(i) for i in range(self.port_combo.count())]: self.port_combo.addItem(current_text) self.port_combo.setCurrentText(current_text) else: self.port_combo.setCurrentText(current_text if current_text else "") def toggle_serial_connection(self): if self.serial_reader and self.serial_reader.isRunning(): self.serial_reader.stop() self.status_label.setText("已断开") self.status_label.setStyleSheet("color: red;") self.connect_button.setText("连接串口") self.loop_timer.stop() else: port = self.port_combo.currentText() baud = int(self.baud_combo.currentText()) self.serial_reader = SerialReader(port, baud) self.serial_reader.data_received.connect(self.handle_serial_data) self.serial_reader.start() self.status_label.setText("已连接") self.status_label.setStyleSheet("color: green;") self.connect_button.setText("断开串口") def handle_serial_data(self, data): self.buffer.extend(data) self.raw_data_lines.append("Received (Hex): " + ' '.join(f"{b:02X}" for b in data)) # 将原始数据转换为Hex格式并添加到缓冲区 self.process_buffer() def process_buffer(self): while len(self.buffer) >= 78: found = False for i in range(len(self.buffer) - 77): if self.buffer[i + 12] == 0xA2 and self.buffer[i + 13] == 0x02: frame = self.buffer[i:i+78] self.buffer = self.buffer[i+78:] self.raw_data_lines.append("Received Frame (Hex): " + ' '.join(f"{b:02X}" for b in frame)) self.parse_hex_data(frame) found = True break if not found: break def flush_display(self): if self.raw_data_lines: lines = self.raw_data_lines self.raw_data_lines = [] for line in lines: cursor = self.raw_data_display.textCursor() cursor.movePosition(cursor.End) cursor.select(cursor.LineUnderCursor) line_count = self.raw_data_display.document().lineCount() if line_count >= self.max_lines: cursor.movePosition(cursor.Start) cursor.select(cursor.LineUnderCursor) cursor.removeSelectedText() cursor.deleteChar() cursor.movePosition(cursor.End) cursor.insertText(line + '\n') self.raw_data_display.setTextCursor(cursor) self.raw_data_display.ensureCursorVisible() for i in range(4): band_name = f"band{i + 1}" self.update_band_display(band_name, self.band_data_cache[band_name]) def parse_hex_data(self, data): try: hex_data = list(data) # 检查长度是否足够 if len(hex_data) < 78: return switch_byte = hex_data[15] switches = [(switch_byte >> i) & 1 for i in range(4)] if len(hex_data) > 29: temp = hex_data[25] volt = (hex_data[26] << 8) | hex_data[27] curr = (hex_data[28] << 8) | hex_data[29] self.band_data_cache["band1"]["temp"] = f"{temp}°C" self.band_data_cache["band1"]["voltage"] = f"{volt / 1000:.3f}V" self.band_data_cache["band1"]["current"] = f"{curr / 1000:.3f}A" self.band_data_cache["band1"]["enabled"] = "on" if switches[0] else "off" if len(hex_data) > 38: temp = hex_data[33] volt = (hex_data[34] << 8) | hex_data[35] curr = (hex_data[36] << 8) | hex_data[37] self.band_data_cache["band2"]["temp"] = f"{temp}°C" self.band_data_cache["band2"]["voltage"] = f"{volt / 1000:.3f}V" self.band_data_cache["band2"]["current"] = f"{curr / 1000:.3f}A" self.band_data_cache["band2"]["enabled"] = "on" if switches[1] else "off" if len(hex_data) > 46: temp = hex_data[41] volt = (hex_data[42] << 8) | hex_data[43] curr = (hex_data[44] << 8) | hex_data[45] self.band_data_cache["band3"]["temp"] = f"{temp}°C" self.band_data_cache["band3"]["voltage"] = f"{volt / 1000:.3f}V" self.band_data_cache["band3"]["current"] = f"{curr / 1000:.3f}A" self.band_data_cache["band3"]["enabled"] = "on" if switches[2] else "off" if len(hex_data) > 54: temp = hex_data[49] volt = (hex_data[50] << 8) | hex_data[51] curr = (hex_data[52] << 8) | hex_data[53] self.band_data_cache["band4"]["temp"] = f"{temp}°C" self.band_data_cache["band4"]["voltage"] = f"{volt / 1000:.3f}V" self.band_data_cache["band4"]["current"] = f"{curr / 1000:.3f}A" self.band_data_cache["band4"]["enabled"] = "on" if switches[3] else "off" except Exception as e: print("解析Hex数据失败:", e) def update_band_display(self, band_name, data): widgets = self.band_widgets.get(band_name) if not widgets: return if "enabled" in data: widgets["enabled"].setChecked(data["enabled"] == "on") if "temp" in data: widgets["temp"].setText(data["temp"]) if "current" in data: widgets["current"].setText(data["current"]) if "voltage" in data: widgets["voltage"].setText(data["voltage"]) def update_combined_frame(self): # 构造帧副本 frame = bytearray(self.preset_frame_template) # 计算启用的频段掩码 combined_mask = 0x00 for checkbox in self.band_checkboxes: if checkbox.isChecked(): band_name = checkbox.text() combined_mask |= self.band_masks[band_name] # 设置第16个字节 frame[15] = combined_mask # 计算 CRC(从第12字节开始) crc_data = bytes(frame[12:]) crc_value = crc16_modbus(crc_data) # 插入 CRC frame[8] = (crc_value >> 8) & 0xFF # 高位 frame[9] = crc_value & 0xFF # 低位 # 保存组合帧用于后续自定义帧使用 self.last_combined_frame = frame # 显示在发送框中 hex_with_crc = ''.join(f"{b:02X}" for b in frame) formatted_hex = ' '.join(hex_with_crc[i:i+2] for i in range(0, len(hex_with_crc), 2)) self.send_input.setText(formatted_hex) def send_combined_frame(self): hex_str = self.send_input.text().strip() if not hex_str: self.send_status.setText("请输入有效的Hex数据") self.send_status.setStyleSheet("color: red;") return try: hex_bytes = bytes.fromhex(hex_str.replace(' ', '')) if self.serial_reader and self.serial_reader.serial_port and self.serial_reader.serial_port.is_open: self.serial_reader.serial_port.write(hex_bytes) self.send_status.setText("组合帧发送成功") self.send_status.setStyleSheet("color: green;") else: self.send_status.setText("串口未连接") self.send_status.setStyleSheet("color: red;") except Exception as e: self.send_status.setText("发送失败:" + str(e)) self.send_status.setStyleSheet("color: red;") if __name__ == "__main__": app = QApplication(sys.argv) window = SerialDataDisplay() window.show() sys.exit(app.exec_())
11-18
Rebuild started: Project: Project *** Using Compiler 'V6.19', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Project' compiling app_ble.c... ../Src/main.c(200): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc('s', NULL); ~~~~^ ../Src/main.c(339): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc('w', NULL); ~~~~^ 2 warnings generated. compiling main.c... ../Src/app_at.c(755): warning: passing 'char *' to parameter of type 'uint8_t *' (aka 'unsigned char *') converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign] tcp_rec_show_update((char *)p->payload,p->len); ^~~~~~~~~~~~~~~~~~ ../Src/app_at.c(754): note: passing argument to parameter 'data' here extern void tcp_rec_show_update(uint8_t *data,uint32_t len); ^ 1 warning generated. compiling app_at.c... compiling app_btdm.c... ../Src/app_bt.c(662): warning: format specifies type 'char *' but the argument has type 'void *' [-Wformat] printf("hfg codec connection rsp: %s,type = %d\r\n",Info->p.ptr,codec_type); ~~ ^~~~~~~~~~~ 1 warning generated. compiling app_bt.c... compiling app_task.c... compiling app_hw.c... compiling app_lvgl.c... compiling diskio.c... compiling user_bt.c... compiling autonavi_handler.c... compiling autonavi_profile.c... compiling app_rpmsg.c... compiling msbc_sample.c... compiling app_hid_sdp.c... compiling app_audio.c... compiling sbc_sample.c... compiling mp3_sample.c... compiling btdm_mem.c... compiling controller.c... assembling controller_code.s... compiling host.c... assembling img_rom.s... compiling SWD.c... compiling fal_flash_port.c... compiling fdb_app.c... assembling startup_fr30xx.s... compiling driver_cali.c... compiling trim_fr30xx.c... compiling system_fr30xx.c... compiling driver_efuse.c... compiling driver_frspim.c... compiling driver_flash.c... compiling driver_gpio.c... compiling driver_pmu.c... compiling driver_pmu_iwdt.c... compiling driver_qspi.c... compiling driver_uart.c... compiling driver_trng.c... compiling driver_timer.c... compiling driver_dma.c... compiling driver_display.c... compiling driver_spi_master.c... compiling driver_sd_card.c... compiling driver_sd.c... compiling driver_ipc.c... compiling driver_can.c... compiling driver_pdm.c... compiling driver_i2s.c... compiling driver_psd_dac.c... compiling driver_display_dev.c... compiling driver_saradc.c... compiling driver_st7282_rgb_hw.c... compiling ext_flash.c... compiling co_log.c... ../../../../components/drivers/bsp/spi_flash/IC_W25Qxx.c(240): warning: incompatible pointer types passing 'uint16_t *' (aka 'unsigned short *') to parameter of type 'uint8_t *' (aka 'unsigned char *') [-Wincompatible-pointer-types] __SPI_Read_flash_X1(lu8_DataBuffer, 4, pu8_Buffer, fu32_Length); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../../../../components/drivers/bsp/spi_flash/IC_W25Qxx.h(37): note: expanded from macro '__SPI_Read_flash_X1' #define __SPI_Read_flash_X1(__CMD__, __CSIZE__, __BUFFER__, __SIZE__) spi_master_readflash_X1(&spi_flash_handle, (uint16_t *)__CMD__, __CSIZE__, (void *)__BUFFER__, __SIZE__) ^~~~~~~~~~~~~~~~~~~ ../../../../components/drivers/peripheral/Inc/driver_spi.h(522): note: passing argument to parameter 'fp_CMD_ADDR' here void spi_master_readflash_X1(SPI_HandleTypeDef *hspi, uint8_t *fp_CMD_ADDR, uint32_t fu32_CMDLegnth, uint8_t *fp_Data, uint16_t fu16_Size); ^ 1 warning generated. compiling IC_W25Qxx.c... compiling co_list.c... compiling fal.c... compiling co_util.c... compiling fal_flash.c... compiling fdb.c... compiling fal_partition.c... compiling croutine.c... compiling fdb_utils.c... compiling event_groups.c... compiling list.c... compiling fdb_kvdb.c... compiling stream_buffer.c... compiling queue.c... compiling timers.c... compiling port.c... compiling portasm.c... compiling heap_6.c... assembling cpu_context.s... compiling tasks.c... compiling freertos_sleep.c... compiling heap.c... compiling lv_indev.c... compiling lv_refr.c... compiling lv_obj_pos.c... compiling lv_flex.c... compiling lv_bmp.c... compiling lv_grid.c... compiling lv_ffmpeg.c... compiling lv_freetype.c... compiling lv_fs_fatfs.c... compiling lv_fs_posix.c... compiling lv_fs_stdio.c... compiling lv_fs_win32.c... compiling lv_gif.c... compiling gifdec.c... compiling lodepng.c... compiling lv_png.c... compiling lv_qrcode.c... compiling lv_rlottie.c... compiling lv_sjpg.c... compiling tjpgd.c... compiling lv_fragment.c... compiling lv_fragment_manager.c... compiling lv_gridnav.c... compiling lv_ime_pinyin.c... compiling qrcodegen.c... compiling lv_imgfont.c... compiling lv_msg.c... compiling lv_monkey.c... compiling lv_snapshot.c... compiling lv_theme_basic.c... compiling lv_theme_mono.c... compiling lv_animimg.c... compiling lv_theme_default.c... compiling lv_calendar.c... compiling lv_calendar_header_arrow.c... compiling lv_calendar_header_dropdown.c... compiling lv_imgbtn.c... compiling lv_colorwheel.c... compiling lv_chart.c... compiling lv_keyboard.c... compiling lv_led.c... compiling lv_list.c... compiling lv_menu.c... compiling lv_msgbox.c... compiling lv_meter.c... compiling lv_spinner.c... compiling lv_spinbox.c... compiling lv_span.c... compiling lv_tileview.c... compiling lv_win.c... compiling lv_tabview.c... compiling lv_font.c... compiling lv_extra.c... compiling lv_font_dejavu_16_persian_hebrew.c... compiling lv_font_fmt_txt.c... compiling lv_font_montserrat_8.c... compiling lv_font_loader.c... compiling lv_font_montserrat_10.c... compiling lv_font_montserrat_12.c... compiling lv_font_montserrat_12_subpx.c... compiling lv_font_montserrat_14.c... compiling lv_font_montserrat_16.c... compiling lv_font_montserrat_18.c... compiling lv_font_montserrat_20.c... compiling lv_font_montserrat_22.c... compiling lv_font_montserrat_24.c... compiling lv_font_montserrat_26.c... compiling lv_font_montserrat_28_compressed.c... compiling lv_font_montserrat_28.c... compiling lv_font_montserrat_30.c... compiling lv_font_montserrat_32.c... compiling lv_font_montserrat_34.c... compiling lv_font_montserrat_36.c... compiling lv_font_montserrat_38.c... compiling lv_font_montserrat_40.c... compiling lv_font_montserrat_42.c... compiling lv_font_montserrat_44.c... compiling lv_font_montserrat_48.c... compiling lv_font_montserrat_46.c... compiling lv_font_simsun_16_cjk.c... compiling lv_font_unscii_8.c... compiling lv_font_unscii_16.c... compiling lv_hal_tick.c... compiling lv_hal_indev.c... compiling lv_hal_disp.c... compiling lv_anim.c... compiling lv_anim_timeline.c... compiling lv_area.c... compiling lv_async.c... compiling lv_color.c... compiling lv_bidi.c... compiling lv_fs.c... compiling lv_gc.c... compiling lv_log.c... compiling lv_ll.c... compiling lv_math.c... compiling lv_lru.c... compiling lv_printf.c... compiling lv_mem.c... compiling lv_templ.c... compiling lv_style.c... compiling lv_style_gen.c... compiling lv_tlsf.c... compiling lv_timer.c... compiling lv_utils.c... compiling lv_txt.c... compiling lv_txt_ap.c... compiling lv_arc.c... compiling lv_btn.c... compiling lv_bar.c... compiling lv_checkbox.c... compiling lv_canvas.c... compiling lv_btnmatrix.c... compiling lv_img.c... compiling lv_dropdown.c... compiling lv_objx_templ.c... compiling lv_label.c... compiling lv_line.c... compiling lv_slider.c... compiling lv_switch.c... compiling lv_roller.c... compiling lv_table.c... compiling lv_textarea.c... compiling lv_demo_benchmark.c... compiling img_benchmark_cogwheel_alpha16.c... compiling img_benchmark_cogwheel_argb.c... compiling img_benchmark_cogwheel_chroma_keyed.c... compiling img_benchmark_cogwheel_indexed16.c... compiling img_benchmark_cogwheel_rgb.c... compiling img_benchmark_cogwheel_rgb565a8.c... compiling lv_font_bechmark_montserrat_12_compr_az.c.c... compiling lv_font_bechmark_montserrat_16_compr_az.c.c... compiling lv_font_bechmark_montserrat_28_compr_az.c.c... compiling lv_demo_stress.c... compiling img_clothes.c... compiling lv_demo_widgets.c... compiling img_demo_widgets_avatar.c... compiling img_lvgl_logo.c... compiling lv_demo_music.c... compiling lv_demo_music_list.c... compiling img_lv_demo_music_btn_corner_large.c... compiling img_lv_demo_music_btn_list_pause.c... compiling lv_demo_music_main.c... compiling img_lv_demo_music_btn_list_pause_large.c... compiling img_lv_demo_music_btn_list_play_large.c... compiling img_lv_demo_music_btn_list_play.c... compiling img_lv_demo_music_btn_loop.c... compiling img_lv_demo_music_btn_loop_large.c... compiling img_lv_demo_music_btn_next.c... compiling img_lv_demo_music_btn_next_large.c... compiling img_lv_demo_music_btn_pause.c... compiling img_lv_demo_music_btn_pause_large.c... compiling img_lv_demo_music_btn_play.c... compiling img_lv_demo_music_btn_play_large.c... compiling img_lv_demo_music_btn_prev.c... compiling img_lv_demo_music_btn_prev_large.c... compiling img_lv_demo_music_btn_rnd.c... compiling img_lv_demo_music_btn_rnd_large.c... compiling img_lv_demo_music_corner_left.c... compiling img_lv_demo_music_corner_left_large.c... compiling img_lv_demo_music_corner_right.c... compiling img_lv_demo_music_corner_right_large.c... compiling img_lv_demo_music_cover_1.c... compiling img_lv_demo_music_cover_1_large.c... compiling img_lv_demo_music_cover_2.c... compiling img_lv_demo_music_cover_2_large.c... compiling img_lv_demo_music_cover_3.c... compiling img_lv_demo_music_cover_3_large.c... compiling img_lv_demo_music_icon_1.c... compiling img_lv_demo_music_icon_1_large.c... compiling img_lv_demo_music_icon_2.c... compiling img_lv_demo_music_icon_2_large.c... compiling img_lv_demo_music_icon_3.c... compiling img_lv_demo_music_icon_3_large.c... compiling img_lv_demo_music_icon_4.c... compiling img_lv_demo_music_icon_4_large.c... compiling img_lv_demo_music_list_border.c... compiling img_lv_demo_music_list_border_large.c... compiling img_lv_demo_music_logo.c... compiling img_lv_demo_music_slider_knob.c... compiling img_lv_demo_music_slider_knob_large.c... compiling img_lv_demo_music_wave_bottom.c... compiling img_lv_demo_music_wave_bottom_large.c... compiling img_lv_demo_music_wave_top.c... compiling crc32.c... compiling ffsystem.c... compiling img_lv_demo_music_wave_top_large.c... compiling ffunicode.c... compiling lfs_util.c... compiling ext_flash_program.c... compiling ext_flash_uart.c... compiling ff.c... compiling lfs_port.c... compiling lv_common_function.c... compiling batt_full_yellow.c... compiling lfs.c... compiling batt_full_gren.c... compiling arialuni_bbp1_32px__.c... compiling Number_HarmonyOS_bpp4_16px.c... compiling Number_HarmonyOS_bpp4_12px.c... compiling Number_HarmonyOS_bpp4_36px.c... compiling Number_HarmonyOS_bpp4_44px.c... compiling Number_HarmonyOS_bpp4_92px.c... compiling Number_HarmonyOS_bpp4_20px.c... compiling fr_lv_test_page.c... compiling fr_lv_86box_page.c... compiling fr_lv_app_music_control.c... compiling fr_lv_customer_page.c... compiling fr_lv_instrument_panel.c... compiling fr_lv_list_page.c... compiling fr_lv_instrument_panel_km.c... compiling fr_lv_can_page.c... compiling fr_lv_bt_pan_page.c... compiling fr_guimain.c... compiling fr_watch.c... compiling lv_user_sqlist.c... compiling fr_device_button.c... compiling fr_device_rtc.c... compiling fr_device_encode.c... compiling fr_device_pmu_io.c... compiling fr_device_canfd.c... compiling autoip.c... compiling icmp.c... compiling etharp.c... compiling igmp.c... compiling dhcp.c... compiling ip4.c... compiling ip4_addr.c... compiling dhcp6.c... compiling ethip6.c... compiling ip4_frag.c... compiling icmp6.c... compiling inet6.c... compiling ip6.c... compiling ip6_addr.c... compiling ip6_frag.c... compiling mld6.c... compiling nd6.c... compiling altcp.c... compiling altcp_alloc.c... compiling altcp_tcp.c... compiling def.c... compiling inet_chksum.c... compiling init.c... compiling dns.c... compiling ip.c... compiling mem.c... compiling memp.c... compiling raw.c... compiling netif.c... compiling stats.c... compiling sys.c... compiling pbuf.c... compiling tcp_in.c... compiling tcp.c... compiling tcp_out.c... compiling timeouts.c... compiling udp.c... compiling api_lib.c... compiling err.c... compiling if_api.c... compiling netbuf.c... compiling api_msg.c... compiling netdb.c... compiling netifapi.c... compiling tcpip.c... compiling ethernet.c... compiling ethernetif.c... compiling sys_arch.c... compiling sockets.c... compiling rpmsg.c... compiling rpmsg_ns.c... compiling rpmsg_lite.c... compiling llist.c... compiling rpmsg_queue.c... compiling virtqueue.c... compiling rpmsg_env_freertos.c... compiling rpmsg_platform.c... assembling dsp_code_rom.s... compiling dsp.c... compiling dsp_mem.c... compiling audio_a2dp_sink.c... compiling audio_a2dp_source.c... compiling audio_encoder.c... compiling audio_rpmsg.c... ../../../../components/modules/audio/audio_decoder.c(72): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc(hex2char[(value >> 12)&0xf], NULL); ~~~~^ ../../../../components/modules/audio/audio_decoder.c(73): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc(hex2char[(value >> 8)&0xf], NULL); ~~~~^ ../../../../components/modules/audio/audio_decoder.c(74): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc(hex2char[(value >> 4)&0xf], NULL); ~~~~^ ../../../../components/modules/audio/audio_decoder.c(75): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc(hex2char[(value >> 0)&0xf], NULL); ~~~~^ 4 warnings generated. compiling audio_decoder.c... compiling audio_hw.c... compiling audio_scene.c... compiling local_playback.c... compiling audio_sco.c... compiling loopback.c... compiling recorder.c... compiling voice_recognize.c... compiling algorithm.c... compiling codec.c... compiling resample.c... compiling AMS_client.c... compiling simple_gatt_service.c... compiling ANCS_AMS_client.c... compiling hid_service.c... compiling retarget_io.c... linking... ..\..\..\..\components\tools\keil\xip_flash_add_psram.sct(39): warning: L6329W: Pattern algorithm.o(RO) only matches removed unused sections. ..\..\..\..\components\tools\keil\xip_flash_add_psram.sct: Error: L6220E: Load region LR_IROM1 size (1300776 bytes) exceeds limit (1040384 bytes). Region contains 2110 bytes of padding and 2090 bytes of veneers (total 4200 bytes of linker generated content). ..\..\..\..\components\tools\keil\xip_flash_add_psram.sct: Error: L6221E: Execution region RW_RAM_CODE with Execution range [0x1ffe01b0,0x20000c3c) overlaps with Execution region RW_IRAM1 with Execution range [0x20000000,0x2005cb30). ..\..\..\..\components\tools\keil\xip_flash_add_psram.sct: Error: L6388E: ScatterAssert expression ((ImageLength(RW_RAM_CODE_FRONT) + ImageLength(RW_RAM_CODE)) <= 0x20000) failed on line 52 : (0x20c30 <= 0x20000) Finished: 0 information, 1 warning and 3 error messages. ".\Objects\Project.axf" - 3 Error(s), 10 Warning(s). Target not created. Build Time Elapsed: 00:01:15什么问题
09-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值