|
为什么我的串口读线程会导致程序直接退出? 在线等,
[复制链接]
/************************************************************************/
/* 串口接收回调函数 */
/************************************************************************/
//CString m_strRecv ;
void CMFCWinCEDlg::OnSeriesRead(CWnd* pWnd,BYTE* buf,int bufLen)
{
//SerialPort com = new SerialPort("COM1");
CMFCWinCEDlg *pDlg = (CMFCWinCEDlg *)pWnd;
pDlg->UpdateData(TRUE);
for(int i=0;i
pDlg->m_strRecv.Format(L"%s %c",pDlg->m_strRecv,buf);
pDlg->UpdateData(FALSE);
delete[] buf;
}
//串口读线程函数
DWORD CCESeries::ReadThreadFunc(LPVOID lparam)
{
CCESeries *ceSeries = (CCESeries*)lparam;
//AfxMessageBox(L"Start read thread...");
DWORD evtMask;
BYTE * readBuf = NULL;//读取的字节
//char * readBuf = NULL;
DWORD actualReadLen=0;//实际读取的字节数
DWORD willReadLen;
DWORD dwReadErrors;
COMSTAT cmState;
TCHAR szBuf[200];
//AfxMessageBox(L"Read Thread Func.");
// 清空缓冲,并检查串口是否打开。
//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 (ceSeries->m_hComm !=INVALID_HANDLE_VALUE)//(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];
ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,0);
//如果读取的数据大于0,
if (actualReadLen>0)
{
// wsprintf(szBuf, TEXT("Length: %ld\r\n"), willReadLen);
// OutputDebugString(szBuf);
//触发读取回调函数
// AfxMessageBox(L"Onseriesread!");
ceSeries->m_OnSeriesRead(ceSeries->m_pPortOwner,readBuf,actualReadLen);
}
}
}
//如果收到读线程退出信号,则退出线程
if (WaitForSingleObject(ceSeries->m_hReadCloseEvent,500) == WAIT_OBJECT_0)
{
break;
}
}
return 0;
}
希望有高手可以帮忙,多谢,在线等,
|
|