Search code examples
c#modbus

c# Nmodbus how to tcp slave


I'm converting a project from NModbus4 to NModbus (since NModbus4 has last commit 5 years ago) but I can't find any modbus slave tcp implementation example so I'm stuck to this code and don't know how to get further. I'm searching for a solution all morning but

    applicationLog.Info("Initializing modbus slave");
    TcpListener tcpListener = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), Properties.Settings.Default.TcpModbusPort);
    modbusSlave = modbusFactory.CreateSlave(1, new SlaveDataStore());
    modbusSlaveTransport = modbusFactory.CreateSlaveNetwork(tcpListener);
    applicationLog.Info("Modbus slave listening on port " + Properties.Settings.Default.TcpModbusPort);

Can anyone help me?


Solution

  • With this package I've managed to connect to Modbus RTU<->TCP converter that communicates with measuring device:

    https://www.nuget.org/packages/NModbus/

    Here you have some code samples that may be helpful (Especially from line 466):

    https://github.com/NModbus/NModbus/blob/master/Samples/Program.cs

    I have tested one method for reading holding registers (but as master) and it seems to be working.