Linux下 QT 实现串口通讯小实例

移动开发
本文介绍的是Linux下 QT 实现串口通讯小实例,本文基本没有介绍,基于代码实现串口通讯。请先看代码。

Linux QT 实现串口通讯小实例,代码如下

  1. view plaincopy to clipboardprint?  
  2. //ui_mainwindow.h     
  3. #ifndef UI_MAINWINDOW_H     
  4. #define UI_MAINWINDOW_H     
  5. #include <QtCore/QVariant>     
  6. #include <QtGui/QAction>     
  7. #include <QtGui/QApplication>     
  8. #include <QtGui/QButtonGroup>     
  9. #include <QtGui/QHeaderView>     
  10. #include <QtGui/QLabel>     
  11. #include <QtGui/QMainWindow>     
  12. #include <QtGui/QMenuBar>     
  13. #include <QtGui/QPushButton>     
  14. #include <QtGui/QStatusBar>     
  15. #include <QtGui/QToolBar>     
  16. #include <QtGui/QWidget>     
  17. QT_BEGIN_NAMESPACE     
  18. class Ui_MainWindow     
  19. {     
  20. public:     
  21.     QWidget *centralWidget;     
  22.     QPushButton *writeButton;     
  23.     QPushButton *readButton;     
  24.     QPushButton *closeButton;     
  25.     QLabel *dis_label;     
  26.     QMenuBar *menuBar;     
  27.     QToolBar *mainToolBar;     
  28.     QStatusBar *statusBar;     
  29.     
  30.     void setupUi(QMainWindow *MainWindow)     
  31.     {     
  32.         if (MainWindow->objectName().isEmpty())     
  33.             MainWindow->setObjectName(QString::fromUtf8("MainWindow"));     
  34.         MainWindow->resize(600, 400);     
  35.         centralWidget = new QWidget(MainWindow);     
  36.         centralWidget->setObjectName(QString::fromUtf8("centralWidget"));     
  37.         writeButton = new QPushButton(centralWidget);     
  38.         writeButton->setObjectName(QString::fromUtf8("writeButton"));     
  39.         writeButton->setGeometry(QRect(100, 210, 75, 23));     
  40.         readButton = new QPushButton(centralWidget);     
  41.         readButton->setObjectName(QString::fromUtf8("readButton"));     
  42.         readButton->setGeometry(QRect(240, 210, 75, 23));     
  43.         closeButton = new QPushButton(centralWidget);     
  44.         closeButton->setObjectName(QString::fromUtf8("closeButton"));     
  45.         closeButton->setGeometry(QRect(390, 210, 75, 23));     
  46.         dis_label = new QLabel(centralWidget);     
  47.         dis_label->setObjectName(QString::fromUtf8("dis_label"));     
  48.         dis_label->setGeometry(QRect(200, 90, 191, 16));     
  49.         MainWindow->setCentralWidget(centralWidget);     
  50.         menuBar = new QMenuBar(MainWindow);     
  51.         menuBar->setObjectName(QString::fromUtf8("menuBar"));     
  52.         menuBar->setGeometry(QRect(0, 0, 600, 19));     
  53.         MainWindow->setMenuBar(menuBar);     
  54.         mainToolBar = new QToolBar(MainWindow);     
  55.         mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));     
  56.         MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);     
  57.         statusBar = new QStatusBar(MainWindow);     
  58.         statusBar->setObjectName(QString::fromUtf8("statusBar"));     
  59.         MainWindow->setStatusBar(statusBar);     
  60.     
  61.         retranslateUi(MainWindow);     
  62.     
  63.         QMetaObject::connectSlotsByName(MainWindow);     
  64.     } // setupUi     
  65.     
  66.     void retranslateUi(QMainWindow *MainWindow)     
  67.     {     
  68.         MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));     
  69.         writeButton->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));     
  70.         readButton->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));     
  71.         closeButton->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));     
  72.         dis_label->setText(QApplication::translate("MainWindow", "TextLabel", 0, QApplication::UnicodeUTF8));     
  73.     } // retranslateUi     
  74.     
  75. };     
  76. namespace Ui {     
  77.     class MainWindow: public Ui_MainWindow {};     
  78. } // namespace Ui     
  79. QT_END_NAMESPACE     
  80. #endif // UI_MAINWINDOW_H     
  81. //thread.h     
  82. #ifndef THREAD_H     
  83. #define THREAD_H     
  84. #include<QThread>     
  85. class Thread:public QThread     
  86. {     
  87.   Q_OBJECT     
  88.  public:     
  89.   Thread();     
  90.   char buf[128];     
  91.   volatile bool stopped;     
  92.   volatile bool write_rs;     
  93.   volatile bool read_rs;     
  94.  protected:     
  95.   virtual void run();     
  96. };     
  97. #endif     
  98. //thread.cpp     
  99. #include"thread.h"     
  100. #include <sys/types.h>     
  101. #include <sys/stat.h>     
  102. #include <fcntl.h>     
  103. #include <termios.h>    //串口用到的     
  104. #include <stdio.h>     
  105. #include <stdlib.h>     
  106. #include <unistd.h>     
  107. #include <strings.h>     
  108. #define BAUDRATE B9600     
  109. #define RS_DEVICE "/dev/ttyS0"       //串口0     
  110. //#define RS_DEVICE "/dev/ttySAC1"       //串口1     
  111. Thread::Thread()     
  112. {}                                                 //析构     
  113. void Thread::run()          //这就是线程的具体工作了     
  114. {     
  115.  int fd,c=0,res;     
  116. struct termios oldtio,newtio;     //termios结构是用来保存波特率、字符大小等     
  117. printf("start...\n");     
  118. fd=open(RS_DEVICE,O_RDWR|O_NOCTTY);     //以读写方式打开串口。不控制TTY     
  119. if(fd<0)     
  120. {     
  121. perror("error");     
  122. exit(1);                             //失败退出     
  123. }     
  124. printf("Open...\n");     
  125. tcgetattr(fd,&oldtio);             //保存当前设置到oldtio     
  126. bzero(&newtio,sizeof(newtio));     //清除newtio结构,并重新对它的成员设置如下    
  127. newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;  //9600、8位、忽略DCD信号、启用接收装置     
  128. newtio.c_iflag|=IGNPAR;                    //忽略奇偶     
  129. newtio.c_oflag=0;     
  130. newtio.c_lflag=0;     
  131. newtio.c_cc[VMIN]=0;     
  132. newtio.c_cc[VTIME]=100;                   //在规定时间(VTIME)内读取(VMIN)个字符;     
  133. tcflush(fd,TCIFLUSH);                    //清除所有队列在串口的输入与输出;     
  134. tcsetattr(fd,TCSANOW,&newtio);           //把我们的设置写入termios     
  135. while(stopped)                          //stopped为0时将退出线程     
  136. {     
  137.  if(write_rs)                           //write_rs为1时把字符串从串口中输出     
  138. {     
  139. write_rs=0;     
  140. write(fd,"QtEmbedded-4.5.1",16);  //向串口中写入字符,通过串口调试助手可看到QtEmbedded-4.5.1这个字符     
  141. }     
  142.  if(read_rs)                           //read_rs为1时读取,并存在buf     
  143. {     
  144. read_rs=0;     
  145. res=read(fd,buf,10);     //读取串口的数据到buf     
  146. buf[res]=0;     
  147. emit finished();                      //读完后发一个信号     
  148. }     
  149. }     
  150. printf("Close...\n");     
  151. tcsetattr(fd,TCSANOW,&oldtio);      //重新设置回原来的     
  152. close(fd);     
  153. quit();     
  154. }     
  155. //mainwindow.h     
  156. #ifndef MAINWINDOW_H     
  157. #define MAINWINDOW_H     
  158. #include<QtGui>     
  159. #include"ui_mainwindow.h"    //奇怪?这个头文件从哪里来的?呵呵,刚才用designer做的mainwindow.ui文件,经过make后会生成这个头文件,     
  160. #include"thread.h"           //把我们前面定义的线程包含进来     
  161. class MainWindow:public QMainWindow,public Ui::MainWindow  //多继承     
  162. {     
  163.   Q_OBJECT     
  164.  public:     
  165.   MainWindow(QWidget *parent=0);     
  166.  public slots:     
  167.   void writeThread();     
  168.   void readThread();     
  169.   void closeThread();     
  170.   void display();    
  171.  private:     
  172.   Thread *yy;     
  173. };     
  174. #endif     
  175. //mainwindow.cpp     
  176. #include"mainwindow.h"    
  177. MainWindow::MainWindow(QWidget *parent)     
  178.   :QMainWindow(parent)     
  179. {     
  180.   setupUi(this);     
  181.   yy = new Thread;     
  182.   yy->start();          //启动线程     
  183.   yy->stopped=1;        //初始化变量     
  184.   yy->write_rs=0;     
  185.   yy->read_rs=0;     
  186.   connect(writeButton,SIGNAL(clicked()),this,SLOT(writeThread()));      //信号与槽     
  187.   connect(readButton,SIGNAL(clicked()),this,SLOT(readThread()));     
  188.   connect(closeButton,SIGNAL(clicked()),this,SLOT(closeThread()));     
  189.   connect(yy,SIGNAL(finished()),this,SLOT(display()));      //前面线程读完了不是发一个信号么,这个信号就是发到这个槽     
  190. }     
  191. void MainWindow::display()     
  192. {     
  193. dis_label->setText(yy->buf);     //读到的在dis_label显示,dis_label就是我们前面designer放的标签,显示buf中的内容     
  194. }     
  195. void MainWindow::writeThread()  //前面线程都是根据stopped、write_rs、read_rs的状态来工作的^_^     
  196. {     
  197.   yy->write_rs=1;     
  198. }    
  199. void MainWindow::readThread()     
  200. {     
  201.   yy->read_rs=1;     
  202. }     
  203. void MainWindow::closeThread()     
  204. {     
  205. yy->stopped=0;     
  206. }     
  207. //main.cpp     
  208. #include<QApplication>     
  209. #include"mainwindow.h"     
  210. int main(int argc,char *argv[])     
  211. {     
  212.   QApplication app(argc,argv);     
  213.   MainWindow mw;     
  214.   mw.show();     
  215.   return app.exec();     
  216. }    
  217. //ui_mainwindow.h  
  218. #ifndef UI_MAINWINDOW_H  
  219. #define UI_MAINWINDOW_H  
  220. #include <QtCore/QVariant> 
  221. #include <QtGui/QAction> 
  222. #include <QtGui/QApplication> 
  223. #include <QtGui/QButtonGroup> 
  224. #include <QtGui/QHeaderView> 
  225. #include <QtGui/QLabel> 
  226. #include <QtGui/QMainWindow> 
  227. #include <QtGui/QMenuBar> 
  228. #include <QtGui/QPushButton> 
  229. #include <QtGui/QStatusBar> 
  230. #include <QtGui/QToolBar> 
  231. #include <QtGui/QWidget> 
  232. QT_BEGIN_NAMESPACE  
  233. class Ui_MainWindow  
  234. {  
  235. public:  
  236.     QWidget *centralWidget;  
  237.     QPushButton *writeButton;  
  238.     QPushButton *readButton;  
  239.     QPushButton *closeButton;  
  240.     QLabel *dis_label;  
  241.     QMenuBar *menuBar;  
  242.     QToolBar *mainToolBar;  
  243.     QStatusBar *statusBar;  
  244.  
  245.     void setupUi(QMainWindow *MainWindow)  
  246.     {  
  247.         if (MainWindow->objectName().isEmpty())  
  248.             MainWindow->setObjectName(QString::fromUtf8("MainWindow"));  
  249.         MainWindow->resize(600, 400);  
  250.         centralWidget = new QWidget(MainWindow);  
  251.         centralWidget->setObjectName(QString::fromUtf8("centralWidget"));  
  252.         writeButton = new QPushButton(centralWidget);  
  253.         writeButton->setObjectName(QString::fromUtf8("writeButton"));  
  254.         writeButton->setGeometry(QRect(100, 210, 75, 23));  
  255.         readButton = new QPushButton(centralWidget);  
  256.         readButton->setObjectName(QString::fromUtf8("readButton"));  
  257.         readButton->setGeometry(QRect(240, 210, 75, 23));  
  258.         closeButton = new QPushButton(centralWidget);  
  259.         closeButton->setObjectName(QString::fromUtf8("closeButton"));  
  260.         closeButton->setGeometry(QRect(390, 210, 75, 23));  
  261.         dis_label = new QLabel(centralWidget);  
  262.         dis_label->setObjectName(QString::fromUtf8("dis_label"));  
  263.         dis_label->setGeometry(QRect(200, 90, 191, 16));  
  264.         MainWindow->setCentralWidget(centralWidget);  
  265.         menuBar = new QMenuBar(MainWindow);  
  266.         menuBar->setObjectName(QString::fromUtf8("menuBar"));  
  267.         menuBar->setGeometry(QRect(0, 0, 600, 19));  
  268.         MainWindow->setMenuBar(menuBar);  
  269.         mainToolBar = new QToolBar(MainWindow);  
  270.         mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));  
  271.         MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);  
  272.         statusBar = new QStatusBar(MainWindow);  
  273.         statusBar->setObjectName(QString::fromUtf8("statusBar"));  
  274.         MainWindow->setStatusBar(statusBar);  
  275.         retranslateUi(MainWindow);  
  276.         QMetaObject::connectSlotsByName(MainWindow);  
  277.     } // setupUi  
  278.     void retranslateUi(QMainWindow *MainWindow)  
  279.     {  
  280.         MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));  
  281.         writeButton->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));  
  282.         readButton->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));  
  283.         closeButton->setText(QApplication::translate("MainWindow", "PushButton", 0, QApplication::UnicodeUTF8));  
  284.         dis_label->setText(QApplication::translate("MainWindow", "TextLabel", 0, QApplication::UnicodeUTF8));  
  285.     } // retranslateUi  
  286. };  
  287. namespace Ui {  
  288.     class MainWindow: public Ui_MainWindow {};  
  289. } // namespace Ui  
  290. QT_END_NAMESPACE  
  291. #endif // UI_MAINWINDOW_H  
  292. //thread.h  
  293. #ifndef THREAD_H  
  294. #define THREAD_H  
  295. #include<QThread> 
  296. class Thread:public QThread  
  297. {  
  298.   Q_OBJECT  
  299.  public:  
  300.   Thread();  
  301.   char buf[128];  
  302.   volatile bool stopped;  
  303.   volatile bool write_rs;  
  304.   volatile bool read_rs;  
  305.  protected:  
  306.   virtual void run();  
  307. };  
  308. #endif  
  309. //thread.cpp  
  310. #include"thread.h"  
  311. #include <sys/types.h> 
  312. #include <sys/stat.h> 
  313. #include <fcntl.h> 
  314. #include <termios.h>    //串口用到的  
  315. #include <stdio.h> 
  316. #include <stdlib.h> 
  317. #include <unistd.h> 
  318. #include <strings.h> 
  319. #define BAUDRATE B9600  
  320. #define RS_DEVICE "/dev/ttyS0"       //串口0  
  321. //#define RS_DEVICE "/dev/ttySAC1"       //串口1  
  322. Thread::Thread()  
  323. {}                    //析构  
  324. void Thread::run()       //这就是线程的具体工作了  
  325. {  
  326.  int fd,c=0,res;  
  327. struct termios oldtio,newtio;     //termios结构是用来保存波特率、字符大小等  
  328. printf("start...\n");  
  329. fd=open(RS_DEVICE,O_RDWR|O_NOCTTY);     //以读写方式打开串口。不控制TTY  
  330. if(fd<0)  
  331. {  
  332. perror("error");  
  333. exit(1);                             //失败退出  
  334. }  
  335. printf("Open...\n");  
  336. tcgetattr(fd,&oldtio);             //保存当前设置到oldtio  
  337. bzero(&newtio,sizeof(newtio));     //清除newtio结构,并重新对它的成员设置如下  
  338. newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;  //9600、8位、忽略DCD信号、启用接收装置  
  339. newtio.c_iflag|=IGNPAR;                    //忽略奇偶  
  340. newtio.c_oflag=0;  
  341. newtio.c_lflag=0;  
  342. newtio.c_cc[VMIN]=0;  
  343. newtio.c_cc[VTIME]=100;                   //在规定时间(VTIME)内读取(VMIN)个字符;  
  344. tcflush(fd,TCIFLUSH);                    //清除所有队列在串口的输入与输出;  
  345. tcsetattr(fd,TCSANOW,&newtio);           //把我们的设置写入termios  
  346. while(stopped)                          //stopped为0时将退出线程  
  347. {  
  348.  if(write_rs)                           //write_rs为1时把字符串从串口中输出  
  349. {  
  350. write_rs=0;  
  351. write(fd,"QtEmbedded-4.5.1",16);  //向串口中写入字符,通过串口调试助手可看到QtEmbedded-4.5.1这个字符  
  352. }  
  353.  if(read_rs)                           //read_rs为1时读取,并存在buf  
  354. {  
  355. read_rs=0;  
  356. res=read(fd,buf,10);     //读取串口的数据到buf  
  357. buf[res]=0;  
  358. emit finished();                      //读完后发一个信号  
  359. }  
  360. }  
  361. printf("Close...\n");  
  362. tcsetattr(fd,TCSANOW,&oldtio);      //重新设置回原来的  
  363. close(fd);  
  364. quit();  
  365. }  
  366. //mainwindow.h  
  367. #ifndef MAINWINDOW_H  
  368. #define MAINWINDOW_H  
  369. #include<QtGui> 
  370. #include"ui_mainwindow.h"    //奇怪?这个头文件从哪里来的?呵呵,刚才用designer做的mainwindow.ui文件,经过make后会生成这个头文件,  
  371. #include"thread.h"        //把我们前面定义的线程包含进来  
  372. class MainWindow:public QMainWindow,public Ui::MainWindow  //多继承  
  373. {  
  374.   Q_OBJECT  
  375.  public:  
  376.   MainWindow(QWidget *parent=0);  
  377.  public slots:  
  378.   void writeThread();  
  379.   void readThread();  
  380.   void closeThread();  
  381.   void display();  
  382.  private:  
  383.   Thread *yy;  
  384. };  
  385. #endif  
  386. //mainwindow.cpp  
  387. #include"mainwindow.h"  
  388. MainWindow::MainWindow(QWidget *parent)  
  389.   :QMainWindow(parent)  
  390. {  
  391.   setupUi(this);  
  392.   yy = new Thread;  
  393.   yy->start();          //启动线程  
  394.   yy->stopped=1;        //初始化变量  
  395.   yy->write_rs=0;  
  396.   yy->read_rs=0;  
  397.   connect(writeButton,SIGNAL(clicked()),this,SLOT(writeThread()));      //信号与槽  
  398.   connect(readButton,SIGNAL(clicked()),this,SLOT(readThread()));  
  399.   connect(closeButton,SIGNAL(clicked()),this,SLOT(closeThread()));  
  400.   connect(yy,SIGNAL(finished()),this,SLOT(display()));      //前面线程读完了不是发一个信号么,这个信号就是发到这个槽  
  401. }  
  402. void MainWindow::display()  
  403. {  
  404. dis_label->setText(yy->buf);     //读到的在dis_label显示,dis_label就是我们前面designer放的标签,显示buf中的内容  
  405. }  
  406. void MainWindow::writeThread()  //前面线程都是根据stopped、write_rs、read_rs的状态来工作的^_^  
  407. {  
  408.   yy->write_rs=1;  
  409. }  
  410. void MainWindow::readThread()  
  411. {  
  412.   yy->read_rs=1;  
  413. }  
  414. void MainWindow::closeThread()  
  415. {  
  416. yy->stopped=0;  
  417. }  
  418. //main.cpp  
  419. #include<QApplication> 
  420. #include"mainwindow.h"  
  421. int main(int argc,char *argv[])  
  422. {  
  423.   QApplication app(argc,argv);  
  424.   MainWindow mw;  
  425.   mw.show();  
  426.   return app.exec();  

本例中用到了线程,线程有一个好处就是可以单独的去扫描串行端口,当有数据发送到端口时,显示相应的字符到显示框中。使用时注意波特率和串口的设置,这里用的波特率是9600,串口时ttyS0。

小结:LinuxQT 实现串口通讯小实例的内容介绍完了,都是代码实现,希望这些代码对你有所帮助!想要更多内容,请参考编辑推荐!

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

2011-06-22 17:49:35

Linux Qt 串口

2011-06-13 17:46:07

Qt 串口通信

2011-06-21 14:12:14

Qt Linux 登录界面

2011-08-30 16:40:56

UbuntuVMware

2011-06-30 09:46:01

QT 显示视频 linux

2011-06-29 11:22:06

Qt Windows 入口函数

2011-06-21 09:33:49

Qt 启动 界面

2011-06-27 11:08:37

Qt 串口 通信

2011-07-22 17:48:29

IOS Socket TCP

2011-06-08 14:06:42

linux SDK Qt

2011-07-05 10:55:14

Linux Qt 移植

2011-07-05 11:10:23

Linux Qt 移植

2017-03-13 16:46:11

Linuxminicomusb串口

2011-06-24 16:09:24

Qt 动画 状态机

2011-06-28 16:40:17

Qt Widget 图片

2011-07-01 13:03:32

QT 线程 串口

2011-06-21 17:01:44

Qt 静态 编译

2011-06-16 10:09:25

QT Windows DLL

2011-06-29 16:50:17

Qt 显示宽字符

2011-06-21 09:19:24

Qt 界面 多国语言
点赞
收藏

51CTO技术栈公众号