调用libusb_bulk_transfer在某些设备上挂起,在其他设备上工作

时间:2015-08-03 14:44:23

标签: c linux usb barcode-scanner libusb

我编写了以下代码来访问raspbian系统上的USB条形码扫描仪,并使用libusb从它们中读取扫描数据。在最长的时间里,我遇到了libusb_bulk_transfer的呼叫不会返回的问题;虽然使用了扫描仪,但是呼叫并没有返回数据。如果给出超时参数,则会超时。

我公司使用三种不同型号的条码扫描仪。一旦我尝试使用不同的模型进行测试,程序就按预期开始工作,即stdout有一些输出给我预期的数据。

进一步的测试表明它适用于我们拥有的三种扫描仪中的两种。每当我使用第三种扫描仪时,程序就会调到调用libusb_bulk_transfer的行,然后阻塞。

所有扫描仪都设置为"键盘"模式。 我还尝试使用libusb在分离内核驱动程序之后和声明接口之前更改扫描程序的配置。将配置设置为1(我还不知道如何确定哪些值有效)适用于程序按预期工作的扫描程序,但同样的调用给了我-6(LIBUSB_ERROR_BUSY)对于我无法工作的设备。尝试释放现有的句柄,如文档中所建议的,给我一个错误-5(LIBUSB_ERROR_NOT_FOUND)我尝试的接口ID值。

我忘记了什么吗?我怎样才能更详细地弄清楚出了什么问题?

这是在raspbian 4.0.8-v7 +上,使用libusb-1.0.19。

这是代码。为简洁起见,删除了错误处理和输出。

#include <stdlib.h>
#include <stdio.h>
#include "libusb.h"

int main(void) {
    libusb_device_handle *dev_handle;
    libusb_context *ctx = NULL;

    libusb_init(&ctx);
    libusb_set_debug(ctx, 3);

    int vendorID = 0xXXXX, productID = 0xYYYY;

    dev_handle = libusb_open_device_with_vid_pid(ctx, vendorID, productID);

    if(libusb_kernel_driver_active(dev_handle, 0) == 1) {
        libusb_detach_kernel_driver(dev_handle, 0);
    }

    libusb_set_auto_detach_kernel_driver(dev_handle, 0);

    libusb_set_configuration(dev_handle, 1);

    libusb_claim_interface(dev_handle, 0);

    int bytes_read = 0;
    unsigned char data[8];
    int bytes_to_read = sizeof(data);
    int endpointID = 0x81;

    do {
        int r = libusb_bulk_transfer(
              dev_handle, 
              endpointID,
              data, 
              bytes_to_read, 
              &bytes_read, 
              0);

        for (int bi = 0; bi < bytes_read; ++bi) {
            printf("0x%02X  ", data[bi]);
        }
        printf("\n");
    } while (r == 0);

    libusb_release_interface(dev_handle, 0);

    libusb_close(dev_handle);
    libusb_exit(ctx);

    return 0;
}

0 个答案:

没有答案