如何使用 libusb-1.0 接收 HID 报告?
我有一个 USB HID 秤,需要从中获取称重报告。我可以在 Linux 上通过从 /dev/hidraw#
一次读取 7 个字节来完成此操作,但我想使用 libusb-1.0 获得相同的信息。
即使我确实获得了一些非空字节,我也会收到错误 -9: LIBUSB_ERROR_PIPE
我正在尝试使用控制传输,如下所示:
#define WEIGH_REPORT_SIZE 7
/*
* Open a handle to the found scale
*/
libusb_open(dev, &handle);
#ifdef __linux__
libusb_detach_kernel_driver(handle, 0);
#endif
libusb_claim_interface(handle, 0);
/*
* Try to transfer data about status
*
*/
unsigned char data[WEIGH_REPORT_SIZE];
unsigned int len = libusb_control_transfer(
handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS |
LIBUSB_RECIPIENT_INTERFACE,
HID_REPORT_GET,
//wValue => hid report, no report ID
0x0100,
0x00, //windex => interface 0
data,
WEIGH_REPORT_SIZE, //wLength
10000 //timeout => 10 sec
);
int i;
printf("Got %d bytes from control transfer:\n", len);
for(i = 0; i < WEIGH_REPORT_SIZE; i++) {
printf("%x\n", data[i]);
}
I have a USB HID scale that I need to fetch the weighing reports from. I am able to do this on Linux by reading 7 bytes at a time from /dev/hidraw#
, but I would like to get the same information using libusb-1.0.
Even when I do get some non-null bytes, I get error -9: LIBUSB_ERROR_PIPE
I am attempting to use a control transfer like so:
#define WEIGH_REPORT_SIZE 7
/*
* Open a handle to the found scale
*/
libusb_open(dev, &handle);
#ifdef __linux__
libusb_detach_kernel_driver(handle, 0);
#endif
libusb_claim_interface(handle, 0);
/*
* Try to transfer data about status
*
*/
unsigned char data[WEIGH_REPORT_SIZE];
unsigned int len = libusb_control_transfer(
handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS |
LIBUSB_RECIPIENT_INTERFACE,
HID_REPORT_GET,
//wValue => hid report, no report ID
0x0100,
0x00, //windex => interface 0
data,
WEIGH_REPORT_SIZE, //wLength
10000 //timeout => 10 sec
);
int i;
printf("Got %d bytes from control transfer:\n", len);
for(i = 0; i < WEIGH_REPORT_SIZE; i++) {
printf("%x\n", data[i]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 libusb-win 从 USB HID 读卡器读取数据的示例 -
http://rowsandcolumns.blogspot.com/2011/02/read-from-magtek-card-swipe-reader-in.html
An example to read from a USB HID card reader using libusb-win -
http://rowsandcolumns.blogspot.com/2011/02/read-from-magtek-card-swipe-reader-in.html
HID 使用中断传输 AFAIK。您需要重写代码才能使用这些。看看那些描述符 - 它们告诉您要使用哪个接口。
也就是说,我认为在这种情况下使用 /dev/hdiraw# 比使用 libusb 容易得多。
HID uses Interrupt Transfer AFAIK. You need to rewrite your code to use these. And have a look at thouse descriptors - they tell you which interface to use.
That said I think its much easier to use /dev/hdiraw# then libusb in this case.
尝试为
wValue
使用其他值(例如0x0300
)。另请检查
bmRequestType
和bRequest
参数:bmRequestType
必须等于0xA1
、bRequest
—0x01
。Try to use another value for
wValue
(0x0300
, for example).Also check
bmRequestType
andbRequest
parameters:bmRequestType
must be equal to0xA1
,bRequest
—0x01
.