win32 시리얼 포트 목록 얻기
c#이나 .net framework을 쓰면 간편한데
win32로는 영 복잡할수 밖에 없는 듯?
그런데 QueryDosDevice()를 쓰면 0ms 라는데
|
[링크 : http://stackoverflow.com/.../how-do-i-get-a-list-of-available-serial-ports-in-win32]
m_MyPort 변수만 다른걸로 바꿔써주면 잘 작동 하는 듯
단, unicode 기반에서는 (LPSTR)대신 (LPWSTR)로 해주어야 에러가 나지 않는다.
void SelectComPort() //added function to find the present serial
{
TCHAR lpTargetPath[5000]; // buffer to store the path of the COMPORTS
DWORD test;
bool gotPort=0; // in case the port is not found
for(int i=0; i<255; i++) // checking ports from COM0 to COM255
{
CString str;
str.Format(_T("%d"),i);
CString ComName=CString("COM") + CString(str); // converting to COM0, COM1, COM2
test = QueryDosDevice(ComName, (LPSTR)lpTargetPath, 5000);
// Test the return value and error if any
if(test!=0) //QueryDosDevice returns zero if it didn't find an object
{
m_MyPort.AddString((CString)ComName); // add to the ComboBox
gotPort=1; // found port
}
if(::GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
lpTargetPath[10000]; // in case the buffer got filled, increase size of the buffer.
continue;
}
}
if(!gotPort) // if not port
m_MyPort.AddString((CString)"No Active Ports Found"); // to display error message incase no ports found
}
[링크 : http://stackoverflow.com/.../what-is-proper-way-to-detect-all-available-serial-ports-on-windows]