QT5串口编程

您所在的位置:网站首页 Qt中foreach的原理 QT5串口编程

QT5串口编程

2024-01-04 05:12| 来源: 网络整理| 查看: 265

文章目录 1.添加库,添加类一、设置和打开串口二、读取数据三、发送数据四、关闭串口1、创建一个新的Widgets Appliaction工程2、使用QtCreator的ui文件来设计上位机的界面,设计如下:3、mainwindow.h文件内容如下:4、mainwindow.cpp文件内容如下:5、main.cpp文件内容如下:6、测试结果 仿:1.创建QT的mianwindow工程2.对原作者的界面进行仿制,如图所示3.对STC下载器的串口进行研究获得其:波特率,停止位,数据位,校验位,停止位的选项4.制作选项,串口号是根据机器自己的监测进行生成5.分别新建4个槽函数,信号为clicked()6.我们首先将上位机的大小进行固定,防止拖动,对界面美观产生影响;将上位机的背景板移动到合适的大小,在mianwindow可以查看要设置的大小,我这里要设置的大小是430x5317.在开机程序中添加修改界面大小的程序,添加上位机的最大尺寸和最小尺寸8.进行仿真,效果如图:可以在图中看见放大按键消失了9.添加需要的类和头文件10.mianwindow.cpp11.mianwindow.h13. 产生的程序: 转自: https://blog.csdn.net/u014695839/article/details/50611549

1.添加库,添加类

首先,QT5是自带QSerialPort(Qt5封装的串口类)这个类的,使用时需要在pro文件里面添加一行:

QT += serialport

在mainwindow.cpp文件夹中加入以下两个头文件

#include //提供访问串口的功能 #include //提供系统中存在的串口的信息

QT5中,串口通信是借助一个QSerialPort的对象来实现的,在设置QSerialPort对象对串口的名称、波特率、数据位、校验位、停止位等参数后,方能对串口进行读写。下面,我总结了一下借助QSerailPort对串口进行设置、打开、读、写和关闭的过程。

一、设置和打开串口 //创建串口对象 QSerialPort serial; //设置串口名 serial.setPortName("COM3"); //设置波特率 serial.setBaudRate(QSerialPort::Baud9600); //设置数据位数 serial.setDataBits(QSerialPort::Data8); //设置奇偶校验 serial.setParity(QSerialPort::NoParity); //设置停止位 serial.setStopBits(QSerialPort::OneStop); //设置流控制 serial.setFlowControl(QSerialPort::NoFlowControl); //打开串口 serial.open(QIODevice::ReadWrite);

以上代码是QSerialPort对象的设置示例,作用是:

设置串口名为 COM3设置波特率为9600设置数据位为8位设置没有奇偶校验位设置停止位为1位设置没有流控制以可读写的方式打开串口

设置完这些就能进行读写操作了。如果遇到不理解的地方,可以选择QT的类或函数,然后按F1查阅手册。举个例子,如果我们想查看QSerialPort的其它的属性,可以选择QSerialPort的类名或成员函数,然后按F1。

二、读取数据 //从接收缓冲区中读取数据 QByteArray buffer = serial.readAll();

串口在收到数据后,会将数据存入接收缓冲区。此时,我们可以通过readAll()函数将接收缓冲区的数据读出来。当串口的接收缓冲区有数据时,QSerilaPort对象会发出一个readyRead()的信号。因此,我们可以编写一个槽函数来读数据,例如:

//连接信号和槽 QObject::connect(&serial, &QSerialPort::readyRead, this, &MainWindow::serialPort_readyRead); //…… //编写的槽函数 void MainWindow::serialPort_readyRead() { //从接收缓冲区中读取数据 QByteArray buffer = serial.readAll(); //处理数据 //…… } 三、发送数据 serial->write(data);

使用write函数便可以把字节数组中的字节发送出去。

四、关闭串口 serial->close();

串口不用时,可通过close()函数将其关闭。

接下来是一个实例

1、创建一个新的Widgets Appliaction工程 2、使用QtCreator的ui文件来设计上位机的界面,设计如下:

img

