HS Codebank Article
Back
Read Multiple Registers Using Modbus

Author: Admin 06/01/2021
Language: C#
Views: 13


Description:

Read multiple registers using the FieldTalk Modbus Master Library.

Article:

private int Poll_Result;

private void ReadInputs()
        {
            OpenConnection();

            try
            {
                lblResult.Text = "";

                float[] W_G_Vals = new float[Convert.ToInt16(tbNumOfRegisters.Text)];

                Poll_Result = myProtocol.readMultipleRegisters(1, 1, W_G_Vals, 10);

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

                W_G_Vals = null;

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

Modbus Protocol Software (modbusdriver.com)


Back
Comments
Add Comment
There are no comments yet.