Qt-使用QSerialPort实现串口通信
相关资料:
https://blog.csdn.net/weixin_42837024/article/details/81669540 Qt 串口通信(QSerialPort)
https://blog.csdn.net/qq_41068712/article/details/89317764 QT5串口编程——编写简单的上位机
代码实例:
.pro
1 QT += core gui 2 QT += serialport 3 4 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 5 6 CONFIG += c++11 7 8 # The following define makes your compiler emit warnings if you use 9 # any Qt feature that has been marked deprecated (the exact warnings 10 # depend on your compiler). Please consult the documentation of the 11 # deprecated API in order to know how to port your code away from it. 12 DEFINES += QT_DEPRECATED_WARNINGS 13 14 # You can also make your code fail to compile if it uses deprecated APIs. 15 # In order to do so, uncomment the following line. 16 # You can also select to disable deprecated APIs only up to a certain version of Qt. 17 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 18 19 SOURCES += \ 20 main.cpp \ 21 mainwindow.cpp 22 23 HEADERS += \ 24 mainwindow.h 25 26 FORMS += \ 27 mainwindow.ui 28 29 # Default rules for deployment. 30 qnx: target.path = /tmp/$${TARGET}/bin 31 else: unix:!android: target.path = /opt/$${TARGET}/bin 32 !isEmpty(target.path): INSTALLS += target
main.cpp
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QMessageBox> 6 7 #include <QSerialPort> //提供访问串口的功能 8 #include <QSerialPortInfo> //提供系统中存在的串口的信息 9 10 QT_BEGIN_NAMESPACE 11 namespace Ui { class MainWindow; } 12 QT_END_NAMESPACE 13 14 class MainWindow : public QMainWindow 15 { 16 Q_OBJECT 17 18 public: 19 MainWindow(QWidget *parent = nullptr); 20 ~MainWindow(); 21 22 private slots: 23 void on_pushButton_2_clicked(); 24 25 void on_pushButton_clicked(); 26 void on_serialPortRead();// 串口读取 27 void on_pushButton_3_clicked(); 28 29 void on_pushButton_4_clicked(); 30 31 void on_pushButton_5_clicked(); 32 33 private: 34 Ui::MainWindow *ui; 35 // 串口 36 QSerialPort *m_pSerial; 37 }; 38 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 //串口对象 10 m_pSerial = new QSerialPort(this); 11 } 12 13 MainWindow::~MainWindow() 14 { 15 delete ui; 16 } 17 18 void MainWindow::on_pushButton_2_clicked() 19 { 20 ui->textEdit->clear(); 21 //通过QSerialPortInfo查找可用串口 22 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) 23 { 24 ui->textEdit->append(info.portName()); 25 } 26 } 27 28 void MainWindow::on_pushButton_clicked() 29 { 30 QString sCOMName = "COM%1"; 31 sCOMName = sCOMName.arg(ui->lineEdit->text()); 32 // //串口对象 33 // m_pSerial = new QSerialPort(this); 34 //设置串口名 35 m_pSerial->setPortName(sCOMName); 36 //设置波特率 37 m_pSerial->setBaudRate(115200); 38 //设置数据位数 39 m_pSerial->setDataBits(QSerialPort::Data8); 40 //设置奇偶校验 41 m_pSerial->setParity(QSerialPort::NoParity); 42 //设置停止位 43 m_pSerial->setStopBits(QSerialPort::OneStop); 44 //设置流控制 45 m_pSerial->setFlowControl(QSerialPort::NoFlowControl); 46 //打开串口 47 if(!m_pSerial->open(QIODevice::ReadWrite)) 48 { 49 QMessageBox::about(NULL, "提示", "无法打开串口!"); 50 return; 51 } 52 connect(m_pSerial, &QSerialPort::readyRead, this, &MainWindow::on_serialPortRead); 53 } 54 55 void MainWindow::on_serialPortRead() 56 { 57 // 从接收缓冲区中读取数据 58 QByteArray buffer = m_pSerial->readAll(); 59 // 重新显示 60 ui->textEdit_3->append(QString(buffer)); 61 } 62 63 void MainWindow::on_pushButton_3_clicked() 64 { 65 //获取界面上的数据并转换成utf8格式的字节流 66 QByteArray data = ui->textEdit_2->toPlainText().toUtf8(); 67 m_pSerial->write(data); 68 } 69 70 void MainWindow::on_pushButton_4_clicked() 71 { 72 if (m_pSerial->isOpen()) 73 { 74 m_pSerial->close(); 75 } 76 // delete m_pSerial; 77 } 78 79 void MainWindow::on_pushButton_5_clicked() 80 { 81 ui->textEdit->append(QString::number(m_pSerial->isOpen())); 82 }
mainwindow.ui
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ui version="4.0"> 3 <class>MainWindow</class> 4 <widget class="QMainWindow" name="MainWindow"> 5 <property name="geometry"> 6 <rect> 7 <x>0</x> 8 <y>0</y> 9 <width>449</width> 10 <height>314</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>MainWindow</string> 15 </property> 16 <widget class="QWidget" name="centralwidget"> 17 <widget class="QPushButton" name="pushButton"> 18 <property name="geometry"> 19 <rect> 20 <x>130</x> 21 <y>140</y> 22 <width>75</width> 23 <height>23</height> 24 </rect> 25 </property> 26 <property name="text"> 27 <string>打开串口</string> 28 </property> 29 </widget> 30 <widget class="QPushButton" name="pushButton_2"> 31 <property name="geometry"> 32 <rect> 33 <x>130</x> 34 <y>20</y> 35 <width>91</width> 36 <height>23</height> 37 </rect> 38 </property> 39 <property name="text"> 40 <string>显示所有串口</string> 41 </property> 42 </widget> 43 <widget class="QPushButton" name="pushButton_3"> 44 <property name="geometry"> 45 <rect> 46 <x>350</x> 47 <y>190</y> 48 <width>75</width> 49 <height>23</height> 50 </rect> 51 </property> 52 <property name="text"> 53 <string>写</string> 54 </property> 55 </widget> 56 <widget class="QTextEdit" name="textEdit"> 57 <property name="geometry"> 58 <rect> 59 <x>10</x> 60 <y>20</y> 61 <width>111</width> 62 <height>111</height> 63 </rect> 64 </property> 65 </widget> 66 <widget class="QLineEdit" name="lineEdit"> 67 <property name="geometry"> 68 <rect> 69 <x>10</x> 70 <y>140</y> 71 <width>111</width> 72 <height>20</height> 73 </rect> 74 </property> 75 </widget> 76 <widget class="QTextEdit" name="textEdit_2"> 77 <property name="geometry"> 78 <rect> 79 <x>250</x> 80 <y>140</y> 81 <width>181</width> 82 <height>41</height> 83 </rect> 84 </property> 85 </widget> 86 <widget class="QTextEdit" name="textEdit_3"> 87 <property name="geometry"> 88 <rect> 89 <x>250</x> 90 <y>20</y> 91 <width>181</width> 92 <height>111</height> 93 </rect> 94 </property> 95 </widget> 96 <widget class="QLabel" name="label"> 97 <property name="geometry"> 98 <rect> 99 <x>250</x> 100 <y>0</y> 101 <width>54</width> 102 <height>12</height> 103 </rect> 104 </property> 105 <property name="text"> 106 <string>读取数据:</string> 107 </property> 108 </widget> 109 <widget class="QPushButton" name="pushButton_4"> 110 <property name="geometry"> 111 <rect> 112 <x>130</x> 113 <y>170</y> 114 <width>75</width> 115 <height>23</height> 116 </rect> 117 </property> 118 <property name="text"> 119 <string>关闭串口中</string> 120 </property> 121 </widget> 122 <widget class="QPushButton" name="pushButton_5"> 123 <property name="geometry"> 124 <rect> 125 <x>130</x> 126 <y>200</y> 127 <width>75</width> 128 <height>23</height> 129 </rect> 130 </property> 131 <property name="text"> 132 <string>是否打开</string> 133 </property> 134 </widget> 135 </widget> 136 <widget class="QMenuBar" name="menubar"> 137 <property name="geometry"> 138 <rect> 139 <x>0</x> 140 <y>0</y> 141 <width>449</width> 142 <height>23</height> 143 </rect> 144 </property> 145 </widget> 146 <widget class="QStatusBar" name="statusbar"/> 147 </widget> 148 <resources/> 149 <connections/> 150 </ui>
请发表评论