1

I've set up a Modbus-tcp slave using NModbus4 library.

I want to show the slave's status, whether it's listening(running?) or not running(idle?) - to show visually with green/red indicators.

I've looked through NModbus4 library and couldn't find any function that indicates the slave's status. Below is my best shot but it doesn't indicate if the slave stopped.

case "TCP":
    try
    {
        TcpListener slaveTcpListener = new TcpListener(IP, Port);
        slaveTcpListener.Start();
        slave = ModbusTcpSlave.CreateTcp(SlaveID, slaveTcpListener);
        slave.DataStore = DataStoreFactory.CreateDefaultDataStore(MaxCoil, MaxDI, MaxHR, MaxIR);
        slave.DataStore.DataStoreReadFrom += new EventHandler<DataStoreEventArgs>(dataStore_DataStoreReadFrom);
        slave.ModbusSlaveRequestReceived += Slave_ModbusSlaveRequestReceived;
        slave.Listen();

        lblCommStatus.Text = "Listening";
        btnIndicator.BackColor = Color.Green;
    }
    catch
    {
        // Exception Handle
    }

Any help is appreciated.

Thanks.

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73
  • Please clarify your requirement. `slave.Listen()` will block so code after that will not execute. I guess you could periodically call `slave.Masters` to see what (if anything) is connected. Note that [NModbus4](https://github.com/NModbus4/NModbus4) has had no commits since 2016 so you may want to look elsewhere (perhaps [NModbus](https://github.com/NModbus/NModbus/) which is a fork). – Brits Jun 20 '22 at 20:02

0 Answers0