(libusb) usb口自动刷新

news2025/2/25 9:15:57

文章目录

  • libusb
  • 自动刷新程序Code
    • 目录结构
    • Code
      • 项目文件
      • `usb`包
      • `code`包
  • 效果
  • 描述
    • 重置reset
    • 热拔插
    • 使用
  • END

libusb

在操作USB相关内容时,有一个比较著名的库就是libusb

官方网址:libusb

在这里插入图片描述

下载

  • 下载源码
  • 官方编好的库
  • github:Releases · libusb/libusb (github.com)

在这里插入图片描述

在这里插入图片描述

使用:libusb: Application Programming Interface (sourceforge.io)

  • Functions
  • Structures
  • Enums

在这里插入图片描述

自动刷新程序Code

这里介绍一个基于libusb自动刷新usb的demo。

目录结构

  • 3rdparty
    • libusb的头文件和库
    • 这里采用官方编好的现成的库
  • usb
    • 基于C++对libusb的封装
    • 此包为纯C++代码
  • code
    • 基于Qt的ui和线程
E:.
└─usbReset
    │  main.cpp
    │  usbReset.pro
    │
    ├─3rdparty
    │  └─libusb
    │      ├─include
    │      │  └─libusb-1.0
    │      │          libusb.h
    │      │
    │      └─MinGW32
    │          ├─dll
    │          │      libusb-1.0.dll
    │          │      libusb-1.0.dll.a
    │          │
    │          └─static
    │                  libusb-1.0.a
    │
    ├─code
    │      THREAD_TimerUsb.cpp
    │      THREAD_TimerUsb.h
    │      WIDGET_Main.cpp
    │      WIDGET_Main.h
    │      WIDGET_Main.ui
    │
    └─usb
            usb.pri
            USB_Hotplug.cpp
            USB_Hotplug.h
            USB_Reset.cpp
            USB_Reset.h

Code

项目文件

usbReset.pro

QT += core
QT += widgets

#CONFIG += console
CONFIG += c++17

DESTDIR = $$PWD/bin

include($$PWD/usb/usb.pri)

INCLUDEPATH += $$PWD/code
HEADERS += \
    code/THREAD_TimerUsb.h \
    code/WIDGET_Main.h

SOURCES += \
    main.cpp \
    code/THREAD_TimerUsb.cpp \
    code/WIDGET_Main.cpp

FORMS += \
    code/WIDGET_Main.ui

usb/usb.pri

# 链接到libusb(采用静态库)
INCLUDEPATH += $$PWD/../3rdparty/libusb/include
HEADERS += $$PWD/../3rdparty/include/libusb/libusb-1.0/libusb.h
LIBS += -L$$PWD/../3rdparty/libusb/MinGW32/static/ -llibusb-1.0

# 当前封装的包
INCLUDEPATH += $$PWD/..
HEADERS += \
    $$PWD/USB_Hotplug.h \
    $$PWD/USB_Reset.h

SOURCES += \
    $$PWD/USB_Hotplug.cpp \
    $$PWD/USB_Reset.cpp

main.cpp

#include <QApplication>

#include "WIDGET_Main.h"

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MainWidget form;
    form.show();

    return app.exec();
}

usb

USB_Reset.h

#ifndef MYUSB_H_1682212693
#define MYUSB_H_1682212693

extern "C" {
#include "libusb-1.0/libusb.h"
}
#include <string>
#include <vector>

namespace USB {

class Reset final {
public:
    inline static const int EMPTY_POINTER_INT = 114514;

private:
    /// for init & exit
    static libusb_context* context;
    static uint32_t        obj_count;

public:
    static int Reset_context();

private:
    char str[1024]{};

public:
    /// libusb_init()
    Reset();
    /// libusb_exit()
    ~Reset();

public:
    ::std::vector<::std::string> Show_device();

public:
    libusb_device_descriptor Find_descriptByidVendor(uint32_t Vendor);

public:
    /**
     * vendor -> device* -> handle*
     * handle* -> reset
     * handle* -> close
     */
    libusb_device*        Find_deviceByidVendor(uint32_t Vendor);
    libusb_device_handle* Get_handleByOpenDevice(libusb_device* device);
    int  Reset_usbByHandle(libusb_device_handle* device_handle);
    void Close_deviceByHandle(libusb_device_handle* device_handle);
};

}  // namespace USB
#endif  // MYUSB_H_1682212693

