详解 QT 线程 串口接收程序

移动开发
本文内容主要是在讲QT 线程 串口接收程序,文字描述不多,基本是代码实现的,那么大家就看看过程吧。

QT 线程 串口接收程序是本文介绍的内容,不多介绍了,先来看代码。新建工程serial,UI界面如下:

文件的结构如下:

QT 线程 串口接收程序

其中qextserialbase.h qextserialbase.cpp win_qextserialport.h win_qextserialport.cpp是与串口通信相关的

QT 线程 串口接收程序

thread.h 文件:

  1. #ifndef THREAD_H     
  2. #define THREAD_H     
  3. #include <QThread>     
  4. #include <QString>     
  5. #include "qextserialbase.h"     
  6. #include "win_qextserialport.h"     
  7. class Thread : public QThread     
  8. {     
  9.     Q_OBJECT     
  10. public:     
  11.     Thread(QString com, QObject *parent);     
  12.     ~Thread();     
  13.     void run();     
  14.     void stopThread();     
  15. signals:     
  16.     void serialFinished(QByteArray temp);     
  17. private:     
  18.     Win_QextSerialPort *myCom;     
  19.     int stopped;     
  20. };     
  21. #endif // THREAD_H    
  22. #ifndef THREAD_H  
  23. #define THREAD_H  
  24. #include <QThread> 
  25. #include <QString> 
  26. #include "qextserialbase.h"  
  27. #include "win_qextserialport.h"  
  28. class Thread : public QThread  
  29. {  
  30.     Q_OBJECT  
  31. public:  
  32.     Thread(QString com, QObject *parent);  
  33.     ~Thread();  
  34.     void run();  
  35.     void stopThread();  
  36. signals:  
  37.     void serialFinished(QByteArray temp);  
  38. private:  
  39.     Win_QextSerialPort *myCom;  
  40.     int stopped;  
  41. };  
  42. #endif // THREAD_H  

stopped变量是用来控制退出线程的,当stopped为0时,退出线程。

thread.cpp文件:

  1. #include "thread.h"     
  2. Thread::Thread(QString com, QObject *parent)     
  3.     :QThread(parent)     
  4. {     
  5.     myCom = new Win_QextSerialPort(com, QextSerialBase::EventDriven);         
  6.     bool isOpen = myCom->open(QIODevice::ReadWrite);     
  7.     stopped = 1;     
  8.     if(isOpen)     
  9.     {     
  10.         myCom->setBaudRate(BAUD9600);     
  11.         myCom->setDataBits(DATA_8);     
  12.         myCom->setParity(PAR_NONE);     
  13.         myCom->setStopBits(STOP_1);     
  14.         myCom->setFlowControl(FLOW_OFF);     
  15.         myCom->setTimeout(500);     
  16.     }     
  17. }     
  18. Thread::~Thread()     
  19. {     
  20. }     
  21. void Thread::run()     
  22. {     
  23.     while(stopped)     
  24.     {     
  25.         msleep(5000);    //delay 5ms     
  26.         QByteArray temp = myCom->read(8);     
  27.         if(temp.size()==8)     
  28.             emit this->serialFinished(temp.toHex());     
  29.     }     
  30. }     
  31. void Thread::stopThread()     
  32. {     
  33.     stopped = 0;     
  34. }    
  35. #include "thread.h"  
  36. Thread::Thread(QString com, QObject *parent)  
  37.     :QThread(parent)  
  38. {  
  39.     myCom = new Win_QextSerialPort(com, QextSerialBase::EventDriven);      
  40.     bool isOpen = myCom->open(QIODevice::ReadWrite);  
  41.     stopped = 1;  
  42.     if(isOpen)  
  43.     {  
  44.         myCom->setBaudRate(BAUD9600);  
  45.         myCom->setDataBits(DATA_8);  
  46.         myCom->setParity(PAR_NONE);  
  47.         myCom->setStopBits(STOP_1);  
  48.         myCom->setFlowControl(FLOW_OFF);  
  49.         myCom->setTimeout(500);  
  50.     }  
  51. }  
  52. Thread::~Thread()  
  53. {  
  54. }  
  55. void Thread::run()  
  56. {  
  57.     while(stopped)  
  58.     {  
  59.         msleep(5000);    //delay 5ms  
  60.         QByteArray temp = myCom->read(8);  
  61.         if(temp.size()==8)  
  62.             emit this->serialFinished(temp.toHex());  
  63.     }  
  64. }  
  65. void Thread::stopThread()  
  66. {  
  67.     stopped = 0;  

#p#

widget.h文件:

  1. #ifndef WIDGET_H     
  2. #define WIDGET_H     
  3. #include <QWidget>     
  4. #include <QCloseEvent>     
  5. #include "thread.h"     
  6. namespace Ui {     
  7.     class Widget;     
  8. }     
  9. class Widget : public QWidget {     
  10.     Q_OBJECT     
  11. public:     
  12.     Widget(QWidget *parent = 0);     
  13.     ~Widget();     
  14. protected:     
  15.     void changeEvent(QEvent *e);     
  16.     void closeEvent(QCloseEvent *event);     
  17. private:     
  18.     Ui::Widget *ui;     
  19.     Thread *th;     
  20. private slots:     
  21.     void on_pushButton_clicked();     
  22.     void ReadData(QByteArray temp);     
  23. };     
  24. #endif // WIDGET_H    
  25. #ifndef WIDGET_H  
  26. #define WIDGET_H  
  27. #include <QWidget> 
  28. #include <QCloseEvent> 
  29. #include "thread.h"  
  30. namespace Ui {  
  31.     class Widget;  
  32. }  
  33. class Widget : public QWidget {  
  34.     Q_OBJECT  
  35. public:  
  36.     Widget(QWidget *parent = 0);  
  37.     ~Widget();  
  38. protected:  
  39.     void changeEvent(QEvent *e);  
  40.     void closeEvent(QCloseEvent *event);  
  41. private:  
  42.     Ui::Widget *ui;  
  43.     Thread *th;  
  44. private slots:  
  45.     void on_pushButton_clicked();  
  46.     void ReadData(QByteArray temp);  
  47. };  
  48. #endif // WIDGET_H 

widget.cpp文件:

  1. #include "widget.h"     
  2. #include "ui_widget.h"     
  3. Widget::Widget(QWidget *parent) :     
  4.     QWidget(parent),     
  5.     ui(new Ui::Widget)     
  6. {     
  7.     ui->setupUi(this);     
  8.     th = NULL;     
  9. }     
  10. Widget::~Widget()     
  11. {     
  12.     delete ui;     
  13. }     
  14. void Widget::changeEvent(QEvent *e)     
  15. {     
  16.     QWidget::changeEvent(e);     
  17.     switch (e->type()) {     
  18.     case QEvent::LanguageChange:     
  19.         ui->retranslateUi(this);     
  20.         break;     
  21.     default:     
  22.         break;     
  23.     }     
  24. }     
  25. void Widget::on_pushButton_clicked()     
  26. {     
  27. #if 1     
  28.     QString text = ui->comboBox->currentText();     
  29.     th = new Thread(text, this);     
  30.     th->start();     
  31.     connect(th, SIGNAL(serialFinished(QByteArray)), this, SLOT(ReadData(QByteArray)));     
  32. #endif     
  33. }     
  34. void Widget::ReadData(QByteArray temp)     
  35. {     
  36. #if 1     
  37.     ui->textBrowser->insertPlainText(temp);     
  38.     ui->textBrowser->insertPlainText(tr("\n\n"));     
  39. #endif     
  40. }     
  41. void Widget::closeEvent(QCloseEvent *event)     
  42. {     
  43.     if(th!=NULL)     
  44.     {     
  45.         th->stopThread();     
  46.         th->wait();     
  47.     }     
  48.     event->accept();     
  49. }    
  50. #include "widget.h"  
  51. #include "ui_widget.h"  
  52. Widget::Widget(QWidget *parent) :  
  53.     QWidget(parent),  
  54.     ui(new Ui::Widget)  
  55. {  
  56.     ui->setupUi(this);  
  57.     th = NULL;  
  58. }  
  59. Widget::~Widget()  
  60. {  
  61.     delete ui;  
  62. }  
  63. void Widget::changeEvent(QEvent *e)  
  64. {  
  65.     QWidget::changeEvent(e);  
  66.     switch (e->type()) {  
  67.     case QEvent::LanguageChange:  
  68.         ui->retranslateUi(this);  
  69.         break;  
  70.     default:  
  71.         break;  
  72.     }  
  73. }  
  74. void Widget::on_pushButton_clicked()  
  75. {  
  76. #if 1  
  77.     QString text = ui->comboBox->currentText();  
  78.     th = new Thread(text, this);  
  79.     th->start();  
  80.     connect(th, SIGNAL(serialFinished(QByteArray)), this, SLOT(ReadData(QByteArray)));  
  81. #endif  
  82. }  
  83. void Widget::ReadData(QByteArray temp)  
  84. {  
  85. #if 1  
  86.     ui->textBrowser->insertPlainText(temp);  
  87.     ui->textBrowser->insertPlainText(tr("\n\n"));  
  88. #endif  
  89. }  
  90. void Widget::closeEvent(QCloseEvent *event)  
  91. {  
  92.     if(th!=NULL)  
  93.     {  
  94.         th->stopThread();  
  95.         th->wait();  
  96.     }  
  97.     event->accept();  

closeEvent()在关闭窗口时被调用;

wait()函数类似于 pthread_join(),等待一个线程的结束,并进行资源回收。

main.cpp文件:

  1. #include <QtGui/QApplication>     
  2. #include "widget.h"     
  3. int main(int argc, char *argv[])     
  4. {     
  5.     QApplication a(argc, argv);     
  6.     Widget w;     
  7.     w.show();     
  8.     return a.exec();     
  9. }    
  10. #include <QtGui/QApplication> 
  11. #include "widget.h"  
  12. int main(int argc, char *argv[])  
  13. {  
  14.     QApplication a(argc, argv);  
  15.     Widget w;  
  16.     w.show();  
  17.     return a.exec();  

串口通信的内容请查看http://www.yafeilinux.com/?p=820

小结:关于详解 QT 线程 串口接收程序的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-06-29 13:50:15

Qt 串口

2011-06-29 14:06:15

Qt 串口

2011-06-29 14:42:06

Qt 串口

2011-06-29 14:23:08

Qt 串口

2011-06-29 14:32:25

Qt 串口

2011-07-01 10:35:20

QT 多线程 TCP

2023-11-15 13:55:32

2011-06-30 17:21:56

Qt 线程 共享

2009-08-25 17:24:55

C#串口通信程序

2011-06-22 17:49:35

Linux Qt 串口

2011-06-30 10:50:39

Qt OpenCV

2011-06-28 13:38:15

Arm linux QT

2011-06-22 14:30:44

QT 多线程 线程

2011-06-13 17:46:07

Qt 串口通信

2011-07-05 17:54:43

QT Sqlite ARM

2011-06-28 10:38:52

QT 文件

2011-06-22 14:38:09

QT 多线程 线程安全

2021-05-06 08:54:39

串口DMASTM32

2011-06-30 18:03:58

QT 多线程 服务器

2011-06-23 13:25:42

QT 源码 窗口
点赞
收藏

51CTO技术栈公众号