pymodbus3pymodbus

Modbus TCP Client does not connect


Background info:

I am trying to read data from my energy meter. I have connected a gprs modem to the meter(via rs485) port. This modem sends data via tcp-ip protocol to a specified server and port. On the server(aws ec2 instance) I can see a connection from the device.

Question:

When i try to connect to this device using PyModbus, i am unable to open connection.

from pymodbus.client.sync import ModbusTcpClient as Modbusclient
client= Modbusclient(host=<internal ip of the connection>, port=5025)
if client.connect():
    print("Connected")
else:
    print("Not Connected")

The response is always "Not Connected".

I have ensured that a) the port is open. b) ip address is whitelisted on the server

Why can't i connect? What am i missing here?


Solution

  • In my particular case, the gprs modem was acting as a serial-over-tcp gateway.So i had to create a serial port and then connect to it. What i ended up doing was:

    1. Create a pair of serial ports (e.g. p1 and p2) on Linux using socat command
    2. Create a simple tcp listener (using sockets library in python)
    3. Forward all data from tcp port to p1
    4. Import ModbusSerialClient from pymodbus.client.sync
    5. connect to P2 using Modbus Serial Client.