读串口事件只响应一次
本帖最后由 cndx100 于 2010-09-14 18:44:48 编辑 读串口纯程中的WaitCommEvent第一次接收到数据时响应,过后就阻塞住了,新的数据来时也不会退出。关闭串口时他又会退出阻塞状态。请问大家什么地方有问题呀!
//串口读线程函数
DWORD Ce_series::read_thread(LPVOID lparam)
{
Ce_series *ceSeries = (Ce_series*)lparam;
DWORD evtMask;
BYTE *readBuf = NULL;
DWORD actualReadLen=0;
DWORD willReadLen;
DWORD dwReadErrors;
COMSTAT cmState;
ASSERT(ceSeries->m_hComm !=INVALID_HANDLE_VALUE);
PurgeComm(ceSeries->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR );
SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
while (TRUE)
{
if (WaitCommEvent(ceSeries->m_hComm, &evtMask, 0)) // 第一次接收到数据时响应,过后就被阻塞在此,
// 关闭串口时则会退出阻塞状态。
{
SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
//串口收到字符
if (evtMask & EV_RXCHAR)
{
Sleep(430); // you can sleep(90) if use 115200 baud-rate
ClearCommError(ceSeries->m_hComm,&dwReadErrors,&cmState);
willReadLen = cmState.cbInQue;
readBuf = new BYTE[willReadLen];
ZeroMemory(readBuf,willReadLen);
ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,0);
if (actualReadLen > 0)
{
ZeroMemory(read_buffer, 512);
CopyMemory(read_buffer, readBuf, actualReadLen);
read_buffer[actualReadLen] = '\0';
read_len = actualReadLen;
}
delete[] readBuf;
readBuf = NULL;
}
}
//如果收到读线程退出信号,则退出线程
if (WaitForSingleObject(ceSeries->m_hReadCloseEvent, 500) == WAIT_OBJECT_0)
{
break;
}
}
return 0;
}