Files
Feeding_control_system/test.py
2025-11-04 10:21:49 +08:00

32 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)}")