当我尝试使用 pymodbus 和树莓派读取保持寄存器时遇到问题。我似乎无法同时连接两台服务器/从机(一个或另一个都可以工作,但在一起时,我无法从两个设备读取寄存器)。这些连接问题似乎是导致错误消息的原因。也许我在这里缺少一些简单的东西?
from pymodbus.client.sync import ModbusTcpClient
import time
#modbus connection to 1st device
client1 = ModbusTcpClient('172.168.1.9', port=659)
connection1 = client1.connect()
#modbus connection to 2nd device
client2 = ModbusTcpClient('192.168.1.8', port=502)
connection2 = client2.connect()
#read registers of 1st device
request1 = client1.read_holding_registers(11,27) #covert to float
result1 = request1.registers
print (result1)
close = client1.close()
#read registers of 2nd device
request2 = client2.read_holding_registers(1,7) #covert to float
result2 = request2.registers
print (result2)
close = client2.close()
这是我收到的错误消息:
[18, 57, 48, 984, 31, 1, 16, 1, 2, 78, 87, 4, 4, 0, 299, 65517, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Traceback (most recent call last):
File "gpsonce", line 20, in <module>
request2 = client2.read_holding_registers(1,7) #covert to float
File "/usr/local/lib/python2.7/dist-packages/pymodbus-1.2.0-py2.7.egg/pymodbus/client/common.py", line 109, in read_holding_registers
return self.execute(request)
File "/usr/local/lib/python2.7/dist-packages/pymodbus-1.2.0-py2.7.egg/pymodbus/client/sync.py", line 82, in execute
raise ConnectionException("Failed to connect[%s]" % (self.__str__()))
pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[192.168.1.8:502]
我知道如果我将它们分解成不同的脚本并同时运行它们,也会发生同样的错误。
独立地,就像一次运行一个一样,它们可以正确执行而没有错误。
非常感谢大家的帮助!