Click here to Skip to main content
15,793,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a .net c# winforms application , to interact with a master device over serial port. The application should simulate 3 slaves, by responding to the master device requests.
I'm trying to use NModbus, but i'm lost on how to use it for multiple slaves.
Please help.


What I have tried:

using (SerialPort slavePort = new SerialPort(selectedPort))
               // configure serial port
               slavePort.BaudRate = 19200;
               slavePort.DataBits = 8;
               slavePort.Parity = Parity.Even;
               slavePort.StopBits = StopBits.One;

               //Create an instance of the ModbusFactory
               IModbusFactory factory = new ModbusFactory();

               //Create an adapter for the serial port
               var adapter = new SerialPortAdapter(slavePort);

               //Create the slave network
               IModbusSlaveNetwork modbusSlaveNetwork = factory.CreateRtuSlaveNetwork(adapter);

               //Create the slaves
               IModbusSlave slave1 = factory.CreateSlave(1);
               IModbusSlave slave2 = factory.CreateSlave(2);
               IModbusSlave slave31 = factory.CreateSlave(31);
            // need to figure how to use datastore

               //Add the slaves to the network

               await modbusSlaveNetwork.ListenAsync();

               await Task.Delay(1);

               //how to capture the request from the master?
Gerry Schmitz 4-Aug-23 13:06pm    
The "PC app" is the master (device). The slaves can be real devices or simulated using software (instances of a device class).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900