QT 线程 串口接收程序是本文介绍的内容,不多介绍了,先来看代码。新建工程serial,UI界面如下:
文件的结构如下:
其中qextserialbase.h qextserialbase.cpp win_qextserialport.h win_qextserialport.cpp是与串口通信相关的
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变量是用来控制退出线程的,当stopped为0时,退出线程。
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()在关闭窗口时被调用;
wait()函数类似于 pthread_join(),等待一个线程的结束,并进行资源回收。
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();
- }
串口通信的内容请查看http://www.yafeilinux.com/?p=820
小结:关于详解 QT 线程 串口接收程序的内容介绍完了,希望本文对你有所帮助!