1

我正在尝试编写一个应用程序来读取和写入连接的 USB 设备。我正在使用 libusb。似乎找到设备后,配置失败。我正在关注http://libusb.sourceforge.net/doc/index.html上的 libusb 开发人员指南。其中指出:

usb_set_configuration 设置设备的活动配置。配置参数是描述符字段 bConfigurationValue 中指定的值。成功返回 0,错误返回 < 0。

我在源代码(如下)中演示了我使用 bConfigurationValue 作为配置参数,但我从 usb_set_configuration 的返回值始终为 -1。我不明白为什么?

输出:

[user@local workspace]$ ./application 
Device Found @ Address 005 
Vendor ID 0x018d1
Product ID 0x04e12
INFO: bConfigurationValue = 1
config status =-1
claim status =-1
alt status =-22
TX status =-1
RX status =-1 -> RX char = 0
[user@local workspace]$

来源:

int main(int argc, char *argv[])
{
    struct usb_bus *bus;
    struct usb_device *dev;
    struct usb_dev_handle *android_handle;
    int status;
    unsigned char send_data = 0x51;
    unsigned char receive_data = 0;

    usb_init();

    if ((handle = locate_device()) == 0)
    {
        printf("Error: Could not open the device\n");
        return (-1);
    }

    printf("INFO: bConfigurationValue = %d\n", config_val);
    status = usb_set_configuration(handle, config_val);
    printf("config status = %d\n", status);

    status = usb_claim_interface(handle, 0);
    printf("claim status = %d\n", status);

    open_status = usb_set_altinterface(handle, 0);
    printf("alt status = %d\n", status);

    status = usb_bulk_write(handle, 4, &send_data, 1, 500);
    printf("TX status = %d\n", status);

    usb_bulk_read(handle, 3, &receive_data, 1, 500);
    printf("RX status = %d -> RX char = %d\n", status, receive_data);

    usb_close(handle);

    return 0;
}

usb_dev_handle *locate_device(void) 
{ 
    ... 
    config_val = dev->config[myDeviceIndex].bConfigurationValue;
    ...
}

编辑:

以 root 身份运行应用程序时返回以下错误代码:

Device Found @ Address 005 
Vendor ID 0x018d1
Product ID 0x04e12
INFO: bConfigurationValue = 1
config status = -16
claim status = -16
alt status = -22
TX status = -16
RX status = -16 -> RX char = 0
4

0 回答 0