ConnectionException: Modbus Error: [Connection] Failed to connect ModbusTcpClient

Viewed 724

I have been trying to read data from Modbus (Raspberry pi here) over Modbus TCP using pymodbus. I keep on getting the error unable to connect. Here is the code snippet producing this error.

from pymodbus.client.sync import ModbusTcpClient

#modbus connection
client = ModbusTcpClient('192.168.137.238')
connection = client.connect()

#read register
request = client.read_input_registers(1008,3)
result = request.registers
print(result)

I am running this locally on the pi. I get the following with ifconfig and have tried all the IP addresses with ModbusTcpClient. I'm not sure what am I missing here.

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.137.238  netmask 255.255.255.0  broadcast 192.168.137.255
        inet6 fe80::7ce5:23eb:968f:59de  prefixlen 64  scopeid 0x20<link>
        ether dc:a6:32:46:d3:1e  txqueuelen 1000  (Ethernet)
        RX packets 438  bytes 46776 (45.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 330  bytes 31566 (30.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1847  bytes 149291 (145.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1847  bytes 149291 (145.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.1.199  netmask 255.255.255.0  broadcast 10.1.1.255
        inet6 fe80::7f47:6c9f:f1c:1274  prefixlen 64  scopeid 0x20<link>
        inet6 fd9d:fbd1:372:0:7a9c:777b:a806:2350  prefixlen 64  scopeid 0x0<global>
        ether dc:a6:32:46:d3:21  txqueuelen 1000  (Ethernet)
        RX packets 208  bytes 16783 (16.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 53  bytes 7372 (7.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I have already seen this Question 1 and Question 2. Also, pymodbus with pi works over RTU.

1 Answers

As I can see - you want do get data from RPi? But seems to me your setup is for RPi to get data from a 'client' with it's own IP. I believe you need to set up a Modbus slave first to be able to present data with Modbus TCP.

Related