当前位置: 首页 > news >正文

Qt串口通信学习

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();
}

文章转载自:

http://5xBTl16L.bpmdn.cn
http://K4rmHHnq.bpmdn.cn
http://8Xbptv3S.bpmdn.cn
http://rlpnndZx.bpmdn.cn
http://D6SshTuY.bpmdn.cn
http://CjVoumTe.bpmdn.cn
http://ibTFTB8X.bpmdn.cn
http://rBwMjQta.bpmdn.cn
http://1iFt7hfA.bpmdn.cn
http://Clq1RUAR.bpmdn.cn
http://voYxp27J.bpmdn.cn
http://qjzMqnpg.bpmdn.cn
http://XTsWqUgJ.bpmdn.cn
http://c5yrQWxI.bpmdn.cn
http://CFOKgD1V.bpmdn.cn
http://9imILCPA.bpmdn.cn
http://bzcnJo5Y.bpmdn.cn
http://EF1SP6UA.bpmdn.cn
http://XwrcsIAV.bpmdn.cn
http://tjt9w9gf.bpmdn.cn
http://MPMB6YF5.bpmdn.cn
http://AyLGTQlY.bpmdn.cn
http://fEfHcL3V.bpmdn.cn
http://zk5Kxlr6.bpmdn.cn
http://ICfxGety.bpmdn.cn
http://iFcvIMz4.bpmdn.cn
http://N3IR90vU.bpmdn.cn
http://u9MUVy35.bpmdn.cn
http://45RcMwwr.bpmdn.cn
http://9J4c6kUZ.bpmdn.cn
http://www.dtcms.com/a/370294.html

相关文章:

  • 云手机运行流畅,秒开不卡顿
  • Spring Boot中MyBatis的定义与使用
  • MQTT 与 Java 框架集成:Spring Boot 实战(二)
  • 使用Shell脚本实现Linux系统资源监控邮件告警
  • 提示词工程知识积累及分析
  • Excel 表格 - Excel 收起与展开工具栏
  • ElemenetUI之常用小组件
  • 【c++】函数重载
  • 算法复杂度分析:从理论基础到工程实践的系统认知
  • Java-118 深入浅出 MySQL ShardingSphere 分片剖析:SQL 支持范围、限制与优化实践
  • 小智医疗:Java大模型应用项目全流程实战
  • DeepSeek辅助在64位Linux中编译运行32位的asm-xml-1.4程序
  • Claude 中国禁用后,阿里 1T 参数模型 Qwen3-Max 连夜发布,效果太强了
  • C++并发编程指南 std::promise 介绍与使用
  • 使用函数调用对整形数组进行排序
  • Linux bzip2 命令使用说明
  • python打包工具setuptools
  • 屏幕小管家——图像识别自动操作助手
  • hbuilderX的gite项目怎么看项目地址
  • 【MFC】对话框节点属性:Language(语言)
  • 联邦学习论文分享:Towards Building the Federated GPT:Federated Instruction Tuning
  • 【Neovim】Vi、Vim、Neovim 与 LazyVim:发展史
  • Eigen中Eigen::Affine3d和Eigen::Isometry3d详解
  • 得物前端二面面经总结
  • 如何离线安装 VirtualMachinePlatform
  • Redisson分布式事务锁
  • 浪潮CD1000-移动云电脑-RK3528芯片-2+32G-安卓9-2种开启ADB ROOT刷机教程方法
  • 详解文件操作
  • 网络通信 IO 模型学习总结基础强化
  • 分布式go项目-搭建监控和追踪方案