import socket import binascii def convert_to_decimal(hex_str): return int(hex_str, 16) def main(): TCP_IP = '192.168.0.7' TCP_PORT = 26 MESSAGE = bytes.fromhex('01 03 00 00 00 02 C4 0B') with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((TCP_IP, TCP_PORT)) s.send(MESSAGE) data = s.recv(1024) hex_data = binascii.hexlify(data).decode('utf-8') print("Received Hex Data:", hex_data) # 获取校验位和所需的数据部分 # checksum = hex_data[-4:] # 校验位是最后两个字节 required_data = hex_data[10:-4] # 数据部分是从索引12开始到校验位之前 # decimal_checksum = convert_to_decimal(checksum) decimal_required_data = (int)(convert_to_decimal(required_data)/1000) # print("Decimal Checksum:", decimal_checksum) print("Decimal Required Data:", decimal_required_data) if __name__ == "__main__": main()