jqz_software
jqz_software
采纳率0%
2018-11-19 10:24 阅读 907

MFC 窗口程序中线程与串口问题

5

我创建了一个线程,在线程中启动了串口,但是无法触发OnCommMscomm1事件,将启动串口的函数移出
线程,OnCommMscomm1能够触发,请问是怎么回事?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • jqz_software jqz_software 2018-11-19 10:26

    void CMCF_TestDlg::OnBnClickedButton5()
    {

    test_log.ResetContent();//清理listbox内数据
    //PurgeComm(g_hComm,PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR);

    test_log.AddString(_T("检测接口连接!"));
    pStartTestEngineThread = AfxBeginThread(StartTestEngineThread,(LPVOID)this);//启动线程
    //pStartTestEngineThread->m_bAutoDelete = FALSE;
    //WaitForSingleObject( pStartTestEngineThread->m_hThread, INFINITE );//结束线程
    
    //OnBnClickedButton7();
    

    }

    UINT CMCF_TestDlg::StartTestEngineThread(void param)
    {
    CMCF_TestDlg *pDlg = (CMCF_TestDlg
    )param;

    dbg_devHandle = openTestEngine(USBDBG, "1", 0, 5000, 0);
    if(dbg_devHandle == 0)
    {
        //Cache_con = USB_CON_ERROR;
        pDlg->test_log.AddString(_T("USB口连接失败!"));
        TRB_devHandle = openTestEngine(TRB, "1", 0, 5000, 0);
        if(TRB_devHandle == 0)
        {
            //Cache_con = TRB_CON_ERROR;
            pDlg->test_log.AddString(_T("TRB口连接失败!"));
        }
        else
        {
            //Cache_con = TRB_CON_SUCCEED;
            pDlg->test_log.AddString(_T("TRB口连接成功!"));
            devHandle = TRB_devHandle;
        }
    }
    else
    {
        //Cache_con = USB_CON_SUCCEED;
        pDlg->test_log.AddString(_T("USB口连接成功!"));
        devHandle = dbg_devHandle;
    }
    
    if(devHandle != 0)
    {
        success = teConfigCacheInit(devHandle, CFG_DB_PARAM);
    
        if (success == TE_OK)
        {
            success = teConfigCacheRead(devHandle, NULL, unused);
    
            if(success != TE_OK)
            {
                //Cache_con = RED_FACILITY_DATA_ERROR;
                pDlg->test_log.AddString(_T("读取设备信息失败!"));
                //WaitForSingleObject( pStartTestEngineThread->m_hThread, INFINITE );//结束线程
                return 0;
            }
            else
            {
                //Cache_con = RED_FACILITY_DATA_SUCCEED;
                pDlg->test_log.AddString(_T("读取设备信息成功!"));  
    
                pDlg->RadDeviceName(devHandle,success,unused);
    
                pDlg->RedBluetoothAddress(devHandle,success,unused);
    
                pDlg->RadXtalTrimValue(devHandle,success,unused);
                pDlg->RadXtalTrimValue2(devHandle,success,unused);
    
                pDlg->OnBnClickedButton7();
            }
    
            WaitForSingleObject( pDlg->pStartTestEngineThread->m_hThread, INFINITE );//结束线程
        }
        else
        {
            //Cache_con = INIT_CACHE_ERROR;
            pDlg->test_log.AddString(_T("初始化缓存失败"));
            //WaitForSingleObject( pStartTestEngineThread->m_hThread, INFINITE );//结束线程
            closeTestEngine(devHandle);
            return 0;
        }
    }
    return 0;
    

    }

    这是相关代码,其中OnBnClickedButton7是启动串口的函数

    点赞 评论 复制链接分享
  • phenix2009 白色一大坨 2018-11-20 04:18

    一般你要做这种操作,启动和停止不应该放在线程里面,线程里面做读写即可。

    点赞 评论 复制链接分享

相关推荐