1

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.

Banks
  • 179
  • 13

1 Answers1

1

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.

  • Modbus slave is there, that's how I'm able to get data from RPi using Modbus RTU protocol. And yes, I'm trying to get data from the RPi – Banks Aug 18 '21 at 21:22
  • Then it would be interesting to see your slave setup. Also you may try to connect from PC first, with some simple sowtware like "Modus master tool". – Artyrm Sergeev Aug 19 '21 at 00:57
  • Apparently, it was able to connect at port 22 and not at 502. But now it cannot recognise registers. And a different script running RTU recognise the registers with the same address. – Banks Aug 19 '21 at 02:36
  • I have posted the 2 scripts [here](https://stackoverflow.com/questions/68841668/modbus-rtu-works-but-modbus-tcp-doesnt-with-pymodbus?noredirect=1#comment121663499_68841668) – Banks Aug 19 '21 at 04:35
  • 1
    That's what I was talking about. Different port or slave number is often the reason master couldn't connect to slave over TCP. – Artyrm Sergeev Aug 19 '21 at 07:47
  • Somehow "You need 50 reputation" to comment there. What I can say is 1) that "waiting for 12000 bytes" is hardly what you meant, so maybe the settings for registers are interpreted like some huge number - you'd better check it with help/examples. RTU is much more tolerable on that matter an may forgive you "asking non-existent registers" from slave. 2) personally I had good experience with modbus_tk library. – Artyrm Sergeev Aug 19 '21 at 07:52
  • I think you were also correct about the TCP slave server, I might have misunderstood you. Do you think it would be a good idea to use RTU as a master to read/write from registers and then use it t make a TCP slave server? The reason is because they are still communicating over RS485. Or can I still write a TCP salve that can read/ write values directly from registers? – Banks Aug 19 '21 at 12:30
  • Adding on to the previous comment, there are sensors connected directly to the ports. – Banks Aug 19 '21 at 12:37
  • 1
    AFAIK slave only presents data, master reads. So if you have directly connected Modbus RTU sensors that you want to aggregate on RPI and translate further over Modbus TCP, then I think yes, you should go like that: Sensors--->(Modbus RTU Master *RPI* Modbus TCP Slave)--->Server I.e. RPI should have both Master RTU and Slave TCP running and translate values between their registers programmicaly via variables (m.b. even dataframes). – Artyrm Sergeev Aug 19 '21 at 20:47
  • 1
    Many thanks, this has been indeed helpful. Would you happen to know/ refer me to a minimal implementation for Modbus RTU Master RPi to Modbus TCP Slave for reading/ writing registers? – Banks Aug 19 '21 at 22:54
  • 1
    You're welcome. Unfortunately, no. But I think it shouldn't be that hard. You might look at those two examples of mudbus_tk and see, if they appear easier for you than pymodbus: https://github.com/ljean/modbus-tk/blob/master/examples/rtumaster_example.py https://github.com/ljean/modbus-tk/blob/master/examples/tcpslave_example.py Anyways, I'd suggest to start with just one value, and see how it goes. Since addressing tables in Modbus is always tricky. – Artyrm Sergeev Aug 20 '21 at 00:18