USB_Reset.cpp

#include "USB_Reset.h"

namespace USB {
/**
 * @brief UsbBase::context
 * static data
 * context: 所有对象共享这一个全局的上下文对象
 * obj_count: 引用计数
 */
libusb_context* Reset::context   = nullptr;
uint32_t        Reset::obj_count = 0;

/**
 * @brief UsbBase::Reset_context
 * @return
 * static 重置上下文
 */
int Reset::Reset_context() {
    /// 有实体对象才重置 context
    if (0 != obj_count) {
        if (nullptr != context) {
            libusb_exit(context);
            context = nullptr;
        }
        return libusb_init(&context);
    }
    return LIBUSB_SUCCESS;
}

/**
 * @brief UsbBase::UsbBase
 * constructor
 * 上下文的init
 * 维护引用计数
 */
Reset::Reset() {
    /// 查看版本号
    const struct libusb_version* version = libusb_get_version();
    printf(">>>Libusb-Version:%s\n", version->describe);
    printf(">>>Libusb-Version:%d.%d.%d.%d\n",
           version->major,
           version->minor,
           version->micro,
           version->nano
           );

    /// 第一个对象,或者之前没有注册成功
    if (0 == obj_count || nullptr == context) {
        if (int res = libusb_init(&context); res != 0) {
            sprintf(str, "fail to init: %d\n", res);
            /// TODO
            /// 根据实际情况,日志、断言、异常等
            /// TODO
        }
    }

    obj_count += 1;
}

/**
 * @brief UsbBase::~UsbBase
 * distructor
 * 维护引用计数
 * 上下文的退出
 */
Reset::~Reset() {
    obj_count += -1;
    if (0 == obj_count) {
        if (nullptr != context) {
            libusb_exit(context);
            context = nullptr;
        }
    }
}

/**
 * @brief UsbBase::show_device
 * just show device-list message
 */
::std::vector<::std::string> Reset::Show_device() {
    ::std::vector<::std::string> messageList;
    messageList.push_back(
        "********************** show device-list message BEGIN "
        "**********************");

    /// help data
    libusb_device** deviceList = nullptr;
    libusb_get_device_list(nullptr, &deviceList);
    for (int i = 0; deviceList[i] != nullptr; i += 1) {
        auto                            device = deviceList[i];
        struct libusb_device_descriptor descript;
        int ret = libusb_get_device_descriptor(device, &descript);
        if (LIBUSB_SUCCESS != ret) {
            continue;
        }

        sprintf(str,
                "*"
                "idVendor:%6d "
                "idProduct:%6d "
                "bDeviceClass:%6d "
                "(bus:%3d, device:%3d)"
                "*",
                descript.idVendor, descript.idProduct, descript.bDeviceClass,
                libusb_get_bus_number(device),
                libusb_get_device_address(device));
        messageList.push_back(str);
    }
    messageList.push_back(
        "********************** show device-list message END "
        "************************");
    return messageList;
}

/**
 * @brief MyUsb::Find_descriptByidVendor
 * @param idVendor
 * @return
 * 获取设备描述对象
 */
libusb_device_descriptor Reset::Find_descriptByidVendor(uint32_t idVendor) {
    /// ret
    libusb_device_descriptor descriptor;

    /// help data
    libusb_device_handle* deviceHandle = nullptr;
    libusb_device**       deviceList   = nullptr;

    libusb_get_device_list(nullptr, &deviceList);
    for (int i = 0; deviceList[i] != nullptr; i += 1) {
        auto device = deviceList[i];
        int  ret    = libusb_get_device_descriptor(device, &descriptor);
        if (LIBUSB_SUCCESS != ret) {
            continue;
        }

        if (descriptor.idVendor == idVendor) {
            const int isOpen = libusb_open(device, &deviceHandle);
            if (LIBUSB_SUCCESS == isOpen) {
                libusb_close(deviceHandle);
                break;
            }
        }
    }

    return descriptor;
}

/**
 * @brief UsbBase::Find_deviceByidVendor
 * @param Vendor
 * @return
 * 获取device
 */
libusb_device* Reset::Find_deviceByidVendor(uint32_t Vendor) {
    /// ret
    libusb_device* device = nullptr;

    /// help data
    libusb_device_handle* deviceHandle = nullptr;
    libusb_device**       deviceList   = nullptr;

    libusb_get_device_list(nullptr, &deviceList);
    for (int i = 0; deviceList[i] != nullptr; i += 1) {
        device = deviceList[i];
        libusb_device_descriptor descriptor;
        int ret = libusb_get_device_descriptor(device, &descriptor);
        if (LIBUSB_SUCCESS != ret) {
            continue;
        }

        if (descriptor.idVendor == Vendor) {
            const int isOpen = libusb_open(device, &deviceHandle);
            if (LIBUSB_SUCCESS == isOpen) {
                libusb_close(deviceHandle);
                break;
            } else {
                device = nullptr;
            }
        } else {
            device = nullptr;
        }
    }

    return device;
}

/**
 * @brief UsbBase::Get_handleByOpenDevice
 * @param device
 * @return
 * 根据传入的设备指针
 * 通过open操作
 * 获取句柄指针
 */
libusb_device_handle* Reset::Get_handleByOpenDevice(libusb_device* device) {
    if (nullptr == device) {
        return nullptr;
    }
    libusb_device_handle* deviceHandle = nullptr;
    const int             isOpen       = libusb_open(device, &deviceHandle);
    if (LIBUSB_SUCCESS == isOpen) {
        return deviceHandle;
    } else {
        return nullptr;
    }
}

/**
 * @brief UsbBase::Reset_usbByHandle
 * @param device_handle
 * @return
 * 通过句柄重置设备
 * 为0则表示成功
 */
int Reset::Reset_usbByHandle(libusb_device_handle* device_handle) {
    if (nullptr == device_handle) {
        return EMPTY_POINTER_INT;
    }
    const int isReset = libusb_reset_device(device_handle);
    //! TODO if (isReset ?= 0) => log
    return isReset;
}

/**
 * @brief UsbBase::Close_deviceByHandle
 * @param device_handle
 * 手动通过句柄指针关闭设备
 */
void Reset::Close_deviceByHandle(libusb_device_handle* device_handle) {
    if (nullptr == device_handle) {
        return;
    }
    libusb_close(device_handle);
}
}  // namespace USB

