Question

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]);
    }
Was it helpful?

Solution

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

OTHER TIPS

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top