libusb-win32 - 无法从键盘读取
我正在尝试在 Windows 7 下为键盘(HID,如果重要的话)编写一个自定义“驱动程序”。最终目标是将两个键盘连接到计算机,但将其中一个键盘的所有键映射到特殊的(自定义)功能。
我的想法是使用 libusb-win32 作为第二个键盘的驱动程序,并编写一个小程序来从键盘读取数据并对其进行操作。我已成功安装驱动程序,并且该设备已从我的程序中识别出来,但所有传输都超时,即使我正在按键。
这是我的代码:
struct usb_bus *busses;
struct usb_device *dev;
char buf[1024];
usb_init();
usb_find_busses();
usb_find_devices();
busses = usb_get_busses();
dev = busses->devices;
cout << dev->descriptor.idVendor << '\n' << dev->descriptor.idProduct << '\n';
usb_dev_handle *h = usb_open(dev);
cout << usb_set_configuration(h, 1) << '\n';
cout << usb_claim_interface(h, 0) << '\n';
cout << usb_interrupt_read(h, 129, buf, 1024, 5000) << '\n';
cout << usb_strerror();
cout << usb_release_interface(h, 0) << '\n';
cout << usb_close(h) << '\n';
它返回:(
1133
49941
0
0
-116
libusb0-dll:err [_usb_reap_async] timeout error
0
0
我在这 5 秒内按了很多键)
只有一根总线、一台设备、一种配置、一个接口和一个端点。 端点有 bmAttributes = 3
这意味着我应该使用中断传输(对吗?)
那么为什么我没有得到任何东西?我是否滥用libusb?你知道没有 libusb 的方法吗?
I'm trying to write a custom 'driver' for a keyboard (HID, if it matters), under Windows 7. The final goal is having two keyboards connected to the computer, but mapping all of the keys of one of them to special (custom) functions.
My idea is to use libusb-win32 as the 2nd keyboard's driver, and write a small program to read data from the keyboard and act upon it. I've successfully installed the driver, and the device is recognized from my program, but all transfers timeout, even though I'm pressing keys.
here's my code:
struct usb_bus *busses;
struct usb_device *dev;
char buf[1024];
usb_init();
usb_find_busses();
usb_find_devices();
busses = usb_get_busses();
dev = busses->devices;
cout << dev->descriptor.idVendor << '\n' << dev->descriptor.idProduct << '\n';
usb_dev_handle *h = usb_open(dev);
cout << usb_set_configuration(h, 1) << '\n';
cout << usb_claim_interface(h, 0) << '\n';
cout << usb_interrupt_read(h, 129, buf, 1024, 5000) << '\n';
cout << usb_strerror();
cout << usb_release_interface(h, 0) << '\n';
cout << usb_close(h) << '\n';
and it returns:
1133
49941
0
0
-116
libusb0-dll:err [_usb_reap_async] timeout error
0
0
(I'm pressing lots of keys in those 5 seconds)
There's only one bus, one device, one configuration, one interface and one endpoint.
The endpoint has bmAttributes = 3
which implies I should use interrupt transfers (right?)
so why am I not getting anything? Am I misusing libusb? Do you know a way to do this without libusb?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上非常简单 - 当从 USB 设备读取时,您必须准确读取正确的字节数。您可以通过阅读
wMaxPacketSize
来了解该金额。显然,任何其他大小的读取请求都会导致超时。
It's pretty simple actually - when reading from the USB device, you must read exactly the right amount of bytes. You know what that amount is by reading
wMaxPacketSize
.Apparently a read request with any other size simply results in a timeout.