QT做一个USB HID设备识别软件

1.下载 HidApi库:GitHub - yigityuce/HidApi: Human Interface Device Api (HidApi) with C++

2.pro文件添加

复制代码
DEFINES -= UNICODE
LIBS += -lsetupapi

3.h文件

复制代码
#ifndef My_Usb_Hid_Device_H
#define My_Usb_Hid_Device_H

#include <QWidget>
#include <QString>
#include <QDir>
#include <QFileDialog>
#include <QDebug>
#include "Redefine/MyTextEdit.h"
#include "Redefine/MyLabel.h"
#include "Redefine/MyToolButton.h"
#include "UsbHidApi/HidApi.h"
#include "Redefine/MyComboBox.h"

class My_Usb_Hid_Device: public QWidget
{
public:
    My_Usb_Hid_Device();
    void setParent(QWidget *parent);
    void resize(int w, int h);

    void Open_File_Button_Clicked();
    void Open_Device_Button_Clicked();
    void Clear_Button_Clicked();
    void USB_HID_Listen_Clicked();

    QString FilePath;
    QString FileString;
    int Get_File_Size;
    bool USB_HID_DEVICE_IsOpen = false;

    HidApi hidApi;
    HidDevice hidDev;//HID设备
    HidDeviceList devList;//HID设备列表
    HidDeviceList Last_devList;//HID设备列表

    MyLabel Update_File_Path_Lable;
    MyTextEdit File_Path_Edit;
    MyToolButton Open_File_Button;

    MyComboBox Usb_PidVid_Box;
    MyLabel Pid_Lable;
    MyLabel Vid_Lable;
    QTextEdit Pid_Edit;
    QTextEdit Vid_Edit;
    MyComboBox Usb_Type_Box;
    MyToolButton Open_Device_Button;
    MyToolButton Clear_Button;

    QTextEdit Out_Edit;
};

#endif // My_Usb_Hid_Device_H

4.CPP文件

复制代码
#include "My_Usb_Hid_Device.h"

My_Usb_Hid_Device::My_Usb_Hid_Device()
{
    FilePath = QDir::homePath();
    FilePath += "/Desktop/";
    File_Path_Edit.setText(FilePath);

    hidApi.isInitialized();
    Last_devList = devList = hidApi.scanDevices();
    for(size_t i = 0; i < devList.size(); i++)//可打印扫描到的设备的信息
    {
        qDebug() << i + 1 << ". DEVICE\n";
        qDebug() << "Path        : " << devList[i].getPath().c_str();
        qDebug() << "Vendor Id   : " << devList[i].getVendorId();
        qDebug() << "Product Id  : " << devList[i].getProductId();
        //qDebug() << "Serial      : " << devList[i].getSerial();
        //qDebug() << "Manufacturer: " << devList[i].getManufacturer();
        //qDebug() << "Product     : " << devList[i].getProductString();
        qDebug() << "Release No  : " << devList[i].getRelease();
        qDebug() << "Usage Page  : " << devList[i].getUsagePage();
        qDebug() << "Usage       : " << devList[i].getUsage();
        qDebug() << "Interface   : " << devList[i].getInterface();
        qDebug() << "-----------------------------------------------";

        Out_Edit.append(QString("%1").arg(i) + ". DEVICE");
        Out_Edit.append(devList[i].getPath().c_str());
        Out_Edit.append("Serial      : " + QString::fromStdWString(devList[i].getSerial()));
        Out_Edit.append("Manufacturer: " + QString::fromStdWString(devList[i].getManufacturer()));
        Out_Edit.append("Product     : " + QString::fromStdWString(devList[i].getProductString()));
        Out_Edit.append("Vendor Id   : " + QString("0X%1").arg(devList[i].getVendorId(), 4, 16, QLatin1Char('0')));
        Out_Edit.append("Product Id  : " + QString("0X%1").arg(devList[i].getProductId(),4, 16, QLatin1Char('0')));
        Out_Edit.append("Release No  : " + QString("%1").arg(devList[i].getRelease(),    4, 10, QLatin1Char('0')));
        Out_Edit.append("Usage Page  : " + QString("%1").arg(devList[i].getUsagePage(),  4, 10, QLatin1Char('0')));
        Out_Edit.append("Usage       : " + QString("%1").arg(devList[i].getUsage(),      4, 10, QLatin1Char('0')));
        Out_Edit.append("Interface   : " + QString("%1").arg(devList[i].getInterface(),  4, 10, QLatin1Char('0')));
        Out_Edit.append(0);

        //Usb_PidVid_Box.addItem(QString("%1").arg(i));
        Usb_PidVid_Box.addItem(QString::fromStdWString(devList[i].getManufacturer()) + ":VID:" + QString("%1").arg(devList[i].getVendorId()) + "PID:" + QString("%1").arg(devList[i].getProductId()));
    }
}

