|
//分配设备缓冲区
SetupComm(m_hComm,512,512);
读取串口的部分代码如下:
//清空串口
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)
{
ClearCommError(ceSeries->m_hComm,&dwReadErrors,&cmState);
willReadLen = cmState.cbInQue ;
if (willReadLen <= 0)
{
continue;
}
readBuf = new BYTE[willReadLen+1];
while(dwHaveBytesRead!=willReadLen)
{
ReadFile(ceSeries->m_hComm, readBuf+dwHaveBytesRead, willReadLen-dwHaveBytesRead, &actualReadLen,0);
dwHaveBytesRead= dwHaveBytesRead+actualReadLen;
}
readBuf[willReadLen]='\0';
if (dwHaveBytesRead)
{
//触发读取回调函数
ceSeries->m_OnSeriesRead(ceSeries->m_pPortOwner,readBuf,dwHaveBytesRead);
dwHaveBytesRead=0;
}
PurgeComm(ceSeries->m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR );
}
}麻烦你看下吧。。。 |
|