使用 libusb 的 Linux USB 应用程序

发布于 2024-12-07 08:31:25 字数 2152 浏览 2 评论 0原文

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

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

I am trying to write an application to read and write to a connected USB device. I am using libusb. It seems after I find the device, configuration fails. I am following the developers guide for libusb at http://libusb.sourceforge.net/doc/index.html. Which states that:

usb_set_configuration sets the active configuration of a device. The configuration parameter is the value as specified in the descriptor field bConfigurationValue. Returns 0 on success or < 0 on error.

I demonstrate in my source (below) that I use the bConfigurationValue as the configuration parameter, but my return value from usb_set_configuration is always -1. I don't understand why?

The output:

[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]$

The source:

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;
    ...
}

EDIT:

The following error codes are returned when the application is ran as 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文