当我试图使用pymodbus和树莓pi读取保持寄存器时,我遇到了一个问题。我似乎无法同时连接两个服务器/从机(一个或另一个可以工作,但在一起,我无法从两个设备读取寄存器)。这些连接问题似乎是导致错误消息的原因。也许我错过了一些简单的东西?
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]
我知道,如果将这些脚本分解为不同的脚本并同时运行它们,则会发生相同的错误。
独立地,就像一次运行一个,它们正确地执行,没有错误。
非常感谢大家的帮助!
最佳答案
我在电脑上试过了,没有问题。您确实有不需要的行:
connection1 = client1.connect()
..
connection2 = client2.connect()
也许这就是问题所在。。。
关于python - 多个服务器pymodbus连接问题Modbus TCP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35118083/