HS Codebank Article
Back
Read Multiple Coils Using Modbus

Author: Admin 06/06/2021
Language: C#
Views: 5


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);
            }
        }


Back
Comments
Add Comment
There are no comments yet.