m_SerialPort.InitPort()串口初始化时没有找到串口 直接返回0了
OpenPort:
int nPort=m_ctrlComPort.GetCurSel()+1;
if (m_SerialPort.InitPort(this,nPort,9600,'N',8,1,EV_RXFLAG|EV_RXCHAR,512))
{
m_SerialPort.StartMonitoring();
m_bPortOpen=true;
}
else
{
AfxMessageBox(_T("not found the com"));
m_bPortOpen=false;
}
InitPort():部分代码
EnterCriticalSection(&m_csCommunicationSync);
//if the port is already opened: close it
if(m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
// prepare port strings
sprintf(szPort,"COM%d",portnr);
sprintf(szBand,"baud=%d parity=%c data=%d stop=%d",baud,parity,databits,stopbits);
//get a handle to the port
m_hComm=CreateFile((LPCWSTR)szPort, //communication port string (COMX)
GENERIC_READ|GENERIC_WRITE, //read/write types
0, //comm devices must be opened exclusive access
NULL, //no security attributes
OPEN_EXISTING, //comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, //Asyns I/O
0); //template must be 0 for comm devices
if (m_hComm==INVALID_HANDLE_VALUE)
{
//NOT FOUND PORT
DWORD dwError=GetLastError();
return FALSE;
}