writefile()指令之后,为什么会收到两次EV_RXCHAR事件?按道理应该收到一次EV_RXCHAR事件,可是收到两次EV_RXCHAR事件
这到底是什么原因???
- ...
- for(;;)
- {
- bResult=WaitCommEvent(m_hCom,&hMask,&ReadOver);
- if(OpenCloseFlag==1) //串口已被打开才做下一部
- {
- if(!bResult)
- {
- switch(dwErr=GetLastError())
- {
- case ERROR_IO_PENDING:
- Flag^=1;
- break;
- /*case 87:
- break;*/
- default:
- SetDlgItemText((HWND)lparam,IDC_COMMSTATE,"WaitCommEvent Error");
- goto ErrorNext;
- }
- }else
- {
- bResult=ClearCommError(m_hCom,&dwErr,&comstat);
- if(comstat.cbInQue==0)
- continue;
- }
- WaitForSingleObject(ReadOver.hEvent,INFINITE);///等侍异步操作完成
- Flag=0;
- GetCommMask(m_hCom,&hMask);
- if(hMask & EV_RXCHAR)
- {
- EnterCriticalSection(§ion);
- bResult=ClearCommError(m_hCom,&dwErr,&comstat);
- if(!ReadFile(m_hCom,pszRecv,100,&ReadBytes,&ReadOver))
- {
- switch(dwErr=GetLastError())
- {
- case ERROR_IO_PENDING:
- Flag^=1;
- break;
- default:
- goto ErrorNext;
- }
- }
-
- if(Flag & 1)
- {
- SetDlgItemText((HWND)lparam,IDC_COMMSTATE,"正在读取数据");
- WaitForSingleObject(ReadOver.hEvent,INFINITE);
-
- if(GetOverlappedResult(m_hCom,&ReadOver,&ReadBytes,TRUE))
- {
- SetDlgItemText((HWND)lparam,IDC_COMMSTATE,"读取数据完成");
- SetDlgItemText((HWND)lparam,IDC_SHOWINFO,pszRecv);
- }
- }
-
-
- LeaveCriticalSection(§ion);
-
- }
- ErrorNext:
- Flag=0;
- PurgeComm(m_hCom,PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR);
-
- }
-
- }
- return 1;
- }
- ...