VC得到可用的串口列表

来源:互联网 发布:淘宝描述的图片尺寸 编辑:程序博客网 时间:2024/06/06 15:04
//枚举串口//参数:bEnablePort,哪个串口有效,bEnablePort[0]表示COM1,bEnablePort[n-1]表示COMn//返回值:有效的串口个数int EnumAllComPort(bool* bEnablePort){    int nCommSum = 0;//串口个数    HANDLE hCom;    CString str;    for(int i=1;i<=256;i++)    {        str.Format(_T("COM%d"),i);        hCom = CreateFile(str, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);        if (hCom == INVALID_HANDLE_VALUE)        {            bEnablePort[i-1] = false;            continue;        }        else        {            bEnablePort[i-1] = true;        }        CloseHandle(hCom);        nCommSum++;    }    return nCommSum;}