diff --git a/test.py b/test.py new file mode 100644 index 0000000..de26e5b --- /dev/null +++ b/test.py @@ -0,0 +1,32 @@ +import socket + +# 设备信息 +IP = "192.168.250.63" +PORT = 502 +TIMEOUT = 5 # 超时时间(秒) + +# 创建TCP socket +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + try: + s.settimeout(TIMEOUT) # 设置超时,避免一直阻塞 + # 连接设备 + s.connect((IP, PORT)) + print(f"✅ 已通过TCP连接到 {IP}:{PORT}") + + # 尝试接收数据(不发送任何请求,纯等待) + print("等待设备发送数据...(若5秒内无响应则超时)") + data = s.recv(1024) # 最多接收1024字节 + + if data: + # 打印收到的原始数据(16进制和字节列表) + # print(f"收到数据(16进制):{data.hex()}") + print(f"收到数据(字节列表):{list(data)}") + else: + print("❌ 未收到任何数据(设备未主动发送)") + + except ConnectionRefusedError: + print(f"❌ 连接失败:{IP}:{PORT} 拒绝连接(设备离线/端口错误)") + except socket.timeout: + print(f"❌ 超时:{TIMEOUT}秒内未收到设备数据(设备未主动发送)") + except Exception as e: + print(f"❌ 发生错误:{str(e)}") \ No newline at end of file