Exemple #1
0
        private void handleModbusReadRequest(ushort startAdd, ushort nReg, ushort [] rxBuf)
        {
            int rxReg = -1;

            try
            {
                rxReg = mrAccess.readModbusRegister(startAdd, nReg, rxBuf);
                if (rxReg == -1)
                {
                    switch (rxBuf[0])
                    {
                    case 1:
                        lblMBException.Content = "ILLEGAL FUNCTION";
                        break;

                    case 2:
                        lblMBException.Content = "ILLEGAL DATA ADDRESS";
                        break;

                    case 3:
                        lblMBException.Content = "ILLEGAL DATA VALUE";
                        break;

                    case 4:
                        lblMBException.Content = "SLAVE DEVICE FAILURE";
                        break;

                    case 6:
                        lblMBException.Content = "SLAVE DEVICE BUSY";
                        break;

                    default:
                        lblMBException.Content = rxBuf[0].ToString();
                        break;
                    }
                }
                else
                {
                    lblMBException.Content = "Success !";
                    modbusResponseGrid[] msGrid = new modbusResponseGrid[nReg];
                    for (int i = 0; i < nReg; i++)
                    {
                        msGrid[i]        = new modbusResponseGrid();
                        msGrid[i].regAdd = (startAdd + i).ToString();
                        msGrid[i].val    = rxBuf[i].ToString();
                        msGrid[i].valHex = utility.U16ToHex(rxBuf[i]);
                        grdModbus.Items.Add(msGrid[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 private void txtEnterValDone_KBLostFocus(object sender, KeyboardFocusChangedEventArgs e)
 {
     if (utility.validateIntegerTextBox(txtVal.Text.ToString()) == false)
     {
         MessageBox.Show("please enter only 0-9 integer value");
         txtVal.Text = "0";
     }
     else
     {
         if (txtVal.Text.ToString() != "")
         {
             ushort val = Convert.ToUInt16(txtVal.Text);
             txtValHex.Text = utility.U16ToHex(val).ToString();
         }
     }
 }