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.看看效果