qt中libusb热插拔检测示例代码

qt中libusb热插拔检测示例代码

cpp 复制代码
#include <QCoreApplication>
#include <QDebug>
#include <libusb/libusb.h>

// 回调函数
int hotplugCallback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data) {
    libusb_device_descriptor desc;
    if (libusb_get_device_descriptor(dev, &desc) != LIBUSB_SUCCESS) {
        qDebug() << "无法获取设备描述符";
        return 0;
    }

    uint8_t busNumber = libusb_get_bus_number(dev);
    uint8_t deviceAddress = libusb_get_device_address(dev);

    if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) {
        qDebug() << "【设备插入】"
                 << "VID: " << QString::number(desc.idVendor, 16)
                 << ", PID: " << QString::number(desc.idProduct, 16)
                 << ", 总线号: " << busNumber
                 << ", 设备地址: " << deviceAddress;

        // 获取接口信息
        libusb_config_descriptor *config;
        if (libusb_get_active_config_descriptor(dev, &config) == LIBUSB_SUCCESS) {
            for (int i = 0; i < config->bNumInterfaces; i++) {
                const libusb_interface &interface = config->interface[i];
                for (int j = 0; j < interface.num_altsetting; j++) {
                    const libusb_interface_descriptor &altsetting = interface.altsetting[j];
                    qDebug() << "接口号: " << altsetting.bInterfaceNumber
                             << ", 端点数: " << altsetting.bNumEndpoints
                             << ", 类别: " << altsetting.bInterfaceClass;
                }
            }
            libusb_free_config_descriptor(config);
        }
    } else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) {
        qDebug() << "【设备拔出】"
                 << "VID: " << QString::number(desc.idVendor, 16)
                 << ", PID: " << QString::number(desc.idProduct, 16)
                 << ", 总线号: " << busNumber
                 << ", 设备地址: " << deviceAddress;
    }

    return 0;
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    // 初始化 libusb
    libusb_context *ctx = nullptr;
    if (libusb_init(&ctx) < 0) {
        qDebug() << "libusb 初始化失败";
        return 1;
    }
    qDebug() << "libusb 初始化成功";

    // 检查是否支持热插拔
    if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
        qDebug() << "当前系统不支持 USB 热插拔检测";
        libusb_exit(ctx);
        return 1;
    }

    // 注册热插拔回调
    libusb_hotplug_callback_handle callbackHandle;
    int result = libusb_hotplug_register_callback(ctx,
                                                   LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
                                                   LIBUSB_HOTPLUG_ENUMERATE,
                                                   LIBUSB_HOTPLUG_MATCH_ANY,
                                                   LIBUSB_HOTPLUG_MATCH_ANY,
                                                   LIBUSB_HOTPLUG_MATCH_ANY,
                                                   hotplugCallback,
                                                   nullptr,
                                                   &callbackHandle);

    if (result != LIBUSB_SUCCESS) {
        qDebug() << "注册回调失败: " << libusb_error_name(result);
        libusb_exit(ctx);
        return 1;
    }

    qDebug() << "USB 热插拔检测已启动...";

    // 循环处理事件
    while (true) {
        libusb_handle_events(ctx);
    }

    // 退出前释放资源
    libusb_hotplug_deregister_callback(ctx, callbackHandle);
    libusb_exit(ctx);
    return 0;
}
相关推荐
Quz2 小时前
QML 拖拽篇:接收拖拽与数据交换
qt
欧特克_Glodon4 小时前
OpenCV计算机视觉开发入门与实践<二十七>:图像分割概述
c++·人工智能·opencv·计算机视觉
蒸蒸yyyyzwd4 小时前
cpp 选手秋招学习笔记 day21
c++·面试·八股
Interview Aid1124 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
CoderIsArt11 小时前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
AI情绪识别开源14 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
「QT(C++)开发工程师」15 小时前
C++ auto 用法详解
开发语言·c++
OPEN-F15 小时前
C++STL教程:容器适配器与实用工具
开发语言·c++
yaoxin52112315 小时前
507. Java 反射 - 在 BeanFactory 中实现依赖注入
java·开发语言