BOOL CSerial::OpenCommPort(DWORD dwPort){ if(m_hCom != INVALID_HANDLE_VALUE) { return FALSE; }
strcpy(m_szComm, "\\\\.\\COM"); char sz[5]; ltoa(dwPort, sz, 10);
strcat(m_szComm, sz);//
WCHAR wstr[MAX_PATH]; memset(wstr, 0, MAX_PATH); MultiByteToWideChar(CP_ACP, 0, m_szComm, strlen(m_szComm), wstr, sizeof(m_szComm));
m_hCom = ::CreateFile(wstr, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if(m_hCom == INVALID_HANDLE_VALUE) { return FALSE; }
DWORD dwId; m_bExit = FALSE; m_hThread = ::CreateThread(NULL, 0, SerialThreadProc, this, 0, &dwId); if(m_hThread == NULL) { ::CloseHandle(m_hCom); m_hCom = INVALID_HANDLE_VALUE; // return FALSE; }
// here to return TRUE;}