USB_Hotplug.h

#ifndef HOTPLUG_H_27452998650
#define HOTPLUG_H_27452998650

extern "C" {
#include "libusb-1.0/libusb.h"
}

namespace USB {
class Hotplug final {
private:
    static libusb_device_handle *m_deviceHandle;

private:
    static int LIBUSB_CALL usb_arrived_callback(struct libusb_context *ctx,
                                                struct libusb_device  *dev,
                                                libusb_hotplug_event   event,
                                                void *userdata);
    static int LIBUSB_CALL usb_left_callback(struct libusb_context *ctx,
                                             struct libusb_device  *dev,
                                             libusb_hotplug_event   event,
                                             void                  *userdata);

private:
    char str[1024]{};

private:
    libusb_hotplug_callback_handle usb_arrived_handle;
    libusb_hotplug_callback_handle usb_left_handle;
    libusb_context                *context = nullptr;

private:
    uint32_t m_regiest_vendor      = LIBUSB_HOTPLUG_MATCH_ANY;
    uint32_t m_regiest_product     = LIBUSB_HOTPLUG_MATCH_ANY;
    uint32_t m_regiest_deviceClass = LIBUSB_HOTPLUG_MATCH_ANY;

private:
    Hotplug();

public:
    Hotplug(uint32_t vendor, uint32_t product, uint32_t deviceClass);
    ~Hotplug();

public:
    int Register_arrived();
    int Register_left();
};
}  // namespace USB

#endif  // HOTPLUG_H_27452998650

USB_Hotplug.cpp

#include "USB_Hotplug.h"

#include <iostream>

