0

I am setting up a new TCP Server connected to client through an Ethernet TCP/IP modbus and is supposed to push certain values to a given modbus register (hr = 6022), every few seconds. I do not see any exceptions/errors raised by the script but no data is received by the client. With a StartTCPserver command, I expected to see any network traffic (atleast the handshake) but I do not see any traffic on Wireshark. What could be the next possible diagnostic?

I have tried running similar script locally (without an external ethernet connection); One acting as a client and another as a server and did see the values update on the client register.

from pymodbus.server.sync import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
import time
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s'
          ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)


def run_server():
    store = ModbusSlaveContext(
        ir=ModbusSequentialDataBlock(6022, [152, 276]), 
        zero_mode=True
    )
    context = ModbusServerContext(slaves=store, single=True)
    StartTcpServer(context, address=("192.168.10.2", 502))


if __name__ == "__main__":
    run_server()
MuizVhora
  • 53
  • 1
  • 1
  • 7
  • The address on which the server is running and the address at which the client is requesting registers are different. Is that a typo? – Sanju Sep 13 '19 at 01:02
  • That was a typo. I keep changing the server IP address so, I can avoid error when I attempt to connect multiple times "OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted." Not a great way to deal with, but I use this workaround just to get the job done. – MuizVhora Sep 13 '19 at 16:27
  • Can you share debug logs for both client and server? – Sanju Sep 14 '19 at 01:36

1 Answers1

0

The lines after run_server() are never reached. Code connecting to the server can be placed in a different script;

from pymodbus.client.sync import ModbusTcpClient as ModbusClient     

cli = ModbusClient('192.168.10.2', port=502)                       
assert cli.connect()                                                 
res = cli.read_input_registers(6022, count=1, unit=1)                
print(res.registers[0]) 
Bosz
  • 359
  • 6
  • 15
  • Yes, that's a part of a separate script. I just did't present it very well. – MuizVhora Sep 13 '19 at 16:29
  • I did a test with the server code you provided and the client as I posted and did not have any problems. – Bosz Sep 14 '19 at 11:38
  • Thanks. Since running these client and server programs from separate python terminals doesn't require an Ethernet connection, I tried those too and was able to receive data as I mentioned in the question. However, when I connect to an actual client device (via external Ethernet connection), that's where I am unable to send/receive data. Any ideas what could be wrong. Been stuck for a while now. – MuizVhora Sep 19 '19 at 14:08