コード例 #1
0
ファイル: Form1.cs プロジェクト: ronghaoZHI/about-labview
        private void timer1_Tick(object sender, System.EventArgs e)
        {
            int iIdx;

            int[]  iData;
            bool[] bData;

            if (m_bRegister)             // Read registers (4X references)
            {
                // read register (4X) data from slave
                if (adamCom.Modbus(m_iAddr).ReadHoldingRegs(m_iStart, m_iLength, out iData))
                {
                    m_iCount++;                     // increment the reading counter
                    txtStatus.Text = "Read registers " + m_iCount.ToString() + " times...";
                    // update ListView
                    for (iIdx = 0; iIdx < m_iLength; iIdx++)
                    {
                        listViewModbusCur.Items[iIdx].SubItems[2].Text = iData[iIdx].ToString();                                // show value in decimal
                        listViewModbusCur.Items[iIdx].SubItems[3].Text = iData[iIdx].ToString("X04");                           // show value in hexdecimal
                    }
                }
                else
                {
                    txtStatus.Text = "Read registers failed!";                     // show error message in "txtStatus"
                }
            }
            else
            {
                // read coil (0X) data from slave
                if (adamCom.Modbus(m_iAddr).ReadCoilStatus(m_iStart, m_iLength, out bData))
                {
                    m_iCount++;                     // increment the reading counter
                    txtStatus.Text = "Read coil " + m_iCount.ToString() + " times...";
                    // update ListView
                    for (iIdx = 0; iIdx < m_iLength; iIdx++)
                    {
                        listViewModbusCur.Items[iIdx].SubItems[2].Text = bData[iIdx].ToString();                                // show value
                    }
                }
                else
                {
                    txtStatus.Text = "Read coil failed!";                     // show error message in "txtStatus"
                }
            }
        }