本教程介绍如何使用QT编程框架通过HID协议获取特定USB设备,并实现从该设备读取数据的功能。适合需要与USB硬件交互的开发者学习参考。
文件夹包含hidapi.h, hidapi.dll 和 hidapi.lib 文件,在 Windows 10 系统使用 VS2013 编译的 release 版本中用于 Qt 的调用,这是可以实现的。
调用过程如下:
```cpp
int res;
res = hid_init();
wchar_t wstr[MAX_STR];
int i;
// 使用 VID, PID 和可选序列号打开设备。
handle = hid_open(0x0483, 0x5750, NULL);
if(handle == NULL) {
qDebug() << NULL-----------------------NULL;
return;
} else {
qDebug() << not ------------NULL-----------------------NULL;
}
// 获取制造商字符串
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
wprintf(LManufacturer String: %s\n, wstr);
// 获取产品字符串
res = hid_get_product_string(handle, wstr, MAX_STR);
wprintf(LProduct String: %s\n, wstr);
// 获取序列号字符串
res = hid_get_serial_number_string(handle, wstr, MAX_STR);
wprintf(LSerial Number String: (%d) %s\n, wstr[0], wstr);
// 读取索引字符串1
res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
wprintf(LIndexed String 1: %s\n, wstr);
qDebug(hid read start);
int res = hid_set_nonblocking(handle, 0); // 设置为非阻塞模式
while (true) {
res = hid_read(handle,buf,sizeof(buf));
QString asd;
for(int i = 0; i < sizeof(buf); ++i){
char str[20];
sprintf(str , %02x, buf[i]);
asd += str ;
}
if(!cardInfo.contains(asd.toUpper())) {
cardInfo.append(asd.toUpper());
for(int i = 0; i < cardInfo.size(); ++i) {
dealWithData(cardInfo[i]);
}
}
}
```
这段代码展示了如何使用 HID API 库来与特定的 USB 设备进行通信,包括初始化库、打开设备、读取相关字符串信息以及设置非阻塞模式下持续读取数据。