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

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

Linux下 QT 实现串口通讯小实例(linux下qt串口通信)  Linux 串口 通讯 第1张

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

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

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

转载请说明出处
知优网 » Linux下 QT 实现串口通讯小实例(linux下qt串口通信)

发表评论

您需要后才能发表评论