首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

串口读写线程怎么退出的有关问题

2013-06-25 
串口读写线程如何退出的问题大家好!在下在弄一个串口读写的工具,但是在ReadThread中无法正常退出,请各位帮

串口读写线程如何退出的问题
大家好!在下在弄一个串口读写的工具,但是在ReadThread中无法正常退出,请各位帮忙给看看,谢谢!!



//这个是读数据的线程,但是当我关闭线程时,将iExitFlag置为1,为什么无法退出呢
//volatile int g_iExitFlag=0;
DWORD ConnPort::ReadThreadProc(LPVOID p)
{
    COMSTAT comStat;
    DWORD   dwErrFlag;
    DWORD   dwLength;
    DWORD   dwModemStat;
    ConnPort *pThis=(ConnPort *)p;
    BYTE     szRev[512];
  
    if(INVALID_HANDLE_VALUE==pThis->m_hPort)
    {
        AfxMessageBox(L"the port is not opened!");
    }
    while(TRUE)
    {
if(1==g_iExitFlag)
break;
        WaitCommEvent(pThis->m_hPort,&dwModemStat,0);
        EnterCriticalSection(&pThis->m_csRead);
        if(dwModemStat&EV_RXCHAR)
        {
        ClearCommError(pThis->m_hPort,&dwErrFlag,&comStat);
        dwLength=comStat.cbInQue;//返回字符长度
        if(dwLength>0)
        {
        //读数据
            BOOL bRet=ReadFile(pThis->m_hPort,szRev,dwLength,&dwLength,NULL);
            if(!bRet) 
            {
                MessageBox(NULL,TEXT("Read Error"),TEXT("Error"),MB_OK);
            }
        }
        } 
        //解析数据
        pThis->ParseComData(szRev);
        GetCommModemStatus(pThis->m_hPort,&dwModemStat);
        LeaveCriticalSection(&pThis->m_csRead);
    }  
    return 0;
}

[解决办法]
设置1时打个断点调试下。是不是有弹框,是不是卡在WaitCommEvent这里

热点排行