问题描述:

I have an issue when I try to read holding registers using pymodbus and a raspberry pi. I can't seem to get two servers/slaves connected at the same time (either one or another will work, but together, I cannot read registers from both devices). These connection issues seem to be causing the error messages. Maybe there is something simple I'm missing here?

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()

Here is the error message I receive:

[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]

I know that if I break these up into different scripts and run them at the same time, the same error will occur.

Independently, as in running one at a time, these execute correctly with no errors.

Everyone's help is greatly appreciated!

相关阅读:
Top