本文介绍的是Linux下 QT 实现串口通讯小实例,本文基本没有介绍,基于代码实现串口通讯。请先看代码。
- viewplaincopytoclipboardprint?
- //ui_mainwindow.h
- #ifndefUI_MAINWINDOW_H
- #defineUI_MAINWINDOW_H
- #include<QtCore/QVariant>
- #include<QtGui/QAction>
- #include<QtGui/QApplication>
- #include<QtGui/QButtonGroup>
- #include<QtGui/QHeaderView>
- #include<QtGui/QLabel>
- #include<QtGui/QMainWindow>
- #include<QtGui/QMenuBar>
- #include<QtGui/QPushButton>
- #include<QtGui/QStatusBar>
- #include<QtGui/QToolBar>
- #include<QtGui/QWidget>
- QT_BEGIN_NAMESPACE
- classUi_MainWindow
- {
- public:
- QWidget*centralWidget;
- QPushButton*writeButton;
- QPushButton*readButton;
- QPushButton*closeButton;
- QLabel*dis_label;
- QMenuBar*menuBar;
- QToolBar*mainToolBar;
- QStatusBar*statusBar;
- voidsetupUi(QMainWindow*MainWindow)
- {
- if(MainWindow->objectName().isEmpty())
- MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
- MainWindow->resize(600,400);
- centralWidget=newQWidget(MainWindow);
- centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
- writeButton=newQPushButton(centralWidget);
- writeButton->setObjectName(QString::fromUtf8("writeButton"));
- writeButton->setGeometry(QRect(100,210,75,23));
- readButton=newQPushButton(centralWidget);
- readButton->setObjectName(QString::fromUtf8("readButton"));
- readButton->setGeometry(QRect(240,210,75,23));
- closeButton=newQPushButton(centralWidget);
- closeButton->setObjectName(QString::fromUtf8("closeButton"));
- closeButton->setGeometry(QRect(390,210,75,23));
- dis_label=newQLabel(centralWidget);
- dis_label->setObjectName(QString::fromUtf8("dis_label"));
- dis_label->setGeometry(QRect(200,90,191,16));
- MainWindow->setCentralWidget(centralWidget);
- menuBar=newQMenuBar(MainWindow);
- menuBar->setObjectName(QString::fromUtf8("menuBar"));
- menuBar->setGeometry(QRect(0,0,600,19));
- MainWindow->setMenuBar(menuBar);
- mainToolBar=newQToolBar(MainWindow);
- mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
- MainWindow->addToolBar(Qt::TopToolBarArea,mainToolBar);
- statusBar=newQStatusBar(MainWindow);
- statusBar->setObjectName(QString::fromUtf8("statusBar"));
- MainWindow->setStatusBar(statusBar);
- retranslateUi(MainWindow);
- QMetaObject::connectSlotsByName(MainWindow);
- }//setupUi
- voidretranslateUi(QMainWindow*MainWindow)
- {
- MainWindow->setWindowTitle(QApplication::translate("MainWindow","MainWindow",0,QApplication::UnicodeUTF8));
- writeButton->setText(QApplication::translate("MainWindow","PushButton",0,QApplication::UnicodeUTF8));
- readButton->setText(QApplication::translate("MainWindow","PushButton",0,QApplication::UnicodeUTF8));
- closeButton->setText(QApplication::translate("MainWindow","PushButton",0,QApplication::UnicodeUTF8));
- dis_label->setText(QApplication::translate("MainWindow","TextLabel",0,QApplication::UnicodeUTF8));
- }//retranslateUi
- };
- namespaceUi{
- classMainWindow:publicUi_MainWindow{};
- }//namespaceUi
- QT_END_NAMESPACE
- #endif//UI_MAINWINDOW_H
- //thread.h
- #ifndefTHREAD_H
- #defineTHREAD_H
- #include<QThread>
- classThread:publicQThread
- {
- Q_OBJECT
- public:
- Thread();
- charbuf[128];
- volatileboolstopped;
- volatileboolwrite_rs;
- volatileboolread_rs;
- protected:
- virtualvoidrun();
- };
- #endif
- //thread.cpp
- #include"thread.h"
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<termios.h>//串口用到的
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<strings.h>
- #defineBAUDRATEB9600
- #defineRS_DEVICE"/dev/ttyS0"//串口0
- //#defineRS_DEVICE"/dev/ttySAC1"//串口1
- Thread::Thread()
- {}//析构
- voidThread::run()//这就是线程的具体工作了
- {
- intfd,c=0,res;
- structtermiosoldtio,newtio;//termios结构是用来保存波特率、字符大小等
- printf("start...\n");
- fd=open(RS_DEVICE,O_RDWR|O_NOCTTY);//以读写方式打开串口。不控制TTY
- if(fd<0)
- {
- perror("error");
- exit(1);//失败退出
- }
- printf("Open...\n");
- tcgetattr(fd,&oldtio);//保存当前设置到oldtio
- bzero(&newtio,sizeof(newtio));//清除newtio结构,并重新对它的成员设置如下
- newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;//9600、8位、忽略DCD信号、启用接收装置
- newtio.c_iflag|=IGNPAR;//忽略奇偶
- newtio.c_oflag=0;
- newtio.c_lflag=0;
- newtio.c_cc[VMIN]=0;
- newtio.c_cc[VTIME]=100;//在规定时间(VTIME)内读取(VMIN)个字符;
- tcflush(fd,TCIFLUSH);//清除所有队列在串口的输入与输出;
- tcsetattr(fd,TCSANOW,&newtio);//把我们的设置写入termios
- while(stopped)//stopped为0时将退出线程
- {
- if(write_rs)//write_rs为1时把字符串从串口中输出
- {
- write_rs=0;
- write(fd,"QtEmbedded-4.5.1",16);//向串口中写入字符,通过串口调试助手可看到QtEmbedded-4.5.1这个字符
- }
- if(read_rs)//read_rs为1时读取,并存在buf
- {
- read_rs=0;
- res=read(fd,buf,10);//读取串口的数据到buf
- buf[res]=0;
- emitfinished();//读完后发一个信号
- }
- }
- printf("Close...\n");
- tcsetattr(fd,TCSANOW,&oldtio);//重新设置回原来的
- close(fd);
- quit();
- }
- //mainwindow.h
- #ifndefMAINWINDOW_H
- #defineMAINWINDOW_H
- #include<QtGui>
- #include"ui_mainwindow.h"//奇怪?这个头文件从哪里来的?呵呵,刚才用designer做的mainwindow.ui文件,经过make后会生成这个头文件,
- #include"thread.h"//把我们前面定义的线程包含进来
- classMainWindow:publicQMainWindow,publicUi::MainWindow//多继承
- {
- Q_OBJECT
- public:
- MainWindow(QWidget*parent=0);
- publicslots:
- voidwriteThread();
- voidreadThread();
- voidcloseThread();
- voiddisplay();
- private:
- Thread*yy;
- };
- #endif
- //mainwindow.cpp
- #include"mainwindow.h"
- MainWindow::MainWindow(QWidget*parent)
- :QMainWindow(parent)
- {
- setupUi(this);
- yy=newThread;
- yy->start();//启动线程
- yy->stopped=1;//初始化变量
- yy->write_rs=0;
- yy->read_rs=0;
- connect(writeButton,SIGNAL(clicked()),this,SLOT(writeThread()));//信号与槽
- connect(readButton,SIGNAL(clicked()),this,SLOT(readThread()));
- connect(closeButton,SIGNAL(clicked()),this,SLOT(closeThread()));
- connect(yy,SIGNAL(finished()),this,SLOT(display()));//前面线程读完了不是发一个信号么,这个信号就是发到这个槽
- }
- voidMainWindow::display()
- {
- dis_label->setText(yy->buf);//读到的在dis_label显示,dis_label就是我们前面designer放的标签,显示buf中的内容
- }
- voidMainWindow::writeThread()//前面线程都是根据stopped、write_rs、read_rs的状态来工作的^_^
- {
- yy->write_rs=1;
- }
- voidMainWindow::readThread()
- {
- yy->read_rs=1;
- }
- voidMainWindow::closeThread()
- {
- yy->stopped=0;
- }
- //main.cpp
- #include<QApplication>
- #include"mainwindow.h"
- intmain(intargc,char*argv[])
- {
- QApplicationapp(argc,argv);
- MainWindowmw;
- mw.show();
- returnapp.exec();
- }
- //ui_mainwindow.h
- #ifndefUI_MAINWINDOW_H
- #defineUI_MAINWINDOW_H
- #include<QtCore/QVariant>
- #include<QtGui/QAction>
- #include<QtGui/QApplication>
- #include<QtGui/QButtonGroup>
- #include<QtGui/QHeaderView>
- #include<QtGui/QLabel>
- #include<QtGui/QMainWindow>
- #include<QtGui/QMenuBar>
- #include<QtGui/QPushButton>
- #include<QtGui/QStatusBar>
- #include<QtGui/QToolBar>
- #include<QtGui/QWidget>
- QT_BEGIN_NAMESPACE
- classUi_MainWindow
- {
- public:
- QWidget*centralWidget;
- QPushButton*writeButton;
- QPushButton*readButton;
- QPushButton*closeButton;
- QLabel*dis_label;
- QMenuBar*menuBar;
- QToolBar*mainToolBar;
- QStatusBar*statusBar;
- voidsetupUi(QMainWindow*MainWindow)
- {
- if(MainWindow->objectName().isEmpty())
- MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
- MainWindow->resize(600,400);
- centralWidget=newQWidget(MainWindow);
- centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
- writeButton=newQPushButton(centralWidget);
- writeButton->setObjectName(QString::fromUtf8("writeButton"));
- writeButton->setGeometry(QRect(100,210,75,23));
- readButton=newQPushButton(centralWidget);
- readButton->setObjectName(QString::fromUtf8("readButton"));
- readButton->setGeometry(QRect(240,210,75,23));
- closeButton=newQPushButton(centralWidget);
- closeButton->setObjectName(QString::fromUtf8("closeButton"));
- closeButton->setGeometry(QRect(390,210,75,23));
- dis_label=newQLabel(centralWidget);
- dis_label->setObjectName(QString::fromUtf8("dis_label"));
- dis_label->setGeometry(QRect(200,90,191,16));
- MainWindow->setCentralWidget(centralWidget);
- menuBar=newQMenuBar(MainWindow);
- menuBar->setObjectName(QString::fromUtf8("menuBar"));
- menuBar->setGeometry(QRect(0,0,600,19));
- MainWindow->setMenuBar(menuBar);
- mainToolBar=newQToolBar(MainWindow);
- mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
- MainWindow->addToolBar(Qt::TopToolBarArea,mainToolBar);
- statusBar=newQStatusBar(MainWindow);
- statusBar->setObjectName(QString::fromUtf8("statusBar"));
- MainWindow->setStatusBar(statusBar);
- retranslateUi(MainWindow);
- QMetaObject::connectSlotsByName(MainWindow);
- }//setupUi
- voidretranslateUi(QMainWindow*MainWindow)
- {
- MainWindow->setWindowTitle(QApplication::translate("MainWindow","MainWindow",0,QApplication::UnicodeUTF8));
- writeButton->setText(QApplication::translate("MainWindow","PushButton",0,QApplication::UnicodeUTF8));
- readButton->setText(QApplication::translate("MainWindow","PushButton",0,QApplication::UnicodeUTF8));
- closeButton->setText(QApplication::translate("MainWindow","PushButton",0,QApplication::UnicodeUTF8));
- dis_label->setText(QApplication::translate("MainWindow","TextLabel",0,QApplication::UnicodeUTF8));
- }//retranslateUi
- };
- namespaceUi{
- classMainWindow:publicUi_MainWindow{};
- }//namespaceUi
- QT_END_NAMESPACE
- #endif//UI_MAINWINDOW_H
- //thread.h
- #ifndefTHREAD_H
- #defineTHREAD_H
- #include<QThread>
- classThread:publicQThread
- {
- Q_OBJECT
- public:
- Thread();
- charbuf[128];
- volatileboolstopped;
- volatileboolwrite_rs;
- volatileboolread_rs;
- protected:
- virtualvoidrun();
- };
- #endif
- //thread.cpp
- #include"thread.h"
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<termios.h>//串口用到的
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<strings.h>
- #defineBAUDRATEB9600
- #defineRS_DEVICE"/dev/ttyS0"//串口0
- //#defineRS_DEVICE"/dev/ttySAC1"//串口1
- Thread::Thread()
- {}//析构
- voidThread::run()//这就是线程的具体工作了
- {
- intfd,c=0,res;
- structtermiosoldtio,newtio;//termios结构是用来保存波特率、字符大小等
- printf("start...\n");
- fd=open(RS_DEVICE,O_RDWR|O_NOCTTY);//以读写方式打开串口。不控制TTY
- if(fd<0)
- {
- perror("error");
- exit(1);//失败退出
- }
- printf("Open...\n");
- tcgetattr(fd,&oldtio);//保存当前设置到oldtio
- bzero(&newtio,sizeof(newtio));//清除newtio结构,并重新对它的成员设置如下
- newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;//9600、8位、忽略DCD信号、启用接收装置
- newtio.c_iflag|=IGNPAR;//忽略奇偶
- newtio.c_oflag=0;
- newtio.c_lflag=0;
- newtio.c_cc[VMIN]=0;
- newtio.c_cc[VTIME]=100;//在规定时间(VTIME)内读取(VMIN)个字符;
- tcflush(fd,TCIFLUSH);//清除所有队列在串口的输入与输出;
- tcsetattr(fd,TCSANOW,&newtio);//把我们的设置写入termios
- while(stopped)//stopped为0时将退出线程
- {
- if(write_rs)//write_rs为1时把字符串从串口中输出
- {
- write_rs=0;
- write(fd,"QtEmbedded-4.5.1",16);//向串口中写入字符,通过串口调试助手可看到QtEmbedded-4.5.1这个字符
- }
- if(read_rs)//read_rs为1时读取,并存在buf
- {
- read_rs=0;
- res=read(fd,buf,10);//读取串口的数据到buf
- buf[res]=0;
- emitfinished();//读完后发一个信号
- }
- }
- printf("Close...\n");
- tcsetattr(fd,TCSANOW,&oldtio);//重新设置回原来的
- close(fd);
- quit();
- }
- //mainwindow.h
- #ifndefMAINWINDOW_H
- #defineMAINWINDOW_H
- #include<QtGui>
- #include"ui_mainwindow.h"//奇怪?这个头文件从哪里来的?呵呵,刚才用designer做的mainwindow.ui文件,经过make后会生成这个头文件,
- #include"thread.h"//把我们前面定义的线程包含进来
- classMainWindow:publicQMainWindow,publicUi::MainWindow//多继承
- {
- Q_OBJECT
- public:
- MainWindow(QWidget*parent=0);
- publicslots:
- voidwriteThread();
- voidreadThread();
- voidcloseThread();
- voiddisplay();
- private:
- Thread*yy;
- };
- #endif
- //mainwindow.cpp
- #include"mainwindow.h"
- MainWindow::MainWindow(QWidget*parent)
- :QMainWindow(parent)
- {
- setupUi(this);
- yy=newThread;
- yy->start();//启动线程
- yy->stopped=1;//初始化变量
- yy->write_rs=0;
- yy->read_rs=0;
- connect(writeButton,SIGNAL(clicked()),this,SLOT(writeThread()));//信号与槽
- connect(readButton,SIGNAL(clicked()),this,SLOT(readThread()));
- connect(closeButton,SIGNAL(clicked()),this,SLOT(closeThread()));
- connect(yy,SIGNAL(finished()),this,SLOT(display()));//前面线程读完了不是发一个信号么,这个信号就是发到这个槽
- }
- voidMainWindow::display()
- {
- dis_label->setText(yy->buf);//读到的在dis_label显示,dis_label就是我们前面designer放的标签,显示buf中的内容
- }
- voidMainWindow::writeThread()//前面线程都是根据stopped、write_rs、read_rs的状态来工作的^_^
- {
- yy->write_rs=1;
- }
- voidMainWindow::readThread()
- {
- yy->read_rs=1;
- }
- voidMainWindow::closeThread()
- {
- yy->stopped=0;
- }
- //main.cpp
- #include<QApplication>
- #include"mainwindow.h"
- intmain(intargc,char*argv[])
- {
- QApplicationapp(argc,argv);
- MainWindowmw;
- mw.show();
- returnapp.exec();
- }
本例中用到了线程,线程有一个好处就是可以单独的去扫描串行端口,当有数据发送到端口时,显示相应的字符到显示框中。使用时注意波特率和串口的设置,这里用的波特率是9600,串口时ttyS0。
小结:Linux下 QT 实现串口通讯小实例的内容介绍完了,都是代码实现,希望这些代码对你有所帮助!想要更多内容,请参考编辑推荐!
转载请说明出处
知优网 » Linux下 QT 实现串口通讯小实例(linux下qt串口通信)
知优网 » Linux下 QT 实现串口通讯小实例(linux下qt串口通信)