欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > QT------------------串口编程

QT------------------串口编程

2025/2/2 10:26:50 来源:https://blog.csdn.net/yuanbenshidiaos/article/details/144896729  浏览:    关键词:QT------------------串口编程

实现思路

  1. QT Serial Port 模块概述
    • QT Serial Port 模块提供了 QSerialPortInfoQSerialPort 类,用于串口通信。
    • QSerialPortInfo 用于获取串口设备的信息,如名称、描述、制造商等。
    • QSerialPort 用于打开、关闭、读写串口数据。
  2. 自定义标签类 TMYLabel 设计和使用
    • 创建一个自定义的标签类 TMYLabel ,可以根据需要添加一些自定义的属性和方法。
  3. 主窗口类定义和初始化
    • 设计主窗口类,包含串口操作的相关控件,如打开、关闭、发送、接收等按钮,以及显示接收数据的控件。
  4. 通过串口读写数据
    • 使用 QSerialPort 类进行串口的读写操作,处理数据的接收和发送。

代码示例

1. 自定义标签类 TMYLabel
#include <QtWidgets/QWidget>
#include <QtWidgets/QLabel>class TMYLabel : public QLabel {Q_OBJECT
public:TMYLabel(QWidget *parent = nullptr) : QLabel(parent) {// 可以在这里添加自定义的初始化代码}// 可以添加自定义的方法和属性void setCustomText(const QString &text) {setText(text);}signals:// 可以添加自定义的信号void customClicked();protected:void mousePressEvent(QMouseEvent *event) override {emit customClicked();QLabel::mousePressEvent(event);}
};
2. 主窗口类定义和初始化,串口编程示例
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QComboBox>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QtCore/QDebug>class MainWindow : public QMainWindow {Q_OBJECT
public:MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {setWindowTitle("Serial Port Example");QWidget *centralWidget = new QWidget(this);QVBoxLayout *layout = new QVBoxLayout(centralWidget);// 串口选择下拉菜单QComboBox *portComboBox = new QComboBox();const auto infos = QSerialPortInfo::availablePorts();for (const QSerialPortInfo &info : infos) {portComboBox->addItem(info.portName());}layout->addWidget(portComboBox);// 打开串口按钮QPushButton *openButton = new QPushButton("Open Serial Port");connect(openButton, &QPushButton::clicked, this, &MainWindow::openSerialPort);layout->addWidget(openButton);// 关闭串口按钮QPushButton *closeButton = new QPushButton("Close Serial Port");connect(closeButton, &QPushButton::clicked, this, &MainWindow::closeSerialPort);layout->addWidget(closeButton);// 发送数据按钮QPushButton *sendButton = new QPushButton("Send Data");connect(sendButton, &QPushButton::clicked, this, &MainWindow::sendData);layout->addWidget(sendButton);// 接收数据显示区域QTextEdit *receiveTextEdit = new QTextEdit();receiveTextEdit->setReadOnly(true);layout->addWidget(receiveTextEdit);setCentralWidget(centralWidget);serialPort = new QSerialPort(this);connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::readData);receiveTextEdit_ = receiveTextEdit;portComboBox_ = portComboBox;}private slots:void openSerialPort() {if (serialPort->isOpen()) {serialPort->close();}QString portName = portComboBox_->currentText();serialPort->setPortName(portName);serialPort->setBaudRate(QSerialPort::Baud9600);serialPort->setDataBits(QSerialPort::Data8);serialPort->setParity(QSerialPort::NoParity);serialPort->setStopBits(QSerialPort::OneStop);serialPort->setFlowControl(QSerialPort::NoFlowControl);if (serialPort->open(QIODevice::ReadWrite)) {qDebug() << "Serial port opened successfully";} else {qDebug() << "Failed to open serial port:" << serialPort->errorString();}}void closeSerialPort() {if (serialPort->isOpen()) {serialPort->close();qDebug() << "Serial port closed";}}void sendData() {if (serialPort->isOpen()) {QByteArray data = "Hello from QT!";serialPort->write(data);} else {qDebug() << "Serial port is not open";}}void readData() {QByteArray data = serialPort->readAll();receiveTextEdit_->append(data);}private:QSerialPort *serialPort;QComboBox *portComboBox_;QTextEdit *receiveTextEdit_;
};int main(int argc, char *argv[]) {QApplication app(argc, argv);MainWindow mainWindow;mainWindow.show();return app.exec();
}#include "main.moc"

代码解释

1. 自定义标签类 TMYLabel
  • TMYLabel 类
    • 继承自 QLabel,可以添加自定义的方法,如 setCustomText 用于设置标签文本。
    • 重写 mousePressEvent 并发出自定义信号 customClicked,可用于处理鼠标点击事件。
2. 主窗口类 MainWindow
  • 构造函数

    • 创建界面元素,包括串口选择下拉菜单、打开/关闭串口按钮、发送数据按钮和接收数据显示区域。
    • 使用 QSerialPortInfo::availablePorts() 获取可用串口信息并添加到下拉菜单。
    • 连接按钮点击信号到相应的槽函数。
  • openSerialPort 槽函数

    • 配置串口参数,如波特率、数据位、校验位、停止位和流控制。
    • 尝试打开串口,根据结果输出相应信息。
  • closeSerialPort 槽函数

    • 关闭已打开的串口。
  • sendData 槽函数

    • 向串口发送数据,这里发送了一个简单的字符串。
  • readData 槽函数

    • 当串口有数据可读时,使用 readAll 读取数据并添加到 QTextEdit 中显示。

在这里插入图片描述

使用说明

  1. 将上述代码保存为 main.cpp 文件。
  2. .pro 文件中添加以下内容:
QT += serialport widgets
CONFIG += c++11
  1. 编译并运行程序。
    • 选择一个可用的串口。
    • 点击 “Open Serial Port” 打开串口。
    • 点击 “Send Data” 发送数据。
    • 接收的数据将显示在 QTextEdit 中。

与 ESP8266 模块的通信程序

  • 上述代码可用于与 ESP8266 模块通信。
  • 确保 ESP8266 模块连接到计算机的串口并已正确配置,例如设置波特率为 9600。
  • 可以修改 sendData 方法发送特定的 AT 命令,例如 serialPort->write("AT\r\n"); 发送 AT 命令给 ESP8266。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com