namespace USB {
/**
 * @brief Hotplug::m_deviceHandle
 * 主要用于静态的回调函数
 */
libusb_device_handle *Hotplug::m_deviceHandle = nullptr;

/**
 * @brief Hotplug::usb_arrived_callback
 * @param ctx
 * @param dev
 * @param event
 * @param userdata
 * @return
 * 热拔插 arrive 的回调
 */
int Hotplug::usb_arrived_callback(libusb_context *ctx, libusb_device *dev,
                                  libusb_hotplug_event event, void *userdata) {
    struct libusb_device_handle    *handle;
    struct libusb_device_descriptor desc;
    unsigned char                   buf[512];
    int                             rc;

    libusb_get_device_descriptor(dev, &desc);
    printf("Add usb device: \n");
    printf("\tCLASS(0x%x) SUBCLASS(0x%x) PROTOCOL(0x%x)\n", desc.bDeviceClass,
           desc.bDeviceSubClass, desc.bDeviceProtocol);
    printf("\tVENDOR(0x%x) PRODUCT(0x%x)\n", desc.idVendor, desc.idProduct);
    rc = libusb_open(dev, &handle);
    if (LIBUSB_SUCCESS != rc) {
        printf("Could not open USB device\n");
        return 0;
    }

    memset(buf, 0, sizeof(buf));
    rc = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, buf,
                                            sizeof(buf));
    if (rc < 0) {
        printf("Get Manufacturer failed\n");
    } else {
        printf("\tManufacturer: %s\n", buf);
    }

    memset(buf, 0, sizeof(buf));
    rc = libusb_get_string_descriptor_ascii(handle, desc.iProduct, buf,
                                            sizeof(buf));
    if (rc < 0) {
        printf("Get Product failed\n");
    } else {
        printf("\tProduct: %s\n", buf);
    }

    memset(buf, 0, sizeof(buf));
    rc = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buf,
                                            sizeof(buf));
    if (rc < 0) {
        printf("Get SerialNumber failed\n");
    } else {
        printf("\tSerialNumber: %s\n", buf);
    }
    libusb_close(handle);

    return 0;
}

/**
 * @brief Hotplug::usb_left_callback
 * @param ctx
 * @param dev
 * @param event
 * @param userdata
 * @return
 * 热拔插left的回调
 */
int Hotplug::usb_left_callback(libusb_context *ctx, libusb_device *dev,
                               libusb_hotplug_event event, void *userdata) {
    struct libusb_device_descriptor desc;
    libusb_get_device_descriptor(dev, &desc);
    const int isReset = libusb_reset_device(m_deviceHandle);
    return isReset;
}

/**
 * @brief Hotplug::Hotplug
 * 构造的时候init
 */
Hotplug::Hotplug() {
    libusb_init(&context);
}

/**
 * @brief Hotplug::Hotplug
 * @param vendor
 * @param product
 * 委托构造
 */
Hotplug::Hotplug(uint32_t vendor, uint32_t product, uint32_t deviceClass) : Hotplug() {
    /// data
    {
        m_regiest_vendor      = vendor;
        m_regiest_product     = product;
        m_regiest_deviceClass = deviceClass;
    }

    /// find
    {
        libusb_device **m_deviceList;
        libusb_get_device_list(nullptr, &m_deviceList);
        for (int i = 0; m_deviceList[i] != nullptr; i += 1) {
            auto                            device = m_deviceList[i];
            struct libusb_device_descriptor descript;
            int ret = libusb_get_device_descriptor(device, &descript);
            if (ret < 0) {
                sprintf(
                    str,
                    "Error libusb_get_device_descriptor idx = %d res = %d\n", i,
                    ret);
            }

            if (descript.idVendor == vendor && descript.idProduct == product) {
                const int isOpen =
                    libusb_open(m_deviceList[i], &m_deviceHandle);
                if (LIBUSB_SUCCESS == isOpen) {
                    break;
                }
            }
        }
    }  /// find

    /// test
    {
        Register_arrived();
        Register_left();
    }
}

/**
 * @brief Hotplug::~Hotplug
 * 析构的时候注销和释放句柄
 */
Hotplug::~Hotplug() {
    libusb_hotplug_deregister_callback(context, usb_arrived_handle);
    libusb_hotplug_deregister_callback(context, usb_left_handle);
    libusb_exit(context);
}

/**
 * @brief Hotplug::Register_arrived
 * @return
 * 注册热拔插arrive
 */
int Hotplug::Register_arrived() {
    int res = libusb_hotplug_register_callback(
        context, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_NO_FLAGS,
        m_regiest_vendor, m_regiest_product, m_regiest_deviceClass,
        Hotplug::usb_arrived_callback, NULL, &usb_arrived_handle);
    return res;
}

/**
 * @brief Hotplug::Register_left
 * @return
 * 注册热拔插left
 */
int Hotplug::Register_left() {
    int res = libusb_hotplug_register_callback(
        context, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_NO_FLAGS,
        m_regiest_vendor, m_regiest_product, m_regiest_deviceClass,
        Hotplug::usb_left_callback, NULL, &usb_left_handle);
    return res;
}
}  // namespace USB