void My_Usb_Hid_Device::setParent(QWidget *parent)
{
    Update_File_Path_Lable.setParent(parent);
    Update_File_Path_Lable.setText("升级文件路径");
    File_Path_Edit.setParent(parent);
    Open_File_Button.setParent(parent);
    Open_File_Button.setText("打开文件");

    Usb_PidVid_Box.setParent(parent);
    Pid_Lable.setParent(parent);
    Pid_Lable.setText("PID");
    Vid_Lable.setParent(parent);
    Vid_Lable.setText("VID");
    Pid_Edit.setParent(parent);
    Vid_Edit.setParent(parent);
    Usb_Type_Box.setParent(parent);
    Open_Device_Button.setParent(parent);
    Open_Device_Button.setText("打开设备");
    Clear_Button.setParent(parent);
    Clear_Button.setText("清除显示");

    Usb_Type_Box.addItem(QString("系统设备"));
    Usb_Type_Box.addItem(QString("自定义设备"));

    QFont Font;
    Font.setPixelSize(30);
    Pid_Edit.setAlignment(Qt::AlignCenter);
    Pid_Edit.setFont(Font);
    Vid_Edit.setAlignment(Qt::AlignCenter);
    Vid_Edit.setFont(Font);

    Out_Edit.setParent(parent);
}


void My_Usb_Hid_Device::resize(int w, int h)
{
    int Lable_Y = (h / 10);

    int Path_Edit_X = (w / 10) * 4;
    int Path_Edit_Y = Lable_Y;

    int Show_Edit_X = w;
    int Show_Edit_Y = h - Lable_Y * 2;

    int Button_X = (w / 10) * 1;
    int Button_Y = Lable_Y;

    Update_File_Path_Lable.resize(Path_Edit_X, Path_Edit_Y);
    Update_File_Path_Lable.move(0, 0);

    File_Path_Edit.resize(Path_Edit_X, Path_Edit_Y);
    File_Path_Edit.move(Path_Edit_X, 0);

    Open_File_Button.resize(Button_X * 2, Button_Y);
    Open_File_Button.move(Path_Edit_X * 2, 0);

    Usb_PidVid_Box.resize(Button_X * 3, Button_Y);
    Usb_PidVid_Box.move(0, Lable_Y);

    Pid_Lable.resize(Button_X, Button_Y);
    Pid_Lable.move(Button_X * 3, Lable_Y);

    Pid_Edit.resize(Button_X, Button_Y);
    Pid_Edit.move(Button_X * 4, Lable_Y);

    Vid_Lable.resize(Button_X, Button_Y);
    Vid_Lable.move(Button_X * 5, Lable_Y);

    Vid_Edit.resize(Button_X, Button_Y);
    Vid_Edit.move(Button_X * 6, Lable_Y);

    Usb_Type_Box.resize(Button_X, Button_Y);
    Usb_Type_Box.move(Button_X * 7, Lable_Y);

    Open_Device_Button.resize(Button_X, Button_Y);
    Open_Device_Button.move(Button_X * 8, Lable_Y);

    Clear_Button.resize(Button_X, Button_Y);
    Clear_Button.move(Button_X * 9, Lable_Y);

    Out_Edit.resize(Show_Edit_X, Show_Edit_Y);
    Out_Edit.move(0, Path_Edit_Y + Lable_Y);
}

void My_Usb_Hid_Device::Open_File_Button_Clicked()
{
    FilePath = QFileDialog::getOpenFileName(this, "打开文件", FilePath, "*.bin;;*.hex");

    File_Path_Edit.setText(FilePath);
    if(FilePath.isEmpty())
    {
        //QMessageBox::warning(this,"警告","没有选择文件");
        return;
    }
    else
    {

    }
}

