import socket import base64 import json import time import psutil import struct import csv from config import * from x_ray_data_processor import processor from PySide6.QtCore import QObject, Signal, Slot, QThread,QTimer # 定义adcDataUnitDef结构体 class adcDataUnitDef: def __init__(self): self.ucCmd = 0 self.ucAddr = 0 self.usData = 0 class UdpServer(QThread): start_cal = Signal(int, bytearray, str, int) # 信号:接收到消息,包含数据、发送者地址和端口 send_collect_data = Signal(dict) # update_acquisition_info = Signal(int, dict) def __init__(self, ip="0.0.0.0", port=50009, parent=None): super(UdpServer, self).__init__(parent) self.kill_udp_port(port) self.ip = ip self.port = port self.server_socket = None self.running = True self.motor1_direction = "" self.motor2_direction = "" self.collect_object1 = { "method" : "scanner1", "channelA_data" :"base64", "channelB_data" :"base64", "intensityA" : 98, "intensityB" : 98 } self.collect_object2 = { "method" : "scanner2", "channelA_data" :"base64", "channelB_data" :"base64", "intensityA" : 98, "intensityB" : 98 } self.debug_timer = QTimer(self) self.debug_timer.timeout.connect(self.update_debug_timer) if YSMODE: self.debug_timer.start(1000) def update_debug_timer(self): import random channelA = [random.randint(1, 1000) for _ in range(3000)] channelB = [random.randint(1, 1000) for _ in range(3000)] self.collect_object1['channelA_data'] = base64.b64encode(json.dumps(channelA).encode()).decode() self.collect_object1['channelB_data'] = base64.b64encode(json.dumps(channelB).encode()).decode() self.collect_object2['channelA_data'] = base64.b64encode(json.dumps(channelA).encode()).decode() self.collect_object2['channelB_data'] = base64.b64encode(json.dumps(channelB).encode()).decode() self.send_collect_data.emit(self.collect_object1) self.send_collect_data.emit(self.collect_object2) def kill_udp_port(self, port): for proc in psutil.process_iter(): try: for conn in proc.connections(kind='udp'): if conn.laddr.port == port: print(f" PID={proc.pid}, procname={proc.name()}") proc.kill() except psutil.AccessDenied: continue def packet_crc(self, data): usSum = 0 for byte in data: usSum += byte return usSum & 0xFFFF # 确保返回值为16位 def handle_sample_data(self, data): # 确保数据长度是4的倍数 if len(data) % 4 != 0: raise ValueError("Data length must be a multiple of 4") # 使用列表推导式来简化代码 unpacked_data = [struct.unpack_from(' len(binary_data): print("offset:", offset, "data_len:", len(data_points)) break channel_A.append(struct.unpack('