code

WIDGET_Main.h

#ifndef FORM_H_27453073957
#define FORM_H_27453073957

#include <QScopedPointer>
#include <QVector>
#include <QWidget>

#include "THREAD_TimerUsb.h"

namespace Ui {
class MainWidget;
}

class MainWidget : public QWidget {
    Q_OBJECT

private:
    QScopedPointer<Ui::MainWidget> ui;

private:
    TimerUsb m_usb;

public:
    explicit MainWidget(QWidget *parent = nullptr);
    ~MainWidget();

private:
    void show_usbDeviceMsgAll();
    void start_work();
};

#endif  // FORM_H_27453073957

WIDGET_Main.cpp

#include "WIDGET_Main.h"

#include <QDebug>
#include <QMutex>
#include <QThread>
#include <QTimer>

#include "ui_WIDGET_Main.h"
#include "usb/USB_Reset.h"

QWidget         *g_widgetDisplayBoard = nullptr;
QtMessageHandler g_oldMessageHandler  = nullptr;
QMutex           g_dispalyMutex;

/**
 * debug 重定向
 */
void newDebugHandlerFunc(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
    QMutexLocker locker(&g_dispalyMutex);
    if (g_widgetDisplayBoard) {
        dynamic_cast<QTextEdit *>(g_widgetDisplayBoard)->append(msg);
    } else {
        g_oldMessageHandler(type, context, msg);
    }
}

/**
 * @brief MainWidget::MainWidget
 * @param parent
 * 1. debug重定向
 * 2. connect
 */
MainWidget::MainWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MainWidget{}) {
    ui->setupUi(this);
    setWindowTitle("usb controller");

    /// debug -> widget
    {
        g_widgetDisplayBoard       = ui->textEdit;
        g_oldMessageHandler = qInstallMessageHandler(newDebugHandlerFunc);
    }

    /// connect
    {
        connect(ui->btn_showAll, &QPushButton::clicked, this,
                &MainWidget::show_usbDeviceMsgAll);
        connect(ui->btn_clear, &QPushButton::clicked, ui->textEdit,
                &QTextEdit::clear);
        connect(ui->btn_start, &QPushButton::clicked, this,
                &MainWidget::start_work);

        connect(&m_usb, &TimerUsb::signal_message, this,
                [](const QString &msg) { qInfo() << msg; });
    }

    /// before exe
    { show_usbDeviceMsgAll(); }
}

/**
 * @brief MainWidget::~MainWidget
 * 将线程安全关闭
 */
MainWidget::~MainWidget() {
    if (m_usb.isRunning()) {
        m_usb.quit();
        m_usb.wait();
    }
}

/**
 * @brief MainWidget::show_usbDeviceMsgAll
 * 展示所有usb设备的信息
 * 因为设计的是通用库
 * 因此返回的是std::vector<std::string>
 */
void MainWidget::show_usbDeviceMsgAll() {
    for (auto &&s : USB::Reset().Show_device()) {
        qDebug() << s.c_str();
    }
}

/**
 * @brief MainWidget::start_work
 * 检测线程启动
 */
void MainWidget::start_work() {
    uint   vendor = ui->edit_Vendor->text().toUInt();
    double time   = ui->edit_timeout->text().toDouble();
    m_usb.Set_usbDeviceVendor(vendor);
    m_usb.Set_timerInterval(time * 1000);
    m_usb.Set_timerStart();

    if (false == m_usb.isRunning()) {
        qDebug() << "=== start before ===";
        m_usb.start();
        qDebug() << "=== start after ===";
    }
}

THREAD_TimerUsb.h

#ifndef THREADUSB_H_70403004403
#define THREADUSB_H_70403004403

#include <QThread>
#include <QTimer>

class TimerUsb : public QThread {
    Q_OBJECT
private:
    uint32_t m_usbDeviceVendor = 0;

private:
    QTimer m_timer;
    int    m_timerIntervalMsec = 1500;

public:
    TimerUsb();

public:
    void Set_usbDeviceVendor(uint32_t vendor);

public:
    void Set_timerInterval(int msec = 1500);
    void Set_timerStart();
    void Set_timerStop();
    bool Is_timerRunning();

protected:
    void run() override;

signals:
    void signal_message(const QString&);
};

