|
DWORD WINAPI CPSerialPort::ReadPortThread(LPVOID lpParameter)
{
CPSerialPort* m_pSerial;
m_pSerial=(CPSerialPort*)lpParameter;
BOOL fReadState;
DWORD dwCommModemStatus;
DWORD dwLength;
COMSTAT ComStat;
DWORD dwErrorFlags;
unsigned char* buf=new unsigned char[m_pSerial->m_nBufferSize];
try
{
while((m_pSerial->m_hComm!=INVALID_HANDLE_VALUE)&&(m_pSerial->m_bReceiving))
{
WaitCommEvent(m_pSerial->m_hComm,&dwCommModemStatus,0);
if(dwCommModemStatus & EV_RXCHAR)
{
ClearCommError(m_pSerial->m_hComm,&dwErrorFlags,&ComStat);
SetCommMask (m_pSerial->m_hComm, EV_RXCHAR);
dwLength=ComStat.cbInQue;
if(dwLength>0)
{
fReadState=ReadFile(m_pSerial->m_hComm,buf,dwLength,&dwLength,NULL);
if(!fReadState)
{
AfxMessageBox(_T("无法从串口读取数据!"));
}
else
{
if(dwLength!=0)
{
//回送数据
if(m_pSerial->m_lpDataArriveProc!=NULL)
{
m_pSerial->m_lpDataArriveProc(buf,dwLength,m_pSerial->m_dwUserData);
}
}
}
}
}
Sleep(100);
}
delete[] buf;
}
catch(...)
{
AfxMessageBox(_T("MotherReadPortThread"));
}
return 0;
} |
|