串口发送程序可以收发数据了,就是占CPU太多了,这是何故?
串口发送程序可以收发数据了,就是占CPU太多了,这是何故?发现一运行接收数据的函数就会占CPU,我的代码如下,我是通过一个线程调用一个死循环来检测是否收到数据的。
DWORD WINAPI ComRead(HWND hWnd)
{
char ReadBuff[101]="";
DWORD ReadLength1=0;
DWORD ReadLength2=0;
DWORD ReadFlag;
OverCom.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
while(1)
{
SetCommMask(hdCom,EV_RXCHAR);
WaitCommEvent(hdCom,&ComEvent,&OverCom);
if((ComEvent&EV_RXCHAR)==EV_RXCHAR)
{
ClearCommError(hdCom,&ComError,&ComStat);
ReadLength1=ComStat.cbInQue;
if(ReadLength1>0)
{
ReadFlag=ReadFile(hdCom,&ReadBuff,ReadLength1,&ReadLength2,&OverCom);
ReadBuff[ReadLength2]='\0';
if(!ReadFlag)
{
if(GetLastError()==ERROR_IO_PENDING)
{
while(!GetOverlappedResult(hdCom,&OverCom,&ReadLength2,TRUE ))
{
ComError=GetLastError();
if(ComError==ERROR_IO_PENDING)continue;
}
}
}
}
::PostMessage(hWnd,COM_READ,(unsigned int)ReadBuff,ReadLength2);
// strcpy(ReadBuff,"");
ComEvent=0;
}
}
}