#endif  // THREADUSB_H_70403004403

THREAD_TimerUsb.cpp

#include "THREAD_TimerUsb.h"

#include <QDebug>
#include <QTimer>

#include "usb/USB_Reset.h"

#if 0
#define DEBUG_MODEL qInfo() <<
#else
#define DEBUG_MODEL emit this->signal_message
#endif

/**
 * @brief ThreadUsb::ThreadUsb
 * construct
 */
TimerUsb::TimerUsb() {
    Set_timerInterval();
}

/**
 * @brief ThreadUsb::Set_usbDeviceVendor
 * @param vendor
 * 根据 vendor 查询设备
 */
void TimerUsb::Set_usbDeviceVendor(uint32_t vendor) {
    this->m_usbDeviceVendor = vendor;
}

/**
 * @brief ThreadUsb::Set_timerInterval
 * @param msec
 * 设置reset间隔
 */
void TimerUsb::Set_timerInterval(int msec) {
    this->m_timerIntervalMsec = msec;
    m_timer.setInterval(m_timerIntervalMsec);
}

/**
 * @brief TimerUsb::Set_timerStart
 * 启动定时器,但不启动线程
 */
void TimerUsb::Set_timerStart() {
    this->m_timer.start();
}

/**
 * @brief TimerUsb::Set_timerStop
 * 关闭定时器,但不关闭线程
 */
void TimerUsb::Set_timerStop() {
    this->m_timer.stop();
}

/**
 * @brief TimerUsb::Is_timerRunning
 * @return
 * 定时器是否运行
 */
bool TimerUsb::Is_timerRunning() {
    return this->m_timer.isActive();
}

/**
 * @brief ThreadUsb::run
 * 定时器timeout一次,usb-reset一次
 */
void TimerUsb::run() {
    USB::Reset     usb;
    libusb_device* device = nullptr;

    /// 为了防止刚启动的时候没有获得
    /// 高强度轮询获取
    const size_t loopDeviceCount   = 1e5 + 10;
    const size_t loopContextPeriod = 1e3;
    for (size_t i = 0; i < loopDeviceCount; i += 1) {
        device = usb.Find_deviceByidVendor(this->m_usbDeviceVendor);
        if (nullptr != device) {
            break;
        } else {
            if (i % loopContextPeriod == 0) {
                DEBUG_MODEL("device is null & context resert");
                USB::Reset::Reset_context();
            }
        }
    }
    if (nullptr == device) {
        DEBUG_MODEL("libusb_device is null & Thread end!");
        return;
    } else {
        DEBUG_MODEL("libusb_device require ok!");
    }

    libusb_device_handle* handle = usb.Get_handleByOpenDevice(device);
    if (handle == nullptr) {
        DEBUG_MODEL("libusb_device require is null & Thread end!");
        return ;
    } else {
        DEBUG_MODEL("libusb_device_handle require ok!");
    }

    auto con = connect(&this->m_timer, &QTimer::timeout, [&]() {
        int res = usb.Reset_usbByHandle(handle);
        if (LIBUSB_SUCCESS == res) {
            DEBUG_MODEL("reset Success");
        } else {
            DEBUG_MODEL("reset Error; errorType = " + QString::number(res));
        }
        /// 句柄不归还,持续重置
        // usb.Close_deviceByHandle(handle);
    });


    /// 开启事件循环
    exec();
    this->m_timer.stop();
    disconnect(con);
}

效果

在这里插入图片描述

界面功能比较简单,基本就是widget中的代码,设置好vendor和倒计时后点击开始即可。

目前身边没有可以测试的usb设备,因此不展示具体效果。

其中USB::Reset是经过测试可用的。

描述

本demo主要就是libusb的封装,然后是对于封装的简单调用。

重置reset

基本思路:vendor->device*->handle*

然后使用handle*进行reset和最后的close

  • 获取device*
    • 获取设备序列libusb_get_device_list()
    • 遍历序列,获取每个设备的描述信息libusb_get_device_descriptor()
    • 对比描述信息,确认是哪个device*。并测试是否能正常open。
  • 获取handle*
    • 通过libusb_open()即测试打开的同时就能获取
  • 使用handle*进行reset
    • 使用libusb_reset_device()
  • 关闭handle*
    • 使用libusb_close()

注意:有的vendor是一样的编号,请根据实际的情景和需求改变具体的查找规则。

热拔插

