提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录- qt系列之一个简单上位机
- 前言
- 一、实现简单读取数据
- 二、使用步骤
- 1.项目文件,在里面增加一个serialport
- 2.头文件,写函数声明
- 3.类中函数的实现
- 4.主函数,生成可执行文件
- 5.ui文件,注意:ui文件不可修改
- 总结
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,串口通信这门技术也越来越重要,很多人都开启了学习串口,本文就介绍了串口通信的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、实现简单读取数据使用qt5的QSerialPort类,所有代码都在文中。
采用qt5 c++实现,
创建一个qt工程文件夹,名字命名为samp2_7,基类选择widget类。
//samp2_7.pro
#-------------------------------------------------
#
# Project created by QtCreator 2022-07-18T17:43:55
#
#-------------------------------------------------
QT += core gui
QT += serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = samp2_7
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES +=
main.cpp
widget.cpp
HEADERS +=
widget.h
FORMS +=
widget.ui
2.头文件,写函数声明
//widget.h #ifndef WIDGET_H #define WIDGET_H #include3.类中函数的实现#include #include namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_openPortButton_clicked(); void read_com(); private: Ui::Widget *ui; QSerialPort serial1; }; #endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_openPortButton_clicked()
{
//打开串口
if(ui->portNameBox->isEnabled()) //如果串口能够操作
{
ui->openPortButton->setText("closePort"); //调用函数修改按钮显示为closePort
ui->portNameBox->setDisabled(true); //禁止再修改COM串口
serial1.setPortName(ui->portNameBox->currentText()); //得到串口名,设置COM口
serial1.setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections); //设置波特率和读写方向
serial1.open(QIODevice::ReadWrite);
}
else{
ui->openPortButton->setText("openPort"); //按下closeport之后,按键显示为openport
ui->portNameBox->setEnabled(true); //按下closeport后,COM口可以修改
serial1.close(); //关闭串口
}
}
//槽函数,在槽函数之后使用readAll()把收到的数据读到requestData中。
void Widget::read_com()
{
QByteArray requestData;
requestData = serial1.readAll();
QString buf;
ui->plainTextEdit->appendPlainText(requestData.toHex());
}
4.主函数,生成可执行文件
//main.cpp #include "widget.h" #include5.ui文件,注意:ui文件不可修改int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
//widget.uiWidget 0 0 400 300 Widget 20 10 351 181 50 230 87 22 1200 2400 4800 9600 12500 240 230 93 28 open
生成的可执行文件:
提示:这里对文章进行总结:
以上就是今天要讲的内容,本文仅仅简单介绍了qt串口组件的简单使用,而qt提供了大量能使我们快速便捷地处理串口数据的函数和方法。



