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

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

mfc

2个回答

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是启动串口的函数

weixin_43739957
weixin_43739957 不错不错,看看了
一年多之前 回复

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问