mfc 搜索电脑所有可用串口数



void scan_all_uart(u8 *com_port_table)
{
    int i;
    HANDLE m_hComm;
    DWORD dwCreateProperty;
    //Handle to the comms port
    int nPort = 0x00;
    DWORD dwError = 0x00;
    //Call CreateFile to open up the comms port
    CString sPort;
    dwCreateProperty = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;

    for( i=0x00; i<50; i++ ){
         sPort.Format(_T("\\\\.\\COM%d"), nPort);
         m_hComm = CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, dwCreateProperty, NULL);
         if( m_hComm == INVALID_HANDLE_VALUE ){
             dwError = ::GetLastError();
             if( dwError == 2 ){
                 //机器上没有这个串口
             }else if( dwError == 5 ){
                 TRACE("nPort1 = %d.\n", nPort);
                 com_port_table[nPort] = 0x02;
                 //有这个串口,但是已经被打开
             }
         }else{
             //能够正常打开
             TRACE( "m_hComm = 0x%x.\n", m_hComm );
             TRACE("nPort2 = %d.\n", nPort);
             com_port_table[nPort] = 0x01;
             BOOL bSuccess = CloseHandle(m_hComm);
             m_hComm = INVALID_HANDLE_VALUE;
             if( !bSuccess ){
                 TRACE(_T("Failed to close up the comms port, GetLastError:%d\n"), GetLastError());
             }
         }
         nPort++;
    }
}






void Cfile_extractDlg::OnBnClickedButton10()

    int i;
    u8 com_port_table[50];
    memset(com_port_table,0x00,sizeof(com_port_table));
    scan_all_uart(com_port_table);
    for(i=0x00; i<50; i++){
       if(com_port_table[i]){
          TRACE("com_port_table[%d] = %d.\n", i, com_port_table[i]);
  }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值