示例#1
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            bool bRet;

            if (m_bStart) // was started
            {
                panelPID.Enabled = false;
                m_bStart         = false;
                timer1.Enabled   = false;
                adamCom.CloseComPort();
                buttonStart.Text = "Start";
            }
            else
            {
                if (adamCom.OpenComPort())
                {
                    // set COM port state, 9600,N,8,1
                    adamCom.SetComPortState(Baudrate.Baud_9600, Databits.Eight, Parity.None, Stopbits.One);
                    // set COM port timeout
                    adamCom.SetComPortTimeout(500, 500, 0, 500, 0);
                    m_iCount = 0; // reset the reading counter
                    //
                    if (cbxLoop.SelectedIndex >= 0)
                    {
                        RefreshPIDValue();
                    }
                    else
                    {
                        // PID
                        m_loopBase = PID_Loop.Loop0;
                        if (m_AsciiProtocol)
                        {
                            bRet = (adamCom.Pid(m_iAddr).AsciiRefreshBuffer(PID_Loop.Loop0) && adamCom.Pid(m_iAddr).AsciiRefreshBuffer(PID_Loop.Loop1));
                        }
                        else
                        {
                            bRet = (adamCom.Pid(m_iAddr).ModbusRefreshBuffer(PID_Loop.Loop0) && adamCom.Pid(m_iAddr).ModbusRefreshBuffer(PID_Loop.Loop1));
                        }
                        if (bRet)
                        {
                            m_pv1LblHigh[0] = adamCom.Pid(m_iAddr).GetBufferFloat(PID_Loop.Loop0, PID_Addr.Sv1HighLimit);
                            m_pv1LblHigh[1] = adamCom.Pid(m_iAddr).GetBufferFloat(PID_Loop.Loop1, PID_Addr.Sv1HighLimit);
                            m_pv1LblLow[0]  = adamCom.Pid(m_iAddr).GetBufferFloat(PID_Loop.Loop0, PID_Addr.Sv1LowLimit);
                            m_pv1LblLow[1]  = adamCom.Pid(m_iAddr).GetBufferFloat(PID_Loop.Loop1, PID_Addr.Sv1LowLimit);
                            m_pv2LblHigh[0] = adamCom.Pid(m_iAddr).GetBufferFloat(PID_Loop.Loop0, PID_Addr.Sv2HighLimit);
                            m_pv2LblHigh[1] = adamCom.Pid(m_iAddr).GetBufferFloat(PID_Loop.Loop1, PID_Addr.Sv2HighLimit);
                            m_pv2LblLow[0]  = adamCom.Pid(m_iAddr).GetBufferFloat(PID_Loop.Loop0, PID_Addr.Sv2LowLimit);
                            m_pv2LblLow[1]  = adamCom.Pid(m_iAddr).GetBufferFloat(PID_Loop.Loop1, PID_Addr.Sv2LowLimit);
                            RefreshPIDStatic();
                        }
                        else
                        {
                            MessageBox.Show("Failed to refresh data!", "Error");
                            adamCom.CloseComPort();
                            return;
                        }
                    }
                    //
                    panelPID.Enabled = true;
                    timer1.Enabled   = true; // enable timer
                    buttonStart.Text = "Stop";
                    m_bStart         = true; // starting flag
                }
                else
                {
                    MessageBox.Show("Failed to open COM port!", "Error");
                }
            }
        }