简体   繁体   English

如何使用libusb-1.0接收HID报告?

[英]How do I receive HID reports using libusb-1.0?

I have a USB HID scale that I need to fetch the weighing reports from. 我有一个USB HID刻度,我需要从中获取称重报告。 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. 我可以通过从/dev/hidraw#一次读取7个字节来在Linux上执行此操作,但我想使用libusb-1.0获取相同的信息。

Even when I do get some non-null bytes, I get error -9: LIBUSB_ERROR_PIPE 即使我得到一些非空字节,我得到错误-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]);
    }

An example to read from a USB HID card reader using libusb-win - 使用libusb-win从USB HID读卡器读取的示例 -

http://rowsandcolumns.blogspot.com/2011/02/read-from-magtek-card-swipe-reader-in.html http://rowsandcolumns.blogspot.com/2011/02/read-from-magtek-card-swipe-reader-in.html

HID uses Interrupt Transfer AFAIK. HID使用中断传输AFAIK。 You need to rewrite your code to use these. 您需要重写代码才能使用它们。 And have a look at thouse descriptors - they tell you which interface to use. 并查看thouse描述符 - 它们告诉您使用哪个接口。

That said I think its much easier to use /dev/hdiraw# then libusb in this case. 这就是说我认为在这种情况下使用/ dev / hdiraw​​#然后libusb更容易。

Try to use another value for wValue ( 0x0300 , for example). 尝试为wValue使用另一个值(例如0x0300 )。

Also check bmRequestType and bRequest parameters: bmRequestType must be equal to 0xA1 , bRequest0x01 . 还要检查bmRequestTypebRequest参数: bmRequestType必须等于0xA1bRequest - 0x01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM