获取PC可用串口端口,并将其在combo box中显示

通常应用中,我会遇到需要用到多个串口的同时在一台PC上使用的情况,例如通过USB转串口等方式来硬件接入串口数目。

那么软件需要判断到底哪些串口号是可用的,才可以初始化相应的串口应用程序。

下面我们简单介绍一下,如何获取PC可用串口端口号,并将其显示在combo box控件中。

例程我们搜寻最多2个可用串口端口。

  1. 创建工程,在UI上添加如下控件:
  2. 获取PC可用串口端口,并将其在combo box中显示_第1张图片
  3. 因为我们需要用到两个串口,所以我们添加两个串口通信插件communications control(如果不需要操作串口,可不用).
  4. 添加按键事件处理函数 OnBnClickedStart 
  5. 函数处理:
    void CMicrohardTestDlg::OnBnClickedStart()
    {
        // TODO: Add your control notification handler code here
        int i;
        CMscomm1 *pMsc = (CMscomm1*)this->GetDlgItem(IDC_MSCOMM1);
        CComboBox *pcombo = (CComboBox*)this->GetDlgItem(IDC_COMBO1);
        switch (g_w_Steps)
        {
        case 0://check serial port by openning produrce.
        {
                   HANDLE hCom;
                   CString strCOM;;
                   m_Edit1_show = "";
                   pMsc->put_Settings(_T("115200,n,8,1"));
                   pcombo->ResetContent();//clear context
                   for (i = 0; i < 256; i++)
                   {
                       strCOM.Format(_T("COM%d"), i);
                       //open or Create a file or an I/O
                       hCom = CreateFile(_T("\\\\.\\") + strCOM, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
                       if (hCom != INVALID_HANDLE_VALUE)
                       {
                           if (m_Edit1_show.IsEmpty())
                           {
                               pcombo->AddString(strCOM);
                               pcombo->SetCurSel(0);
                               pcombo = (CComboBox*)this->GetDlgItem(IDC_COMBO2);
                               m_Edit1_show = "PC Seral Ports Searching ...\r\nFirst Port was found.\r\n";
                           }
                           else
                           {
                               pcombo->AddString(strCOM);
                               pcombo->SetCurSel(0);
                               m_Edit1_show += "Second Port was found.\r\n";
                           }
                       }
                   }
                   if (m_Edit1_show.IsEmpty())
                   {
                       m_Edit1_show = "No PC Serial Port active.";
                   }
                   UpdateData(false);
                   g_w_Steps++;
                  break;
        }
        case 1:
        {
                  break;
        }
        default:
            break;
        }
    }
    OnBnClickedStart

     

  6. 这里主要函数为
    //open or Create a file or an I/O
     hCom = CreateFile(_T("\\\\.\\") + strCOM, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
                       

    返回值不为 INVALID_HANDLE_VALUE 则说明串口端口可用。

  7. 到目前为止,我们活跃的串口已经打开了,但是我们并没有设置串口参数。那么我们需要先关有效的端口。更改后为:
    void CMicrohardTestDlg::OnBnClickedStart()
    {
        // TODO: Add your control notification handler code here
        int i,num;
        CString str,num_str;
        CMscomm1 *pMsc;
        CComboBox *pcombo;
        switch (g_w_Steps)
        {
        case 0://check serial port by openning produrce.
        {
                   //point to combo box1
                   pcombo = (CComboBox*)this->GetDlgItem(IDC_COMBO1);
                   //clear context
                   pcombo->ResetContent();
                   num = 0;
                   m_Edit1_show += _T("PC Serial Ports Searching ...\r\n");
                   for (i = 0; i < 256; i++)
                   {
                       str.Format(_T("COM%d"), i);
                       //open or Create a file or an I/O
                       HANDLE hCom = CreateFile(_T("\\\\.\\") + str, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
                       if (hCom != INVALID_HANDLE_VALUE)
                       {
                           num++;
                           pcombo->AddString(str);
                           pcombo->SetCurSel(0);
                           //point to combo box2
                           pcombo = (CComboBox*)this->GetDlgItem(IDC_COMBO2);
                           m_Edit1_show += str + _T(" was found.\r\n");
                       }
                       CloseHandle(hCom);
                       if (num >= 2)break;
                   }
                   if (num >= 2)
                   {
                       SetDlgItemText(IDC_BUTTON1, _T("OpenPorts"));
                       g_w_Steps++;
                   }
                   else
                   {
                       str.Format(_T("%d Port are active, we need at least 2 Port Active.Please re-try.\r\n"), num);
                       m_Edit1_show += str;
                   }
                   UpdateData(false);
                  break;
        }
        case 1:
        {
                  pMsc = (CMscomm1*)this->GetDlgItem(IDC_MSCOMM1);
                  pcombo = (CComboBox*)this->GetDlgItem(IDC_COMBO1);
                  //open 2 serial port
                  for (i = 0; i < 2; i++)
                  {
                      if (pMsc->get_PortOpen() == TRUE)
                      {
                          pMsc->put_PortOpen(FALSE);
                      }
                      //get combo box text--> str
                      pcombo->GetLBText(0, str);
                      //get str string num-->e.g "COM1235"==> num_str="1235"
                      num_str = str.Mid(3);
                      //get num from string num-->e.g "1235"==>num=1235
                      num = _ttoi(num_str);
                      pMsc->put_CommPort(num);
                      pMsc->put_Settings(_T("115200,n,8,1"));
                      pMsc->put_PortOpen(TRUE);
                      if (pMsc->get_PortOpen() == TRUE)
                      {
                          m_Edit1_show += str + _T(" was Opened Success. Configuration: 115200,n,8,1\r\n");
                      }
                      pMsc = (CMscomm1*)this->GetDlgItem(IDC_MSCOMM2);
                      pcombo->EnableWindow(FALSE);
                      pcombo = (CComboBox*)this->GetDlgItem(IDC_COMBO2);
                  }
                  SetDlgItemText(IDC_BUTTON1, _T("Login"));
                  UpdateData(false);
                  g_w_Steps++;
                  break;
        }
        case 2:
        {
                  m_Edit1_show += _T("");
                  break;
        }
        default:
            break;
        }
    }
    OnBnClickedStart

    这里我们调用 CloseHandle(hCom); 来关闭相应的端口。

谢谢。

End.

你可能感兴趣的:(获取PC可用串口端口,并将其在combo box中显示)