詳解 QT 線程 串口接收程序
QT 線程 串口接收程序是本文介紹的內(nèi)容,不多介紹了,先來看代碼。新建工程serial,UI界面如下:
文件的結(jié)構(gòu)如下:

其中qextserialbase.h qextserialbase.cpp win_qextserialport.h win_qextserialport.cpp是與串口通信相關(guān)的

thread.h 文件:
- #ifndef THREAD_H
 - #define THREAD_H
 - #include <QThread>
 - #include <QString>
 - #include "qextserialbase.h"
 - #include "win_qextserialport.h"
 - class Thread : public QThread
 - {
 - Q_OBJECT
 - public:
 - Thread(QString com, QObject *parent);
 - ~Thread();
 - void run();
 - void stopThread();
 - signals:
 - void serialFinished(QByteArray temp);
 - private:
 - Win_QextSerialPort *myCom;
 - int stopped;
 - };
 - #endif // THREAD_H
 - #ifndef THREAD_H
 - #define THREAD_H
 - #include <QThread>
 - #include <QString>
 - #include "qextserialbase.h"
 - #include "win_qextserialport.h"
 - class Thread : public QThread
 - {
 - Q_OBJECT
 - public:
 - Thread(QString com, QObject *parent);
 - ~Thread();
 - void run();
 - void stopThread();
 - signals:
 - void serialFinished(QByteArray temp);
 - private:
 - Win_QextSerialPort *myCom;
 - int stopped;
 - };
 - #endif // THREAD_H
 
stopped變量是用來控制退出線程的,當(dāng)stopped為0時(shí),退出線程。
thread.cpp文件:
- #include "thread.h"
 - Thread::Thread(QString com, QObject *parent)
 - :QThread(parent)
 - {
 - myCom = new Win_QextSerialPort(com, QextSerialBase::EventDriven);
 - bool isOpen = myCom->open(QIODevice::ReadWrite);
 - stopped = 1;
 - if(isOpen)
 - {
 - myCom->setBaudRate(BAUD9600);
 - myCom->setDataBits(DATA_8);
 - myCom->setParity(PAR_NONE);
 - myCom->setStopBits(STOP_1);
 - myCom->setFlowControl(FLOW_OFF);
 - myCom->setTimeout(500);
 - }
 - }
 - Thread::~Thread()
 - {
 - }
 - void Thread::run()
 - {
 - while(stopped)
 - {
 - msleep(5000); //delay 5ms
 - QByteArray temp = myCom->read(8);
 - if(temp.size()==8)
 - emit this->serialFinished(temp.toHex());
 - }
 - }
 - void Thread::stopThread()
 - {
 - stopped = 0;
 - }
 - #include "thread.h"
 - Thread::Thread(QString com, QObject *parent)
 - :QThread(parent)
 - {
 - myCom = new Win_QextSerialPort(com, QextSerialBase::EventDriven);
 - bool isOpen = myCom->open(QIODevice::ReadWrite);
 - stopped = 1;
 - if(isOpen)
 - {
 - myCom->setBaudRate(BAUD9600);
 - myCom->setDataBits(DATA_8);
 - myCom->setParity(PAR_NONE);
 - myCom->setStopBits(STOP_1);
 - myCom->setFlowControl(FLOW_OFF);
 - myCom->setTimeout(500);
 - }
 - }
 - Thread::~Thread()
 - {
 - }
 - void Thread::run()
 - {
 - while(stopped)
 - {
 - msleep(5000); //delay 5ms
 - QByteArray temp = myCom->read(8);
 - if(temp.size()==8)
 - emit this->serialFinished(temp.toHex());
 - }
 - }
 - void Thread::stopThread()
 - {
 - stopped = 0;
 - }
 
