Search code examples
tcpraspberry-piplcmodbus-tcp

Pymodbus Failed to Connect with PLC Slave and Raspberry Pi Master


I am trying to use a Raspberry Pi 3 Model B to read values from an Allen Bradly PLC. I'm using the Pymodbus Modbus TCP protocol to communicate between them.

When I run a test client I get the following error:

pi@raspberrypi:/var $ python test1.py
Got here 1
Traceback (most recent call last):
File "test1.py", line 12, in <module>
request = client.read_holding_registers(0,1)
File "build/bdist.linux-armv7l/egg/pymodbus/client/common.py", line 109, in read_holding_registers
File "build/bdist.linux-armv7l/egg/pymodbus/client/sync.py", line 82, in execute
pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[10.0.0.237:502]

Here is the code for the test client:

from pymodbus.client.sync import ModbusTcpClient

#modbus connection 
client = ModbusTcpClient(host='10.0.0.237')
connection = client.connect()

#test print
print "Got here 1"

#read register
request = client.read_holding_registers(0,1)

print request

client.close()

I can ping the IP address of the PLC's ethernet card (10.0.0.237) just fine. (The PLC is on and shows up in RSLinx and RSLogix5000.) But when I try to check port 502 (which Modbus uses) with the command:

telnet 10.0.0.237 502

I get the error:

Trying 10.0.0.237...
telnet: Unable to connect to remote host: Connection refused

The research I've done told me to check whether that port is open, but that is for PCs, I don't know how you check/configure the ports on a PLC.

In addition, I am wondering if the problem is that I am trying to just use Modbus to go between the Raspbery Pi and PLC. I have found prosoft gateways that do the interfacing. But I couldn't find any information on whether those were just another option or whether they were required.

I am new to PLCs and networking, so any help would be appreciated.


Solution

  • I have not used Allen Bradley myself but I do use a multitude of other PLCs though and they all work more or less the same.

    You dont open the port as you would in a router but rather activate modbus connectivity in the PLC. Most often its a setting but if Allen Bradley use their own proprietary bus system (e.g Saia S-Bus) you sometimes need a specific function block to enable modbus.

    A quick google search for example showed that in micrologix 1400 theres a checkbox to enable modbus tcp under some channel setting.

    Check in the PLC if there is a setting to enable modbus master/client (master=serial or RTU, client=TCP but sometimes manufacturers dont differentiate them).

    To test the Raspberry Pi you can download a modbus server simulator and poll that.

    To test the PLC with something other then the Pi you could download a modbus client/master. I personally use QmodMaster which is free and works well for testing purposes (search google).

    Also, make sure youre polling using the right function code. This can be confusing as holding registers (which are addressed 4xxxx) use function code 03. Input registers (which are addressed 3xxxx) use function code 04.

    A modbus gateway is used to enable serial modbus (RTU or ASCII) to communicate via TCP.

    If both devices do have modbus tcp theres no need for a gateway. If for example you have a device which only has wired modbus (RS232/485/422) you can connect that to a gateway and the gateway to TCP.