创建一个线程监视串口,当有一个字符来时,产生一个事件,现在出现一个问题,第一次可以正确接收到字符,第二次就接收不到了!跟踪了一下,发现线程都没有启动,可是设备确实向串口发回了数据啊!!??为什么会这样呢?
解决方案 »
- 用CFile open 创建文件失败
- 请问如何分辨doc 与 xls 文件
- 大家来看看我的CSkinButton里的绘制函数,CreateCompatibleDC()总出错.
- 关于把一个不是基类的类作为基类使用的问题~~求教
- 求清晰的《vc技术内幕(第四版)》中文版
- 十万火急..我在一个局域网内怎么另一个局域网内的HTTP服务器?
- 请问如何改变窗口的颜色以及如何创建一个新的类。
- 关于installshield中program updates的疑问?
- football.cpp
- 在对话框中的图片控件(type为bitmap)怎么实现鼠标移上去就换图,且点击时触发事件。
- Dialog和FormView的字体不一样 如何修改字体?
- 请问,我用DAO写到OPEN时,在本机没事,为什么一到别的机子上一运行就报DAOCORE 内部应用程序错误呢?谢谢指教
这段代码是仿照网上的啊!
UINT CommProc(LPVOID pParam)
{
CXXXXDlg *pDlg=(CXXXXDlg*)pParam;
DWORD dwMask,dwTrans;
COMSTAT comStat;
DWORD dwErrorFlags;
OVERLAPPED os={0};
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(os.hEvent==NULL)
{
AfxMessageBox("CommProc CreateEvent failed");
return -1;
}
for( ; ; )
{
ClearCommError(h_Com,&dwErrorFlags,&comStat );
if(comStat.cbInQue)//串口接收字节数不为零
{
//无限等待WM_MYNOTIFY消息处理完
WaitForSingleObject(pDlg->m_hPostMsgEvent,INFINITE);
ResetEvent(pDlg->m_hPostMsgEvent);
willRead=comStat.cbInQue;
pDlg->PostMessage(WM_MYNOTIFY,EV_RXCHAR,0);
continue;
}
dwMask=0;
if(!WaitCommEvent(h_Com,&dwMask,&os))
{
switch (dwErrorFlags = GetLastError())
{
case ERROR_IO_PENDING:
{
// This is a normal return value if there are no bytes
// to read at the port.
// Do nothing and continue
GetOverlappedResult(h_Com,&os,&dwTrans,TRUE);
//wait for until receiving any char
break;
}
case 87:
{
// Under Windows NT, this value is returned for some reason.
// I have not investigated why, but it is also a valid reply
// Also do nothing and continue.
GetOverlappedResult(h_Com,&os,&dwTrans,TRUE);
break;
}
default:
{
// All other error codes indicate a serious error has
// occured. Process this error.
AfxMessageBox("error");
break;
}
}
}
}
CloseHandle(os.hEvent);
return 0;
}
CString str_receive=_T("");
LRESULT CXXXXDlg:: OnCommNotify(WPARAM wParam,LPARAM lParam)
{
m_pOutbuffer=new char[willRead];
memset(m_pOutbuffer, 0, willRead);
if((wParam&EV_RXCHAR)!=EV_RXCHAR)
{
SetEvent(m_hPostMsgEvent);
return 0L;
}
else
{
int i;
DWORD number=ReadComm(m_pOutbuffer,willRead);
CString string_receive=_T("");
for( i=0;i<(int)number;i++)
{
string_receive=string_receive+m_pOutbuffer[i];
str_receive+=string_receive;
string_receive=_T("");
}
SetEvent(m_hPostMsgEvent);
}
delete []m_pOutbuffer;
return 0L;
}
UINT CommProc(LPVOID pParam)
{
CTESTDlg *pDlg=(CTESTDlg*)pParam;
OVERLAPPED os;
DWORD dwMask, dwTrans;
COMSTAT comStat;
DWORD dwErrorFlags;
memset(&os, 0, sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
if(os.hEvent==NULL)
{
AfxMessageBox("CommProc CreateEvent failed");
return -1;
}
for( ; ; )
{
ClearCommError(h_Com,&dwErrorFlags,&comStat );
if(comStat.cbInQue)//串口接收字节数不为零
{
//无限等待WM_MYNOTIFY消息处理完
WaitForSingleObject(pDlg->m_hPostMsgEvent,INFINITE);
ResetEvent(pDlg->m_hPostMsgEvent);
willRead=comStat.cbInQue;
pDlg->PostMessage(WM_MYNOTIFY,EV_RXCHAR,0);
continue;
}
dwMask=0;
if(!WaitCommEvent(h_Com,&dwMask,&os))
{
if(GetLastError()==ERROR_IO_PENDING)
// 无限等待重叠操作结果
GetOverlappedResult(h_Com, &os, &dwTrans, TRUE);
////////////////////去掉这段代码就可已连续接收了////////////
// else
// {
// CloseHandle(os.hEvent);
// return (UINT)-1;
// }
////////////////////////////////////////////////////////////
}
} CloseHandle(os.hEvent);
return 0;
}
但是我要连续发送4次以上,程序就死了!为什么啊!?