WaitCommEvent挂起

时间:2014-06-03 12:48:45

标签: c++ winapi

我使用非重叠的WaitCommEvent来读取文件数据。 我想提供一段代码如下......

SetCommMask (io_ptr->comPortHandles->hComPort, EV_RXCHAR|EV_TXEMPTY);
WaitCommEvent (io_ptr->comPortHandles->hComPort, &dwMask, 0);

if (dwMask &= EV_RXCHAR) {
  // Loop getting data.

  // Need to loop because our buffer is only 1024 bytes
  while (TRUE)
  {
    ClearCommError( io_ptr->comPortHandles->hComPort, &dwError, &comstat);

    if (!comstat.cbInQue) continue;
    else 
    {
      if(comstat.cbInQue > 0)
        ReceiveInterrupt(io_ptr, comstat);
    }
    // Loop around and check for more data
    // In case additional byte has arrived while reading.
  }
}

1 个答案:

答案 0 :(得分:2)

如果文件句柄没有打开并带有重叠标记,那么WaitCommEvent会阻塞。在这种情况下,它会等到收到char或最后一个char后才会发送。

关于WaitCommEvent的MSDN:

  

如果未使用FILE_FLAG_OVERLAPPED打开hFile,则WaitCommEvent将不会返回,直到指定的事件之一或发生错误。