From 10a35867c5994be02e76598afa5a7002de6e867c Mon Sep 17 00:00:00 2001 From: yanganjie Date: Tue, 4 Nov 2025 10:21:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=8F=98=E9=80=81=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test.py 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