#p#
widget.h文件:
- #ifndef WIDGET_H
 - #define WIDGET_H
 - #include <QWidget>
 - #include <QCloseEvent>
 - #include "thread.h"
 - namespace Ui {
 - class Widget;
 - }
 - class Widget : public QWidget {
 - Q_OBJECT
 - public:
 - Widget(QWidget *parent = 0);
 - ~Widget();
 - protected:
 - void changeEvent(QEvent *e);
 - void closeEvent(QCloseEvent *event);
 - private:
 - Ui::Widget *ui;
 - Thread *th;
 - private slots:
 - void on_pushButton_clicked();
 - void ReadData(QByteArray temp);
 - };
 - #endif // WIDGET_H
 - #ifndef WIDGET_H
 - #define WIDGET_H
 - #include <QWidget>
 - #include <QCloseEvent>
 - #include "thread.h"
 - namespace Ui {
 - class Widget;
 - }
 - class Widget : public QWidget {
 - Q_OBJECT
 - public:
 - Widget(QWidget *parent = 0);
 - ~Widget();
 - protected:
 - void changeEvent(QEvent *e);
 - void closeEvent(QCloseEvent *event);
 - private:
 - Ui::Widget *ui;
 - Thread *th;
 - private slots:
 - void on_pushButton_clicked();
 - void ReadData(QByteArray temp);
 - };
 - #endif // WIDGET_H
 
widget.cpp文件:
- #include "widget.h"
 - #include "ui_widget.h"
 - Widget::Widget(QWidget *parent) :
 - QWidget(parent),
 - ui(new Ui::Widget)
 - {
 - ui->setupUi(this);
 - th = NULL;
 - }
 - Widget::~Widget()
 - {
 - delete ui;
 - }
 - void Widget::changeEvent(QEvent *e)
 - {
 - QWidget::changeEvent(e);
 - switch (e->type()) {
 - case QEvent::LanguageChange:
 - ui->retranslateUi(this);
 - break;
 - default:
 - break;
 - }
 - }
 - void Widget::on_pushButton_clicked()
 - {
 - #if 1
 - QString text = ui->comboBox->currentText();
 - th = new Thread(text, this);
 - th->start();
 - connect(th, SIGNAL(serialFinished(QByteArray)), this, SLOT(ReadData(QByteArray)));
 - #endif
 - }
 - void Widget::ReadData(QByteArray temp)
 - {
 - #if 1
 - ui->textBrowser->insertPlainText(temp);
 - ui->textBrowser->insertPlainText(tr("\n\n"));
 - #endif
 - }
 - void Widget::closeEvent(QCloseEvent *event)
 - {
 - if(th!=NULL)
 - {
 - th->stopThread();
 - th->wait();
 - }
 - event->accept();
 - }
 - #include "widget.h"
 - #include "ui_widget.h"
 - Widget::Widget(QWidget *parent) :
 - QWidget(parent),
 - ui(new Ui::Widget)
 - {
 - ui->setupUi(this);
 - th = NULL;
 - }
 - Widget::~Widget()
 - {
 - delete ui;
 - }
 - void Widget::changeEvent(QEvent *e)
 - {
 - QWidget::changeEvent(e);
 - switch (e->type()) {
 - case QEvent::LanguageChange:
 - ui->retranslateUi(this);
 - break;
 - default:
 - break;
 - }
 - }
 - void Widget::on_pushButton_clicked()
 - {
 - #if 1
 - QString text = ui->comboBox->currentText();
 - th = new Thread(text, this);
 - th->start();
 - connect(th, SIGNAL(serialFinished(QByteArray)), this, SLOT(ReadData(QByteArray)));
 - #endif
 - }
 - void Widget::ReadData(QByteArray temp)
 - {
 - #if 1
 - ui->textBrowser->insertPlainText(temp);
 - ui->textBrowser->insertPlainText(tr("\n\n"));
 - #endif
 - }
 - void Widget::closeEvent(QCloseEvent *event)
 - {
 - if(th!=NULL)
 - {
 - th->stopThread();
 - th->wait();
 - }
 - event->accept();
 - }
 
closeEvent()在關(guān)閉窗口時(shí)被調(diào)用;
wait()函數(shù)類似于 pthread_join(),等待一個線程的結(jié)束,并進(jìn)行資源回收。
main.cpp文件:
- #include <QtGui/QApplication>
 - #include "widget.h"
 - int main(int argc, char *argv[])
 - {
 - QApplication a(argc, argv);
 - Widget w;
 - w.show();
 - return a.exec();
 - }
 - #include <QtGui/QApplication>
 - #include "widget.h"
 - int main(int argc, char *argv[])
 - {
 - QApplication a(argc, argv);
 - Widget w;
 - w.show();
 - return a.exec();
 - }
 
串口通信的內(nèi)容請查看http://www.yafeilinux.com/?p=820
小結(jié):關(guān)于詳解 QT 線程 串口接收程序的內(nèi)容介紹完了,希望本文對你有所幫助!















 
 
 
 
 
 
 