• <menu id="w2i4a"></menu>
  • 首頁 > 慧問 > 頻道

    使用Qt編程時,子線程中要如何使用串口讀寫數(shù)據(jù)?

    發(fā)表于2019-05-10 回復(fù):0 查看:2136  |  
    希望在主線程中創(chuàng)建多個子線程,在各個子線程中進(jìn)行串口數(shù)據(jù)收發(fā)及解析,發(fā)現(xiàn)在子線程中創(chuàng)建串口對象,總是不響應(yīng)串口消息,readData()中打斷點怎么都進(jìn)不去,跪求大俠指點! 代碼如下: 主線程中創(chuàng)建子線程: <pre class='brush: cpp'>QMyThread *MyThread = new QMyThread; MyThread->start();</pre> 子線程中創(chuàng)建串口對象: <pre class='brush: cpp'>void QMyThread::run() { m_MyConn = new MyConnect(this,&(pRtuObj->m_strCommType),&(pRtuObj->m_sListSerialPara),&(pRtuObj->m_sListNetPara)); m_MyConn->openCommu(); m_MyConn->moveToThread(this); } 打開串口: //通信連接 void MyConnect::openCommu() { if(m_Serial != NULL) { delete m_Serial; m_Serial = NULL; } m_Serial = new QSerialPort; connect(m_Serial, SIGNAL(readyRead()), this, SLOT(readData())); m_Serial->setPortName(m_CommSettings.name); m_Serial->setBaudRate(m_CommSettings.baudRate); m_Serial->setDataBits(m_CommSettings.dataBits); m_Serial->setParity(m_CommSettings.parity); m_Serial->setStopBits(m_CommSettings.stopBits); if (m_Serial>open(QIODevice::ReadWrite)) { SendEvent(tr("Connected to %1").arg(m_CommSettings.m_sListSerialPara.join(","))); m_chCommuStatus = 1; } else { SendEvent(tr("Open Serial Error, %1").arg(m_CommSettings.m_sListSerialPara.join(","))); m_chCommuStatus = 0; } } 串口讀數(shù)據(jù): //串口讀數(shù)據(jù) void MyConnect::readData() { QByteArray buffer = m_Serial->readAll(); displayRxTxData(buffer); }</pre>
    0個回答

    打破零回復(fù)...

    回復(fù)

    登錄 慧都網(wǎng)發(fā)表評論

    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();