in the usual project program, it is often necessary to process multipleSerial Portand the network sent overData, and the data volume is relatively large, 9600 baud rate at least 1000 bytes per second data needs to be processed and reflected inInterface, the same thread that started directly with the UI thread, ran on the x86 machine .problem, after all, X86 the minimum frequency of the machine will not be less than 1.6G, but if the amount of data is larger or to the arm run, the direct UI stuck, think of the solution is to use multi-threading, a thread responsible for collecting data, a thread responsible for processing data, when the protocol, if necessary to the data parsing from the serial port to the network data, the previous method is to re-write a TCP communication processing, this is not a very reasonable approach, after all, the resolution protocol is the same, so I summed up a general idea of data processing: a variety of information received in the form of the back row of the queue into a global variable, Open a separate thread to read the first data from this global variable, and then remove the first data, the list of QT directly provides a takefirst function, very cool! Use the while loop to read and lock while reading, so that there is no conflict.
prototype:
Global Variablesfile:
Copy Code
#ifndef App_h
#define App_h
#include "qstring.h"
#include "qstringlist.h"
Class APP
{
Public
Static Qstringlist list;
};
#endif//app_h
Copy Code
#include "app.h"
Qstringlist app::list=qstringlist ();
Processing Data Threads independently:
Copy Code
#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
Copy Code
#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);//do Not add this sentence CPU usage up to 50%
}
stopped=false;
}
Main interface:
Copy Code
#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
Copy Code
#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");
}
}
in order to simulate a large amount of data, I opened a timer here for 50 milliseconds, timed to generate the current time string data into a global variable, and then placed severalButtonused to manually add strings and start stopping threads and Timers.
650) this.width=650; "src=" http://www.qtcn.org/bbs/attachment/Mon_1501/44_110085_67de59347a0fe38.jpg?143 " border= "0" style= "border:0px;" alt= "44_110085_67de59347a0fe38.jpg?143"/>
Welcome to criticize suggestions and guidance! Thank you!
QT Multiple threads for data transfer