创建一个线程监视串口,当有一个字符来时,产生一个事件,现在出现一个问题,第一次可以正确接收到字符,第二次就接收不到了!跟踪了一下,发现线程都没有启动,可是设备确实向串口发回了数据啊!!??为什么会这样呢?

解决方案 »

  1.   

    你产生事件后线程就结束了吧,你好好看看线程方面的资料,应该是触发事件之后又回到线程中去,使用waittimeoutobject这类的可以
      

  2.   

    代码在:http://www.vchelp.net/cndevforum/subject_view.asp?subject_id=59979&forum_id=34
      

  3.   

    DWORD willRead=0;
    这段代码是仿照网上的啊!
    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;
    }
      

  4.   

    我估计是线程结束,你可以debug, 在return前放breakpoint.
      

  5.   

    DWORD willRead=0;
    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次以上,程序就死了!为什么啊!?