Qt学习:Qt5.9.2+vs2017移植usb第三方库
接下来用 Debug x64模式编译文件,会生成所需要的动态库。 我们需要三个文件:hidapi.h hidapi.dll hidapi.lib hidapi.dll 和 hidapi.lib 在所建工程的一级目录下 x64 ->debug下
接下来新建一个测试工程 :hidtest 将头文件hidapi.h拷贝到工程目录下, 新建hidtest.cpp,加入如下代码进行简单测试:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include “hidapi.h” int main(int argc, char* argv[]) { int res; unsigned char buf[256]; #define MAX_STR 255 wchar_t wstr[MAX_STR]; hid_device *handle; int i;
#ifdef WIN32 UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); #endif
struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
printf("Device Found
type: %04hx %04hx
path: %s
serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("
");
printf(" Manufacturer: %ls
", cur_dev->manufacturer_string);
printf(" Product: %ls
", cur_dev->product_string);
printf(" Release: %hx
", cur_dev->release_number);
printf(" Interface: %d
", cur_dev->interface_number);
printf("
");
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
// Open the device using the VID, PID,
// and optionally the Serial number.
handle = hid_open(0x4d8, 0x3f, L"12345");
handle = hid_open(0x17ef, 0x6018, NULL);
if (!handle) {
printf("unable to open device
");
return 1; //可以通过返回值确认程序出错位置
}
system("pause");
return 0;
} 进行工程属性设置: 在附加库目录中加入hidapi.lib所在文件夹目录 在附加依赖项中加入hidapi.lib
如果编译通过,返回值为0,说明该库正确,可以正确打开hidusb设备。
注意代码:handle = hid_open(0x17ef, 0x6018, NULL); 此处vid和hid值需要通过前面枚举出的设备值修改以对应自己电脑上的设备,否则无法正确打开。 调试里可以先把打开设备那几行代码注释掉,单独查看枚举出的hidusb设备信息。否则会因为if语句中的return 1; 导致程序一闪而过,无法观察。
调试好这个,接下来可以在qt工程中正常使用代码。 注意配置好工程,拷贝好相应文件。