void My_Usb_Hid_Device::Open_Device_Button_Clicked()
{
    hidDev = devList[Usb_PidVid_Box.currentIndex()];

    if(hidDev.isInitialized())
    {
        if(USB_HID_DEVICE_IsOpen == true)
        {
            USB_HID_DEVICE_IsOpen = false;
            Open_Device_Button.setText("打开设备");
            hidDev.close();
        }
        else
        {
            USB_HID_DEVICE_IsOpen = hidDev.open();
            if(USB_HID_DEVICE_IsOpen)
            {
                USB_HID_DEVICE_IsOpen = true;
                Open_Device_Button.setText("关闭设备");

                if(hidDev.readAvailable())//假如有数据可读
                {
                    std::string ring = hidDev.read(-1);//从设备读取数据
                }
            }
        }
    }
    Out_Edit.clear();
    Out_Edit.append("Manufacturer: " + QString::fromStdWString(devList[Usb_PidVid_Box.currentIndex()].getManufacturer()));
    Out_Edit.append("Vendor Id   : " + QString("0X%1").arg(devList[Usb_PidVid_Box.currentIndex()].getVendorId(), 4, 16, QLatin1Char('0')));
    Out_Edit.append("Product Id  : " + QString("0X%1").arg(devList[Usb_PidVid_Box.currentIndex()].getProductId(),4, 16, QLatin1Char('0')));

    //hidDev.close();
}

void My_Usb_Hid_Device::Clear_Button_Clicked()
{
    Out_Edit.clear();
}

void My_Usb_Hid_Device::USB_HID_Listen_Clicked()
{
    devList = hidApi.scanDevices();
    if(Last_devList.size() != devList.size())
    {
        Last_devList = devList;

        Usb_PidVid_Box.clear();
        for(size_t i = 0; i < devList.size(); i++)//可打印扫描到的设备的信息
        {
            Out_Edit.append(QString("%1").arg(i) + ". DEVICE");
            Out_Edit.append(devList[i].getPath().c_str());
            Out_Edit.append("Serial      : " + QString::fromStdWString(devList[i].getSerial()));
            Out_Edit.append("Manufacturer: " + QString::fromStdWString(devList[i].getManufacturer()));
            Out_Edit.append("Product     : " + QString::fromStdWString(devList[i].getProductString()));
            Out_Edit.append("Vendor Id   : " + QString("0X%1").arg(devList[i].getVendorId(), 4, 16, QLatin1Char('0')));
            Out_Edit.append("Product Id  : " + QString("0X%1").arg(devList[i].getProductId(),4, 16, QLatin1Char('0')));
            Out_Edit.append("Release No  : " + QString("%1").arg(devList[i].getRelease(),    4, 10, QLatin1Char('0')));
            Out_Edit.append("Usage Page  : " + QString("%1").arg(devList[i].getUsagePage(),  4, 10, QLatin1Char('0')));
            Out_Edit.append("Usage       : " + QString("%1").arg(devList[i].getUsage(),      4, 10, QLatin1Char('0')));
            Out_Edit.append("Interface   : " + QString("%1").arg(devList[i].getInterface(),  4, 10, QLatin1Char('0')));
            Out_Edit.append(0);

            Usb_PidVid_Box.addItem(QString::fromStdWString(devList[i].getManufacturer()) + ":VID:" + QString("%1").arg(devList[i].getVendorId()) + "PID:" + QString("%1").arg(devList[i].getProductId()));
        }
    }
}

5.看看效果

相关推荐
车载小杜1 小时前
基于指针的线程池
开发语言·c++
沐知全栈开发1 小时前
Servlet 点击计数器
开发语言
m0Java门徒1 小时前
Java 递归全解析:从原理到优化的实战指南
java·开发语言
桃子酱紫君2 小时前
华为配置篇-BGP实验
开发语言·华为·php
QTX187302 小时前
JavaScript 中的原型链与继承
开发语言·javascript·原型模式
shaoing2 小时前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库
The Future is mine2 小时前
Python计算经纬度两点之间距离
开发语言·python
Enti7c3 小时前
HTML5和CSS3的一些特性
开发语言·css3
AAA废品回收站陈师傅3 小时前
19信号和槽_信号和槽的基本概念
qt
爱吃巧克力的程序媛3 小时前
在 Qt 创建项目时,Qt Quick Application (Compat) 和 Qt Quick Application
开发语言·qt