串口异步读写问题
DWORD WINAPI SerialPort1ThreadProcess( HWND hWnd) //主窗口句柄
{
DWORD dwEvtMask = 0;
OVERLAPPED osRead = {0};
CSerialPortAPIDlg *pdlg = (CSerialPortAPIDlg*)CWnd::FromHandle(hWnd);
WaitCommEvent(pdlg->hCom, &dwEvtMask, &osRead);
SetCommMask(pdlg->hCom, EV_RXCHAR| EV_TXEMPTY);
if((dwEvtMask&EV_RXCHAR)==EV_RXCHAR)
{
COMSTAT comstat;
DWORD dwlength;
DWORD dwbytesread; //读取的字节数
DWORD dwerror;
DWORD *lpRead=NULL;
DWORD dwerrorflags;
ClearCommError(pdlg->hCom, &dwerrorflags, &comstat);
dwlength = comstat.cbInQue;
if(dwlength>0)
{
BOOL freadstat;
freadstat = ReadFile(pdlg->hCom, lpRead, dwlength, &dwbytesread, &osRead);
if(! freadstat)
{
if(GetLastError()==ERROR_IO_PENDING)
{
while(!GetOverlappedResult(pdlg->hCom, &osRead, &dwbytesread, true))
{
dwerror=GetLastError();
if(dwerror==ERROR_IO_INCOMPLETE)
continue;
::PostMessage((HWND)hWnd, COM_RECVDATA, (unsigned int)lpRead, dwbytesread);
}
}
}
}
}
return true;
}
初学,现在程序编译通过,但是读写还是不成功,郁闷,因为比较急就请大家帮下忙,谢谢!