Beispiel #1
0
        /// <summary>
        /// Read Coils.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonCoilRead_Click(object sender, EventArgs e)
        {
            try
            {
                // Get the selected device address.
                byte slaveAddr = GetDeviceAddress();

                // If the register address text box is empty...
                if (string.IsNullOrWhiteSpace(textBoxCoilAddressDecimal.Text))
                {
                    // Convert from hex.
                    TextBoxCoilAddressHex_Leave(null, null);
                }

                // Get the selected register address.
                ushort regAddr = GetRegisterAddress(textBoxCoilAddressDecimal.Text);

                // Read the selected coil.
                bool[] coils = _mbMaster.ReadCoils(slaveAddr, regAddr, 1);

                // Show the value of the coil as true or false.
                if (coils.Length != 0)
                {
                    if (coils[0] == true)
                    {
                        radioButtonCoilFalse.Checked = false;
                        radioButtonCoilTrue.Checked  = true;
                    }
                    else
                    {
                        radioButtonCoilTrue.Checked  = false;
                        radioButtonCoilFalse.Checked = true;
                    }
                }

                // Update the status.
                toolStripStatusLabel1.Text = "Coil Read Successfully";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().Name.ToString());
            }
        }