3、mainwindow.h文件内容如下: //mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include #include namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void serialPort_readyRead(); void on_searchButton_clicked(); void on_openButton_clicked(); void on_sendButton_clicked(); void on_clearButton_clicked(); private: Ui::MainWindow *ui; QSerialPort serial; }; #endif // MAINWINDOW_H 4、mainwindow.cpp文件内容如下: //mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //连接信号和槽 QObject::connect(&serial, &QSerialPort::readyRead, this, &MainWindow::serialPort_readyRead); //发送按键失能 ui->sendButton->setEnabled(false); //波特率默认选择下拉第三项:9600 ui->baudrateBox->setCurrentIndex(3); } MainWindow::~MainWindow() { delete ui; } void MainWindow::serialPort_readyRead() { //从接收缓冲区中读取数据 QByteArray buffer = serial.readAll(); //从界面中读取以前收到的数据 QString recv = ui->recvTextEdit->toPlainText(); recv += QString(buffer); //清空以前的显示 ui->recvTextEdit->clear(); //重新显示 ui->recvTextEdit->append(recv); } void MainWindow::on_searchButton_clicked() { ui->portNameBox->clear(); //通过QSerialPortInfo查找可用串口 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { ui->portNameBox->addItem(info.portName()); } } void MainWindow::on_openButton_clicked() { if(ui->openButton->text()==QString("打开串口")) { //设置串口名 serial.setPortName(ui->portNameBox->currentText()); //设置波特率 serial.setBaudRate(ui->baudrateBox->currentText().toInt()); //设置数据位数 switch(ui->dataBitsBox->currentIndex()) { case 8: serial.setDataBits(QSerialPort::Data8); break; default: break; } //设置奇偶校验 switch(ui->ParityBox->currentIndex()) { case 0: serial.setParity(QSerialPort::NoParity); break; default: break; } //设置停止位 switch(ui->stopBitsBox->currentIndex()) { case 1: serial.setStopBits(QSerialPort::OneStop); break; case 2: serial.setStopBits(QSerialPort::TwoStop); break; default: break; } //设置流控制 serial.setFlowControl(QSerialPort::NoFlowControl); //打开串口 if(!serial.open(QIODevice::ReadWrite)) { QMessageBox::about(NULL, "提示", "无法打开串口!"); return; } //下拉菜单控件失能 ui->portNameBox->setEnabled(false); ui->baudrateBox->setEnabled(false); ui->dataBitsBox->setEnabled(false); ui->ParityBox->setEnabled(false); ui->stopBitsBox->setEnabled(false); ui->openButton->setText(QString("关闭串口")); //发送按键使能 ui->sendButton->setEnabled(true); } else { //关闭串口 serial.close(); //下拉菜单控件使能 ui->portNameBox->setEnabled(true); ui->baudrateBox->setEnabled(true); ui->dataBitsBox->setEnabled(true); ui->ParityBox->setEnabled(true); ui->stopBitsBox->setEnabled(true); ui->openButton->setText(QString("打开串口")); //发送按键失能 ui->sendButton->setEnabled(false); } } void MainWindow::on_sendButton_clicked() { //获取界面上的数据并转换成utf8格式的字节流 QByteArray data = ui->sendTextEdit->toPlainText().toUtf8(); serial.write(data); } void MainWindow::on_clearButton_clicked() { ui->recvTextEdit->clear(); } 5、main.cpp文件内容如下: #include "mainwindow.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } 6、测试结果

在这里插入图片描述

仿:

新建QT程序的mainwindow,QT只要有几个文件组成: (废话) .pro文件是整个文件的开端 ,程序是从这里开始的,当然他是我们工程打开的地方;程序需要加什么类,什么文件都是在这里操作的 mianwindow.h是mainwindow.cpp的头文件,.cpp里面的东西都会在.h里面出现,.cpp里面要调用的头文件,也会在这里面进行声明; mainwindow.cpp里面包含了,开始的程序,槽函数,关闭的函数

1.创建QT的mianwindow工程 2.对原作者的界面进行仿制,如图所示

在这里插入图片描述

3.对STC下载器的串口进行研究获得其:波特率,停止位,数据位,校验位,停止位的选项

在这里插入图片描述

4.制作选项,串口号是根据机器自己的监测进行生成

在这里插入图片描述

5.分别新建4个槽函数,信号为clicked()

在这里插入图片描述

