系统自动,自动检测机器串口,下面的程序怎么都能检测到,但实际上没有那么多串口,该怎么检测啊,大侠帮我看看,多谢
HANDLE hComm;
hComm = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (!hComm)
{
AfxMessageBox("COM1 in not existed or be used !");
}
else
{
fgComm = 1;
m_ComboxSerial.AddString("COM1");
CEdit *pEdit = (CEdit*)GetDlgItem(IDC_COMBO_SERIAL);
pEdit->SetWindowText("COM1");
CloseHandle(hComm);
}

解决方案 »

  1.   

    从注册表中取得com的设置情况int CUtil::GetSystemPorts(CString *pNameList, CString *pPortList)
    {
    CRegKey RegKey;
    int nCount = 0;

    if(RegKey.Open(HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm") == ERROR_SUCCESS)
    {
    while(true)
    {
    char ValueName[_MAX_PATH];
    unsigned char ValueData[_MAX_PATH];
    DWORD nValueSize = _MAX_PATH;
    DWORD nDataSize = _MAX_PATH;
    DWORD nType;

    if(::RegEnumValue(HKEY(RegKey), nCount, ValueName, &nValueSize, NULL, &nType, ValueData, &nDataSize) == ERROR_NO_MORE_ITEMS)
    {
    break;
    }

    if(pNameList)
    pNameList[nCount] = ValueName;

    if(pPortList)
    pPortList[nCount] = ValueData;

    nCount++;
    }
    }

    return nCount;
    }