diff --git a/protocol/TCP/IOTCPClinet.py b/protocol/TCP/IOTCPClinet.py index 58797bf..85e758f 100644 --- a/protocol/TCP/IOTCPClinet.py +++ b/protocol/TCP/IOTCPClinet.py @@ -7,7 +7,7 @@ from collections import deque logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') -class Box1Communicator: +class TCPCommunicator: START_FLAG = b'\xF3\x12' END_FLAG = b'\xF3\x14' HEADER_SIZE = 10 @@ -262,7 +262,7 @@ class Box1Communicator: # AO响应处理 elif dataType == self.RESP_WRITE_AO: - print(len(body)) + # print(len(body)) success = struct.unpack('' + 'd'*36, body) + # 修改:使用小端字节序解包数据 + ao_data = struct.unpack('<' + 'd'*36, body) self.ao_values = list(ao_data) logging.info(f"接收到的AO: {ao_data}... (total {len(ao_data)} values)") return self.create_ao_response(True, packet_num) @@ -172,32 +173,45 @@ class Box1Simulator: for i in range(4): if random.random() < 0.2: # 20%几率翻转 self.di_states[i] = 1 - self.di_states[i] + # print(self.di_states, 1111) - # 打包AI数据 (8 doubles) - ai_bytes = struct.pack('>' + 'd'*8, *self.ai_values) + # 修改:生成500组AI/DI数据 + full_body = b'' + for _ in range(500): + # 修改:使用小端字节序打包8个AI值 + ai_bytes = struct.pack('<' + 'd'*8, *self.ai_values) + + # 打包DI状态为一个64位整数 + di_state_int = 0 + for i, state in enumerate(self.di_states): + di_state_int |= (state << i) + + di_bytes = struct.pack('Q', di_state_int) + # 使用logging记录发送的AI数据 + logging.info(f"发送的AI为: AI={self.ai_values}, DI={self.di_states} (共500组)") - body = ai_bytes + di_bytes - print("发送的AI为", self.ai_values, self.di_states) - return self.create_packet(self.RESP_READ_AI, body, packet_num) - + return self.create_packet(self.RESP_READ_AI, full_body, packet_num) def create_ao_response(self, success, packet_num): """创建AO写入响应数据包""" # 使用0.0表示成功,1.0表示失败 result = 0.0 if success else 1.0 - body = struct.pack('>d', result) + # 修改:使用小端字节序 + body = struct.pack('' + 'd'*16, *self.deltat_values) + # 修改:使用小端字节序打包16个64位无符号整数 + body = struct.pack('<' + '16Q', *self.deltat_values) + + # 使用logging记录发送的DeltaT数据 + logging.info(f"发送的DeltaT为: {self.deltat_values}") + return self.create_packet(self.RESP_READ_DELTAT, body, packet_num) def create_packet(self, cmd_type, body, packet_num):