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

发布于 2024-10-15 14:05:19 字数 1105 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢烬 2024-10-22 14:05:19

使用 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

遗弃M 2024-10-22 14:05:19

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.

情独悲 2024-10-22 14:05:19

尝试为 wValue 使用其他值(例如 0x0300)。

另请检查 bmRequestTypebRequest 参数:bmRequestType 必须等于 0xA1bRequest0x01

Try to use another value for wValue (0x0300, for example).

Also check bmRequestType and bRequest parameters: bmRequestType must be equal to 0xA1, bRequest0x01.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文