热拔插部分没有测试,不做重点描述。

但是基本原理就是注册拔&插的回调函数。

libusb_hotplug_register_callback()

  • 标记:LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED
  • 标记:LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT

使用

全在QThread::run()函数中。

在实际作用时,可能因为物理设备实际问题,导致设备指针和句柄的获取失败。

因此可以设置一个比较大的循环,无脑获取多次,直到获取成功,但若多次获取失败,则直接视为失败了。

然后启动一个定时器,

注意请不要close句柄。因为设备的实际请款,可能关闭后就再获取不到了,只要不随便乱插,设备标号和句柄是不会变的,因此直接保留好。直到真正不需要时再关闭(根据实际业务和逻辑需求)。




END

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1477693.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Mysql REGEXP正则运算符

# 邮箱h开头 mysql> select email form xxx where email REGEXP ^h;

就业班 2401--2.28 Linux Day7--存储管理1

一 .存储管理 主要知识点: 基本分区、逻辑卷LVM、EXT3/4/XFS文件系统、RAID 初识硬盘 机械 HDD 固态 SSD SSD的优势 SSD采用电子存储介质进行数据存储和读取的一种技术&#xff0c;拥有极高的存储性能&#xff0c;被认为是存储技术发展的未来新星。 与传统硬盘相比&#…

单点登录的三种方式

前言 在B/S系统中&#xff0c;登录功能通常都是基于Cookie 来实现的。当用户登录成功后&#xff0c;一般会将登录状态记录到Session中&#xff0c;或者是给用户签发一个 Token&#xff0c;无论哪一种方式&#xff0c;都需要在客户端保存一些信息(Session ID或Token)&#xff0…

服务器数据恢复-异常断电导致服务器硬盘离线的数据恢复案例

服务器数据恢复环境&#xff1a; dell某型号服务器中有一组通过raid卡组建的raid10&#xff0c;该raid阵列中一共有4块磁盘。上层部署XenServer虚拟化平台&#xff0c;作为网站服务器使用。 服务器故障&#xff1a; 服务器异常断电导致服务器上的一台虚拟机不可用。需要恢复这…

优维全面可观测产品能力分解④:故障可观测

《优维全面可观测产品能力分解》系列文章的第一篇&#xff0c;介绍了「架构可观测」是从系统架构的视角来呈现链路与服务的状态数据&#xff1b;第二篇介绍了「变更可观测」是从变更的角度看系统状态的变化&#xff0c;及与事件的关联关系&#xff1b;第三篇介绍了「应用服务可…

配置资源管理Secret

目录 一、什么是Secret? 二、secret的三种类型 三、pod适用secret的三种方式 四、secret实例 1、创建secret 2、使用Secret方式 一、什么是Secret? Secret 是用来保存密码、token、密钥等敏感数据的 k8s 资源&#xff0c;目的是为了更方便的控制使用数据&#xff0c;并…

MWC 2024丨移远通信全新Wi-Fi 7和蓝牙5.4模组组合,为PC提供巅峰无线连接体验

2月26日&#xff0c;在MWC 2024展会期间&#xff0c;全球领先的物联网整体解决方案供应商移远通信对外宣布&#xff0c;其已正式推出新型Wi-Fi 7和蓝牙5.4二合一模组NCM8x5系列。该系列产品专为个人电脑&#xff08;PC&#xff09;无线连接场景设计&#xff0c;具有低延时、超高…

python 中常用的热门库介绍

阅读本文之前请参阅-----如何系统的自学python Python 是一种非常流行的编程语言&#xff0c;它的一个主要优势是拥有一个庞大的生态系统&#xff0c;其中包括许多强大的库。这些库为各种任务提供了解决方案&#xff0c;从数据分析到机器学习&#xff0c;从网络爬虫到图像处理。…

【ArcPy】验证是否存在

