#define MAX 10
//可指定为更大,但我想没哪个机器有超过10个串口的,2个就够不错了
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
using namespace std;typedef vector<string> CommPorts;void GetCommPorts( CommPorts& _ac );int main( int argc, char* argv[] )
{
  CommPorts cps;
  GetCommPorts(cps);
  if( 0 == cps.size() )
  {
    cout << "No available serialports" << endl;
  }
  else
  {
    cout << "There are " << cps.size() << ( cps.size() == 1 ? " serialport" : " serialports" )
         << " in your computer!" << endl;
    for(int i = 0 ; i < cps.size(); i++)
    {
      cout << cps[ i ] << endl;
    }
  }
  system( "PAUSE" );
  return 0;
}void GetCommPorts( CommPorts& _ac )
{
char _coms[ 10 ] = "COM";

for(int i = 1; i < MAX; i++)
{
char ID[ 3 ] = "1";
_ltoa( i, ID, 10 );
strcat( _coms,ID );

HANDLE _hCommHandle = CreateFile(
_coms, 
GENERIC_READ | GENERIC_WRITE,  //读写兼之
0,
NULL,   //
OPEN_EXISTING,  //操作串口,必须指定为该值
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL //操作串口,必须指定为该值
);  //该函数打开串口成功会返回一非零的句柄

if( ERROR_FILE_NOT_FOUND == GetLastError() ) //如果创建句柄成功,则返回0
{   //ERROR_FILE_NOT_FOUND is a long integer value 2
break;
}

if( _hCommHandle )   //如果句柄非零,就将串口名字压入vector
{
string _tmp(_coms);
_ac.push_back(_tmp);
CloseHandle( _hCommHandle );
strcpy( _coms,"COM" );
}  //End if
}//End for
}//End member function GetAvailableCommPort发此贴,共分享!有比我更好的方法的请顶贴,谢谢赐教!

解决方案 »

  1.   

    10个以上串口,你做工控或者楼控就很容易碰到。
    建议 "COM1" 之类的文件名改为 "\\\\.\\COM1"
      

  2.   

    谢谢啊
    我再改进下#define MAX 100   
    //可指定为更大,但我想没那个机器有超过10个串口的,2个就够不错了#include <iostream>
    #include <windows.h>
    #include <string>
    #include <vector>
    using namespace std;typedef vector<string> CommPorts;void GetCommPorts(CommPorts& _ac);int main(int argc, char* argv[])
    {
      CommPorts cps;
      GetCommPorts(cps);
      if( 0 == cps.size() )
      {
        cout << "No available serialports" << endl;
      }
      else
      {
        cout << "There are " << cps.size() << ( cps.size() == 1 ? " serialport" : " serialports" )
             << " in your computer!" << endl;
        for(int i = 0 ; i < cps.size(); i++)
        {
          cout << cps[ i ].substr( 4 ) << endl;
        }
      }
      system("PAUSE");
      return 0;
    }void GetCommPorts(CommPorts& _ac)
    {
    char _coms[10] = "\\\\.\\COM";

    for(int i = 1; i < MAX; i++)
    {
    char ID[3] = "1";
    _ltoa(i, ID, 10);
    strcat(_coms,ID);

    HANDLE _hCommHandle = CreateFile(
    _coms, 
    GENERIC_READ | GENERIC_WRITE,  //读写兼之
    0,
    NULL,   //
    OPEN_EXISTING,  //操作串口,必须指定为该值
    FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 
    NULL //操作串口,必须指定为该值
    );  //该函数打开串口成功会返回一非零的句柄

    if(ERROR_FILE_NOT_FOUND == GetLastError()) //如果创建句柄成功,则返回0
    {   //ERROR_FILE_NOT_FOUND is a long integer value 2
    strcpy(_coms,"\\\\.\\COM");
    continue;//此处不能break
    }

    if( _hCommHandle )   //如果句柄非零,就将窗口名字压入vector
    {
    string _tmp(_coms);
    _ac.push_back(_tmp);
    CloseHandle( _hCommHandle );
    strcpy(_coms,"\\\\.\\COM");
    }  //End if
    }//End for
    }//End member function GetAvailableCommPort我发现配有笔记本上面只有一COM3于是那1楼的去测试,显示没有可用串口。一看是
    break出现问题了!
    把它改为continue,在在 continue之前加上strcpy(_coms, "\\\\.\\COM")测试通过了