赞
踩
1. 下载uabhid的开源文件用VS2010生成hidapi.dll与hidapi.lib。
2.用Qt Creator建立一个工程USB_HID,将hidapi.h与hidapi.lib拷贝到工程下。
3.在USB_HID.pro最后添加生成的库
LIBS += -L$$_PRO_FILE_PWD_/ -lhidapi
4.简单测试代码
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include "hidapi.h" const unsigned char Cmdbuf[9]={0x06,0x01,0x03,0x00,0x54,0x00,0x02,0x85,0xdb}; MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); int res; hid_device * handle; res = hid_init(); // 这一句可要可不要 qDebug("hid init res %d", res); handle = hid_open(0xC258,0x3A0A,NULL);// 0x0483, 0x5750, NULL); unsigned char buf[128]; unsigned char readBuf[65]; memset(buf, 0, sizeof(buf)); for(int i = 0;i < 9;i++) buf[i] = Cmdbuf[i]; //第一个字节是report id需要和下位机保持一致 res = hid_write(handle, buf, 64); qDebug("hid write res %d", res); if(64 != res) { qDebug("%ls ", hid_error(handle)); } else { res = hid_read_timeout(handle,readBuf, sizeof(readBuf),100); //返回读取的字节,100为超时 if(64 != res) { qDebug("%ls ", hid_error(handle)); } } res = hid_exit(); qDebug("hid exit res %d", res); } |
5.将hidapi.dll放到生成的exe的目录下即可运行。
详见
(1184条消息) Qt 调用USB HID设备读写_qt usb hid_xflySnail的博客-CSDN博客
测试代码,包含dll与lib
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。