6.我们首先将上位机的大小进行固定,防止拖动,对界面美观产生影响;将上位机的背景板移动到合适的大小,在mianwindow可以查看要设置的大小,我这里要设置的大小是430x531

在这里插入图片描述

7.在开机程序中添加修改界面大小的程序,添加上位机的最大尺寸和最小尺寸

在这里插入图片描述

8.进行仿真,效果如图:可以在图中看见放大按键消失了

在这里插入图片描述

9.添加需要的类和头文件

在.pro中添加:

QT += serialport

在.h文件中添加

#include //提供访问串口的功能 #include //提供系统中存在的串口的信息 10.mianwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->comboBox->clear(); //通过QSerialPortInfo查找可用串口 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { ui->comboBox->addItem(info.portName()); } this->setMaximumSize(430,531); this->setMinimumSize(430,531); //连接信号和槽 QObject::connect(&serial, &QSerialPort::readyRead, this, &MainWindow::serialPort_readyRead); } MainWindow::~MainWindow() { delete ui; } //监测串口 void MainWindow::on_pushButton_3_clicked() { ui->comboBox->clear(); //通过QSerialPortInfo查找可用串口 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { ui->comboBox->addItem(info.portName()); } } //打开串口 void MainWindow::on_pushButton_4_clicked() { if(ui->pushButton_4->text()==QString("打开串口")) { //设置串口名 serial.setPortName(ui->comboBox->currentText()); //设置波特率 serial.setBaudRate(ui->comboBox_2->currentText().toInt()); //设置数据位数 switch(ui->comboBox_3->currentIndex()) { case 8: serial.setDataBits(QSerialPort::Data8); break; default: break; } //设置奇偶校验 switch(ui->comboBox_4->currentIndex()) { case 0: serial.setParity(QSerialPort::NoParity); break; default: break; } //设置停止位 switch(ui->comboBox_5->currentIndex()) { case 1: serial.setStopBits(QSerialPort::OneStop); break; case 2: serial.setStopBits(QSerialPort::TwoStop); break; default: break; } //设置流控制 serial.setFlowControl(QSerialPort::NoFlowControl); //打开串口 if(!serial.open(QIODevice::ReadWrite)) { QMessageBox::about(NULL,"提示","无法打开串口!"); return; } //下拉菜单控件失能 ui->comboBox->setEnabled(false); ui->comboBox_2->setEnabled(false); ui->comboBox_3->setEnabled(false); ui->comboBox_4->setEnabled(false); ui->comboBox_5->setEnabled(false); ui->pushButton_4->setText(QString("关闭串口")); //发送按键使能 ui->pushButton_2->setEnabled(true); } else { //关闭串口 serial.close(); //下拉菜单控件使能 ui->comboBox->setEnabled(true); ui->comboBox_2->setEnabled(true); ui->comboBox_3->setEnabled(true); ui->comboBox_4->setEnabled(true); ui->comboBox_5->setEnabled(true); ui->pushButton_4->setText(QString("打开串口")); //发送按键失能 ui->pushButton_2->setEnabled(false); } } //发送数据 void MainWindow::on_pushButton_2_clicked() { //获取界面上的数据并转换成utf8格式的字节流 QByteArray data = ui->textEdit_2->toPlainText().toUtf8(); serial.write(data); } //清空接收 void MainWindow::on_pushButton_clicked() { ui->textEdit->clear(); } //串口接收 void MainWindow::serialPort_readyRead() { //从接收缓冲区中读取数据 QByteArray buffer = serial.readAll(); //从界面中读取以前收到的数据 QString recv = ui->textEdit->toPlainText(); recv += QString(buffer); //清空以前的显示 ui->textEdit->clear(); //重新显示 ui->textEdit->append(recv); } 11.mianwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include //提供访问串口的功能 #include //提供系统中存在的串口的信息 namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void serialPort_readyRead(); void on_pushButton_3_clicked(); void on_pushButton_4_clicked(); void on_pushButton_2_clicked(); void on_pushButton_clicked(); private: Ui::MainWindow *ui; QSerialPort serial; }; #endif // MAINWINDOW_H 13. 产生的程序:

https://download.csdn.net/download/qq_41068712/11117436



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3