HS Banner
Back
Read Multiple Coils Using Modbus

Author: Admin 06/06/2021
Language: C#
Tags:


Description:

Read multiple coils using the FieldTalk Modbus Master Library.

Article:

private void ReadCoil()
        {
            OpenConnection();

            try
            {
                lblResult.Text = "";

                bool[] ReadCoils = new bool[Convert.ToInt16(tbNumOfRegisters.Text)];

                Poll_Result = myProtocol.readCoils(1, 1, ReadCoils, Convert.ToInt16(tbNumOfRegisters.Text));

                if (Poll_Result == BusProtocolErrors.FTALK_SUCCESS)
                {
                    for (int i = 0; i < ReadCoils.Length; i++)
                    {
                        lblResult.Text += "Ref: " + (Convert.ToInt16(tbRefNumber.Text) + i) + "  Value:  " + Convert.ToString(ReadCoils[i]) + Environment.NewLine;
                    }
                }
                else
                {
                    lblResult.Text = "Error was: " + BusProtocolErrors.getBusProtocolErrorText(Poll_Result);
                }

                ReadCoils = null;

                //Close connection
                if (myProtocol.isOpen())
                    myProtocol.closeProtocol();
                myProtocol = null;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

Read More: https://www.modbusdriver.com/doc/mbusmaster.net/index.htm



Back
Comments
Add Comment
There are no comments yet.