平時的項目程序中,經(jīng)常需要處理多個串口和網(wǎng)絡(luò)發(fā)送過來的數(shù)據(jù),而且數(shù)據(jù)量還比較大,9600的波特率每秒鐘至少1000個字節(jié)的數(shù)據(jù)需要處理并反映到界面上,一開始直接和UI主線程同一個線程,在x86的機器上跑還沒問題,畢竟X86的機器最少主頻也不會低于1.6G,但是如果數(shù)據(jù)量再更大或者到了ARM上跑,直接UI卡住不動,想到的解決辦法就是用多線程,一個線程負責收數(shù)據(jù),一個線程負責處理數(shù)據(jù),當協(xié)議一樣的時候,如果需要將數(shù)據(jù)解析從串口改為網(wǎng)絡(luò)端口監(jiān)聽的數(shù)據(jù),以前的辦法是重新寫一個tcp通信進行處理,這個并不是非常合理的辦法,畢竟解析協(xié)議是一樣的,所以自己總結(jié)了一個通用的數(shù)據(jù)處理思路:各種數(shù)據(jù)接收后排隊的形式存入一個全局變量,單獨開辟一個線程從這個全局變量中讀取第一個數(shù)據(jù),處理完則移除第一個數(shù)據(jù),Qt中的鏈表直接提供了一個takeFirst函數(shù),用起來非常爽!用while循環(huán)讀取,在讀取的時候加鎖,這樣的話就不會沖突了。
雛形:
全局變量文件:
復(fù)制代碼
#ifndef APP_H
#define APP_H
#include "qstring.h"
#include "qstringlist.h"
class App
{
public:
static QStringList list;
};
#endif // APP_H
復(fù)制代碼
#include "app.h"
QStringList App::list=QStringList();
獨立處理數(shù)據(jù)線程:
復(fù)制代碼
#ifndef TEST_H
#define TEST_H
#include "qthread.h"
#include "qmutex.h"
class Thread : public QThread
{
Q_OBJECT
public:
Thread();
~Thread();
void stop();
protected:
void run();
private:
QMutex mutex;
volatile bool stopped;
signals:
void readOne(QString txt);
};
#endif // TEST_H
復(fù)制代碼
#include "thread.h"
#include "app.h"
Thread::Thread()
{
stopped=false;
}
Thread::~Thread()
{
}
void Thread::stop()
{
stopped=true;
}
void Thread::run()
{
while(!stopped){
mutex.lock();
if (App::list.count()>0){
QString txt=App::list.takeFirst();
emit readOne(txt);
}
mutex.unlock();
msleep(1);//不加這句CPU占用率高達50%
}
stopped=false;
}
主界面:
復(fù)制代碼
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "thread.h"
#include "qtimer.h"
namespace Ui {
class frmMain;
}
class frmMain : public QWidget
{
Q_OBJECT
public:
explicit frmMain(QWidget *parent = 0);
~frmMain();
private slots:
void writeOne();
void readOne(QString txt);
void on_btnAppend_clicked();
void on_btnThread_clicked();
void on_btnTimer_clicked();
private:
Ui::frmMain *ui;
QTimer *timer;
Thread *thread;
};
#endif // WIDGET_H
復(fù)制代碼
#include "frmmain.h"
#include "ui_frmmain.h"
#include "app.h"
#include "qdatetime.h"
#include "qdesktopwidget.h"
#define _TIME_ qPrintable (QTime::currentTime().toString("now : hh:mm:ss:zzz"))
frmMain::frmMain(QWidget *parent) :
QWidget(parent),
ui(new Ui::frmMain)
{
ui->setupUi(this);
this->showMaximized();
timer=new QTimer(this);
timer->setInterval(50);
connect(timer,SIGNAL(timeout()),this,SLOT(writeOne()));
thread=new Thread;
connect(thread,SIGNAL(readOne(QString)),this,SLOT(readOne(QString)));
}
frmMain::~frmMain()
{
delete ui;
}
void frmMain::writeOne()
{
App::list.append(_TIME_);
}
void frmMain::readOne(QString txt)
{
ui->txtOut->append(txt);
}
void frmMain::on_btnAppend_clicked()
{
App::list.append(ui->txtIn->text());
}
void frmMain::on_btnThread_clicked()
{
if (ui->btnThread->text()=="start thread"){
thread->start();
ui->btnThread->setText("stop thread");
ui->txtOut->append("start thread ok");
}else{
thread->stop();
ui->btnThread->setText("start thread");
ui->txtOut->append("stop thread ok");
}
}
void frmMain::on_btnTimer_clicked()
{
if (ui->btnTimer->text()=="start timer"){
timer->start();
ui->btnTimer->setText("stop timer");
ui->txtOut->append("start timer ok");
}else{
timer->stop();
ui->btnTimer->setText("start timer");
ui->txtOut->append("stop timer ok");
}
}
為了模擬大量數(shù)據(jù),我這里開了50毫秒的定時器定時產(chǎn)生當前時間字符串的數(shù)據(jù)存入全局變量,然后放置了幾個按鈕用于手動添加字符串和開始停止線程及定時器。
歡迎提出批評建議以及指點!謝謝!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站名稱:QT多發(fā)線程進行數(shù)據(jù)傳輸-創(chuàng)新互聯(lián)
文章路徑:http://aaarwkj.com/article20/jdhco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、虛擬主機、App開發(fā)、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容