1

I am developing a windows form to function as a modbus tcp Master Simulator.I am using NModbus library. I want to connect to multiple slaves simultaneously and do the read and write operation,does the NModbus library supports this kind of implementation? and if so how?. Currently i am able to connect to single slave device and do the read/write operations,but i am stuck on how to the same with multiple slaves though. Should i use the threading Concept to achieve the same. Here is my code to connect to single slave device and do the read/write operation.

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                TcpClient masterTcpClient = new TcpClient(txtIP.Text, 502);
                master = ModbusIpMaster.CreateIp(masterTcpClient);
                MessageBox.Show("Connected");

            }
            catch (SystemException error)
            {
                MessageBox.Show(error.Message);
            }

        }

        private void btnReadCoil_Click(object sender, EventArgs e)
        {
            try
            {

                byte slaveID = 255;
                ushort startAddress = Convert.ToUInt16(txtStartaddress.Text);
                ushort numInputs = Convert.ToUInt16(txtSize.Text);
                bool[] inputs = master.ReadCoils(slaveID, startAddress, numInputs);
                AnswerFromServer.Items.Clear();
                for (int i = 0; i < inputs.Length; i++)
                {
                    AnswerFromServer.Items.Add(Convert.ToInt16(inputs[i]));
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Exception Reading values from Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void btnWriteSingleRegister_Click(object sender, EventArgs e)
        {
            try
            {

                byte slaveID = 255;
                ushort RegisterAddress = Convert.ToUInt16(txtStartaddress.Text);
                ushort value = Convert.ToUInt16(txtSingleValue.Text);
                master.WriteSingleRegister(slaveID, RegisterAddress, value);

            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Exception writing values to Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

Can any one please help me with this one?

Harshith R
  • 418
  • 1
  • 11
  • 31
  • 1
    The source link for the source code is below. I looked at the code to find static object and the id : private static byte ModbusId; is static which will cause issue with multiple instances of the code. To make code run multiple times you would have to remove the static and then use instances of the class.https://github.com/sankarr/Nmodbus/blob/master/Modbus/Device/ModbusIpMaster.cs – jdweng Oct 03 '18 at 09:37
  • @jdweng I tried Creating the instances of the class and it is Working .Thanks! – Harshith R Oct 04 '18 at 08:41
  • Can you share the code you modified ? – mrid May 27 '22 at 06:17

1 Answers1

0

create another instance of this socket and maintain it.

TcpClient masterTcpClient_Two = new TcpClient(txtIPTwo.Text, 502);
dbc
  • 104,963
  • 20
  • 228
  • 340