void EnumSerialPort()
{
for(INT iPort=1; iPort<32; iPort++)
{
TCHAR szPortName[64];
_stprintf_s(szPortName, _T("\\\\.\\COM%d"), iPort);
HANDLE hComPort = CreateFile(szPortName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
BOOL bDone = FALSE;
if(hComPort != INVALID_HANDLE_VALUE)
{
CloseHandle(hComPort);
bDone = 1;
}
else if(GetLastError() != ERROR_FILE_NOT_FOUND)
{
bDone = 2;
}
if(bDone)
{
TRACE(_T("COM%d%s\n"), iPort,
bDone==2? _T("(In using)"):_T(""));
}
}
}
//调试输出
COM1
COM7
COM8(In using) |