Qt串口通信学习

发布于:2025-09-09 ⋅ 阅读:(23) ⋅ 点赞:(0)

Qt串口通信学习

一、项目概述

本项目基于Qt框架实现了串口通信功能,支持串口参数配置、数据收发、串口状态管理等,适合初学者学习Qt串口模块的实际应用。

二、项目结构

  • 61.pro:Qt工程文件,配置模块与源码文件
  • main.cpp:程序入口,初始化应用与主窗口
  • widget.h / widget.cpp:主窗口类,包含UI与核心逻辑
  • widget.ui:界面文件(未展示,需在Qt Designer中查看)

三、环境配置

  • Qt 5.x及以上
  • 启用serialport、widgets模块
  • C++11支持

四、核心代码详解

1. main.cpp

#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

2. widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSerialPort> // 串口类
#include <QSerialPortInfo> // 串口信息类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:
    void on_pushButton_clicked(bool checked);
private:
    Ui::Widget *ui;           // UI指针
    QSerialPort *serialPort;  // 串口对象
private slots:
    void readData();          // 读取数据槽
    void on_pushButton_2_clicked(); // 发送数据按钮槽
    void on_pushButton_3_clicked(); // 清空发送区按钮槽
};
#endif // WIDGET_H

3. widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    serialPort = new QSerialPort(this);
    // 扫描本机的串口,并且添加到下拉框里
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        ui->comboBox->addItem(info.portName());
    }
    // 读取数据
    connect(serialPort, SIGNAL(readyRead()), this, SLOT(readData()));
}
Widget::~Widget()
{
    delete ui;
}
void Widget::on_pushButton_clicked(bool checked)
{
    if (checked) {
        serialPort->setPortName(ui->comboBox->currentText());
        serialPort->setBaudRate(ui->comboBox_2->currentText().toInt());
        serialPort->setStopBits(QSerialPort::StopBits(ui->comboBox_3->currentText().toInt()));
        serialPort->setDataBits(QSerialPort::DataBits(ui->comboBox_4->currentText().toInt()));
        switch (ui->comboBox_5->currentIndex()) {
        case 0:
            serialPort->setParity(QSerialPort::NoParity);
            break;
        case 1:
            serialPort->setParity(QSerialPort::EvenParity);
            break;
        case 2:
            serialPort->setParity(QSerialPort::OddParity);
            break;
        case 3:
            serialPort->setParity(QSerialPort::SpaceParity);
            break;
        case 4:
            serialPort->setParity(QSerialPort::MarkParity);
            break;
        default:
            break;
        }
        serialPort->setFlowControl(QSerialPort::NoFlowControl);
        if (!serialPort->open(QIODevice::ReadWrite)) {
            QMessageBox::about(this, "错误", "串口打开失败可能被占用了");
            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->setText("关闭串口");
    } else {
        serialPort->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->setText("打开串口");
    }
}
void Widget::readData()
{
    ui->textBrowser->insertPlainText(serialPort->readAll());
}
void Widget::on_pushButton_2_clicked()
{
    serialPort->write(ui->textEdit->toPlainText().toUtf8());
}
void Widget::on_pushButton_3_clicked()
{
    ui->textEdit->clear();
}

网站公告

今日签到

点亮在社区的每一天
去签到