1. 程式人生 > >串列埠自動檢測

串列埠自動檢測

 //檢測可用串列埠,並增加到comboBox_PortName
        private void CheckAndAddAvailableSerialPort()
        {
            bool flag = true;
            comboBox_PortName.Items.Clear();
            for (int i = 1; i < 20; i++)
            {
                try
                {
                    serialPort1.PortName = "COM" + i;
                    serialPort1.Open();
                    serialPort1.Close();
                    comboBox_PortName.Items.Add("COM" + i);    
                    if(flag)
                    {
                        flag = false;
                        comboBox_PortName.Text = serialPort1.PortName; 
                    }
                }
                catch 
                { }
               
            }
        }
//////////////////////////////////////////////////////////////////////////////////////////////
        int availableSerialPortNumber;
        //檢測可用串列埠數量,返回availableSerialPortNumber
        private int CheckAvailableSerialPortNumber()
        {
            availableSerialPortNumber=0;
            //comboBox_PortName.Items.Clear();
            for (int i = 1; i < 20; i++)
            {
                try
                {
                    serialPort1.PortName = "COM" + i;
                    serialPort1.Open();
                    serialPort1.Close();
                    availableSerialPortNumber++;
                }
                catch
                { }
            }
            return availableSerialPortNumber;
        }
<pre name="code" class="csharp">//////////////////////////////////////////////////////////////////////////////////////////////

        //當串列埠數量變化時,
        static int previousAvailableSerialPortNumbe=0;
        private void timer1_Tick(object sender, EventArgs e)
        {        
            int currentAvailableSerialPortNumber = CheckAvailableSerialPortNumber();
            if (previousAvailableSerialPortNumbe != currentAvailableSerialPortNumber)
            {
                CheckAndAddAvailableSerialPort();
                previousAvailableSerialPortNumbe = currentAvailableSerialPortNumber;
            }
        }
<pre name="code" class="csharp">//////////////////////////////////////////////////////////////////////////////////////////////

        //開啟串列埠
        private void Btn_OpenSerialport_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    timer1.Enabled = true;                         //開啟timer1
                    serialPort1.Close();
                    Btn_OpenSerialport.Text = "開啟串列埠";    
                }
                else
                {
                    timer1.Enabled = false;                         //<span style="font-family: Arial, Helvetica, sans-serif;">關閉timer1</span>
                    serialPort1.BaudRate = Convert.ToInt32(comboBox_BandRate.Text);
                    serialPort1.PortName = comboBox_PortName.Text;
                    serialPort1.Open();
                    Btn_OpenSerialport.Text = "關閉串列埠";
                }
            }
            catch { MessageBox.Show("串列埠開啟失敗!"); }

            //是否連線資料庫
            if(checkBox_ConnectDataBase.Checked==true)
            {
                ConnectDataBase();
            }
        
        }