实例展示 代码 import arcpy def script_tool(param0, param1,param2):if arcpy.Exists(param0):arcpy.AddMessage("图层存在");if arcpy.Exists(param0):arcpy.AddMessage("工作空间存在");if arcpy.Exists(param0):arcpy.AddMessage("要素存在&quo…

蓝桥杯-灌溉

参考了大佬的解题思路&#xff0c;先遍历一次花园&#xff0c;找到所有的水源坐标&#xff0c;把它们存入 “水源坐标清单” 数组内&#xff0c;再读取数组里的水源坐标进行扩散。 #include <iostream> using namespace std; int main() {int n,m,t,r,c,k,ans0,list_i0;…

编写科技项目验收测试报告需要注意什么?第三方验收测试多少钱?

科技项目验收测试是一个非常重要的环节&#xff0c;它对于确保科技项目的质量和可用性起着至关重要的作用。在项目完成后&#xff0c;进行科技项目验收测试可以评估项目的功能、性能和可靠性等方面&#xff0c;并生成科技项目验收测试报告&#xff0c;以提供给项目的相关方参考…

C语言数据结构基础-单链表

1.链表概念 在前面的学习中&#xff0c;我们知道了线性表&#xff0c;其中逻辑结构与物理结构都连续的叫顺序表&#xff0c;那么&#xff1a; 链表是⼀种物理存储结构上⾮连续、⾮顺序的存储结构&#xff0c;数据元素的逻辑顺序是通过链表 中的指针链接次序实现的 。 2.链表组…

力扣550 游戏玩法分析 IV

目录 题目描述 思路整理 1. 首次登录日期 2. 第二天登录 3. 计算比率 实现思路 完整代码及解释 题目描述 Table: Activity ----------------------- | Column Name | Type | ----------------------- | player_id | int | | device_id | int | | ev…

ICVQUANTUMCHINA报告:《2024全球量子计算产业发展展望》

2月20日&#xff0c;《2024量子计算产业发展展望》的中文版报告通过光子盒官方平台发布&#xff0c;英文版报告通过ICV官方平台发布。 英文版报告获取地址&#xff1a; https://www.icvtank.com/newsinfo/897610.html 在过去的一年里&#xff0c;光子盒与您一同见证了全球量子…

改进的yolo交通标志tt100k数据集目标检测(代码+原理+毕设可用)

YOLO TT100K: 基于YOLO训练的交通标志检测模型 在原始代码基础上&#xff1a; 修改数据加载类&#xff0c;支持CoCo格式&#xff08;使用cocoapi&#xff09;&#xff1b;修改数据增强&#xff1b;validation增加mAP计算&#xff1b;修改anchor&#xff1b; 注: 实验开启weig…

面试数据库篇(mysql)- 03MYSQL支持的存储引擎有哪些, 有什么区别

存储引擎就是存储数据、建立索引、更新/查询数据等技术的实现方式 。存储引擎是基于表的&#xff0c;而不是基于库的&#xff0c;所以存储引擎也可被称为表类型。 MySQL体系结构 连接层服务层引擎层存储层 存储引擎特点 InnoDB MYSQL支持的存储引擎有哪些, 有什么区别 ? my…

C++初阶:模版相关知识的进阶内容(非类型模板参数、类模板的特化、模板的分离编译)

结束了常用容器的介绍&#xff0c;今天继续模版内容的讲解&#xff1a; 文章目录 1.非类型模版参数2.模板的特化2.1模版特化引入和概念2.2函数模版特化2.3类模板特化2.3.1全特化2.3.1偏特化 3. 模板分离编译3.1分离编译概念3.2**模板的分离编译**分析原因 1.非类型模版参数 模板…

vulnhub-----Hackademic靶机

文章目录 1.C段扫描2.端口扫描3.服务扫描4.web分析5.sql注入6.目录扫描7.写马php反弹shell木马 8.反弹shell9.内核提权 1.C段扫描 kali:192.168.9.27 靶机&#xff1a;192.168.9.25 ┌──(root㉿kali)-[~] └─# arp-scan -l Interface: eth0,…

tinymce上传图片或者其他文件等等

技术选型 tips: tinymce在vue中常用的有两种方式 第一种: 官方组件,点我 优点: 不用自己封装组件 缺点: 需要申请特定apikey,类似于百度,高德地图; 第二种: 就是下面这种 优点: 不需要申请特定的apikey 缺点: 需要自己手动的封装组件,灵活性高 Vue 2.x和3.x基本没有区别 tinym…

《Redis 设计与实现》读书概要

注&#xff1a; 《Redis 设计与实现》一书基于 Redis 2.9 版本编写&#xff0c;部分内容已过时&#xff0c;过时之处本文会有所说明。本文为读书笔记&#xff0c;部分简单和日常使用较少的知识点未记录。原书网页版地址 https://redisbook.com/ 一、底层数据结构 SDS(Simple Dy…