3

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!

Emerson
  • 270
  • 2
  • 4
  • 11
  • If you have telnet program installed, you can test if connections work at all with `telnet 192.168.1.8 502` – J.J. Hakala Jan 31 '16 at 19:15
  • Thanks for the comment. I've verified that both connections work independently. Problem seems to arise when I attempt to connect to both at the same time. – Emerson Jan 31 '16 at 20:24
  • I tried your code with 2 pymodbus servers on `localhost` (a windows 7 pc). One server is listening on port `5020`, the another one on `5021`. Are you sure you configured the ports correctly? Because in the pymodbus examples the configured default port on the server is `5020` and on the client it is `502`. – wewa Feb 01 '16 at 10:29
  • I'm using two arduinos as slaves. I changed the ports to the defaults you mentioned on the servers(arduinos) and in my master code. Still not working! Driving me insane. Again, appreciate everyone's input. – Emerson Feb 01 '16 at 17:24
  • 2
    Is 192.168.1.8 the IP of your local host by any chance ? If so try running your code with elevated privileges (e.g sudo) . 502 is reserved port and requires admin privileges to access it. https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports , if not try to use a port > 1024 while running modbus server and see if that helps. – Sanju Feb 04 '16 at 06:45
  • 192.168.1.3 is the ip of the local host (Modbus TCP Master). I have been out of the country and will try your sudo suggestion when I get a chance and report back. Thanks Sanju. – Emerson Feb 22 '16 at 19:25

1 Answers1

0

I tried it here on a PC with no problems. You do have unneeded lines:

connection1 = client1.connect()
..
connection2 = client2.connect()

Maybe that's the problem...

zappfinger
  • 497
  • 2
  • 5
  • 15