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

Qt 监控串口设备热插拔的方法

一、监控串口设备热插拔的方法

Qt 提供了多种方法来监控串口设备的热插拔事件,可以通过以下方法实现:

1、使用 QSerialPortInfo 轮询检测

定期调用 QSerialPortInfo::availablePorts() 检查可用串口列表的变化。通过比较前后两次的列表差异,可以判断设备是否被插入或拔出。

QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, [=]() {static QList<QSerialPortInfo> previousPorts = QSerialPortInfo::availablePorts();QList<QSerialPortInfo> currentPorts = QSerialPortInfo::availablePorts();if (currentPorts.size() > previousPorts.size()) {// 新设备插入} else if (currentPorts.size() < previousPorts.size()) {// 设备拔出}previousPorts = currentPorts;
});
timer->start(1000); // 每秒检查一次

2、使用平台特定事件监听(Linux)

在 Linux 系统下,可以通过监听 udev 事件来检测串口设备的热插拔。使用 QUdev 或直接通过文件系统监控 /dev 目录变化。

QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
watcher->addPath("/dev");
connect(watcher, &QFileSystemWatcher::directoryChanged, [=](const QString &path) {// 检查 /dev 目录变化
});

3、重写nativeEvent函数


bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, qintptr *result)
{MSG* msg = reinterpret_cast<MSG*>(message);if(msg->message == WM_DEVICECHANGE){qDebug()<<QSerialPortInfo::availablePorts().size();foreach (auto info, QSerialPortInfo::availablePorts()){qDebug()<<info.portName();}}return QMainWindow::nativeEvent(eventType, message, result);
}

二、实例展示

mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QSerialPortInfo>QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();protected:bool nativeEvent(const QByteArray &eventType, void *message, qintptr *result) override;
private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <windows.h>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, qintptr *result)
{MSG* msg = reinterpret_cast<MSG*>(message);if(msg->message == WM_DEVICECHANGE){qDebug()<<QSerialPortInfo::availablePorts().size();foreach (auto info, QSerialPortInfo::availablePorts()){qDebug()<<info.portName();}}return QMainWindow::nativeEvent(eventType, message, result);
}

在这里插入图片描述
在这里插入图片描述

http://www.dtcms.com/a/284357.html

相关文章:

  • javaweb学习开发代码_HTML-CSS-JS
  • [RAG] 文档格式化 | 知识库摄入 | VectorDB.faiss | BM25索引.pkl
  • 松材线虫检测仪在林业的作用
  • 【Lua】题目小练1
  • 九学王资源apk应用名称整理
  • 【机器学习实战【七】】机器学习特征选定与评估
  • ELN:生物医药科研的数字化引擎——衍因科技引领高效创新
  • 多线程(一) --- 线程的基础知识
  • 使用位运算优化 Vue.js 应用:高效状态管理技巧
  • Oracle 19.28 RU 升级最佳实践指南
  • 装饰器模式及优化
  • 大模型Agent应用开发实战:从框架选型到行业落地
  • 十六进制与嵌入式系统及通信系统
  • yolo8+ASR+NLP+TTS(视觉语音助手)
  • 基于Rust Softplus 函数实践方法
  • 【通识】网络的基础知识
  • 学习日志预告
  • 【测试100问】为什么要做接口测试?
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GoodCheapFast(Good - Cheap - Fast三选二开关)
  • 区块链之Casper协议背景、演变发展、运作机制和潜在风险
  • 周志华《机器学习导论》第8章 集成学习 Ensemble Learning
  • 2025开源组件安全工具推荐OpenSCA
  • LVS(Linux virtual server)
  • AWS Lambda 最佳实践:构建高效无服务器应用的完整指南
  • 多维动态规划题解——最长公共子序列【LeetCode】记忆化搜索翻译成递推
  • CCS缺陷|冻干/灌装10大缺陷暴露无菌生产系统性漏洞:气流流型缺陷
  • 【android bluetooth 协议分析 03】【蓝牙扫描详解 3】【Bluetooth 中 EIR、IR、BLE 普通广播与扩展广播详解】
  • 数仓建设中,系统数据录入错误或者延迟,如何对历史数据修复或补入?
  • 安装物理机ubuntu系统
  • 初试Spring AI实现聊天功能