0
votes

I need to communicate with several modules and devices using the ModBus Protocol and the Raspberry Pi. I am using PyModBus on Raspberry Pi to read/write Modbus function codes. (https://pymodbus.readthedocs.io/en/latest/readme.html)

I was able to communicate Modbus RTU over RS485 with the device but now that I am trying to communicate ModbusTCP over a Ethernet cable and keep running into the following error:

import pymodbus
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('127.0.0.1')
connection = client.connect()

Output: ERROR.pymodbus.client.sync: Conection to (127.0.0.1, 502) failed: [Errno 111] connection refused

Any tips or explanation for the error?

1

1 Answers

1
votes

127.0.0.1 is a loopback address; this means that ModbusTcpClient('127.0.0.1') will attempt to establish a connection to the Pi iteslf. Unless there is a Modbus server running on the Pi the error you received is to be expected.

"I am trying to communicate ModbusTCP over a Ethernet cable" indicates you are communicating with another device which should have it's own IP address. You need to work out what that address is and use that when attempting to connect (as well as ensuring your network setup is valid). The method used to set/determine a devices address varies from device to device so you would need to check the documentation (you did not specify what the device is).