【发布时间】:2021-08-18 20:53:25
【问题描述】:
我一直在尝试使用 pymodbus 通过 Modbus TCP 从 Modbus(此处为树莓派)读取数据。我不断收到无法连接的错误。这是产生此错误的代码 sn-p。
from pymodbus.client.sync import ModbusTcpClient
#modbus connection
client = ModbusTcpClient('192.168.137.238')
connection = client.connect()
#read register
request = client.read_input_registers(1008,3)
result = request.registers
print(result)
我在 pi 上本地运行它。我使用 ifconfig 得到以下信息,并使用 ModbusTcpClient 尝试了所有 IP 地址。我不确定我在这里缺少什么。
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.137.238 netmask 255.255.255.0 broadcast 192.168.137.255
inet6 fe80::7ce5:23eb:968f:59de prefixlen 64 scopeid 0x20<link>
ether dc:a6:32:46:d3:1e txqueuelen 1000 (Ethernet)
RX packets 438 bytes 46776 (45.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 330 bytes 31566 (30.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 1847 bytes 149291 (145.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1847 bytes 149291 (145.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.1.1.199 netmask 255.255.255.0 broadcast 10.1.1.255
inet6 fe80::7f47:6c9f:f1c:1274 prefixlen 64 scopeid 0x20<link>
inet6 fd9d:fbd1:372:0:7a9c:777b:a806:2350 prefixlen 64 scopeid 0x0<global>
ether dc:a6:32:46:d3:21 txqueuelen 1000 (Ethernet)
RX packets 208 bytes 16783 (16.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 53 bytes 7372 (7.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
我已经看过这个Question 1 和Question 2。此外,带有 pi 的 pymodbus 可以在 RTU 上运行。
【问题讨论】:
标签: python raspberry-pi pymodbus