c - 使用 libusb 输出不正确

标签 c linux libusb

我用 libusb 做了一个程序。我怀疑输出是否正确,因为所有条目都显示相同的供应商和产品 ID。以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>

void print_devices(libusb_device *dev)
{
    struct libusb_device_descriptor desc;
    struct libusb_config_descriptor *config;

    const struct libusb_interface *inter;
    const struct libusb_interface_descriptor *interdesc;
    const struct libusb_endpoint_descriptor *endpointdesc;

    int ret;
    int i,j,k;

    if(ret<0)
    {   
        fprintf(stderr,"error in getting device descriptor\n");
        return;
    }

    printf("Number of possible configs is %d\n",desc.bNumConfigurations);
    printf("Vendor: %d\n",desc.idVendor);
    printf("Product ID: %d\n",desc.idProduct);

    libusb_get_config_descriptor(dev, 0, &config);

    printf("Interface: %d\n", config->bNumInterfaces);
    printf("\n\n"); 
}


int main(int argc,char *argv[])
{
    libusb_device **devs;
    libusb_context *context = NULL;

    size_t list;
    size_t i;

    int ret,temp;

    ret = libusb_init(&context);

    if(ret < 0)
    {
        perror("libusb_init");
        exit(1);
    }

    list = libusb_get_device_list(context, &devs);

    if(list < 0)
    {
        fprintf(stderr, "Error in getting device list\n");
        libusb_free_device_list(devs, 1);
        libusb_exit(context);
        exit(1);
    }

    temp=(int)list;
    printf("\n%d devices found\n\n",temp);

    for(i=0;i<temp;i++)
    {
        //print devices
        print_devices(devs[i]);
    }

    libusb_free_device_list(devs, 1);
    libusb_exit(context);

    return 0;
}

这是我的输出:

anubhav@anubhav-Inspiron-3421:~/Desktop/usb$ ./usbtest

9 devices found

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 2

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 4

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1

Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1

这是 lsusb 显示的内容:

anubhav@anubhav-Inspiron-3421:~/Desktop/usb$ lsusb
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 064e:812c Suyin Corp. 
Bus 001 Device 007: ID 0a5c:21d7 Broadcom Corp. BCM43142 Bluetooth 4.0
Bus 001 Device 003: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

需要帮助定位错误(如果有的话)。

最佳答案

一定是某处不小心跳过了一行...我建议你添加以下行,否则 ret 被初始化:

int ret;
int i,j,k;

ret = libusb_get_device_descriptor(dev, &desc);//this line !
if(ret<0)
{   
    fprintf(stderr,"error in getting device descriptor\n");
    return;
}

要将输出与 lsusb 的输出进行比较,将 %x 更改为打印 idVendoridProduct使用十六进制格式。

printf("Vendor: %x\n",desc.idVendor);
printf("Product ID: %x\n",desc.idProduct);

下面的问题libusb semi-working, but libusb_device_descriptor undeclared?有助于找到丢失的行。这个帮助我链接了 libusb Libusb undefined reference to它建议通过 gcc main.c -o main -lusb-1.0 编译。

关于c - 使用 libusb 输出不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29083184/

相关文章:

C 中整数指针的常量值分配适用于这种情况

c - 在 C 中初始化和重新初始化全局字符串数组

linux - 使用 shell 脚本检查 EOF

java - 获取 USB 设备的字符串描述符

c - 如何理解这种函数声明?

c - 数学库 native Visual C++

linux - 无法编译基本的Rust程序:与`cc`的链接失败

c - 在 glibc 源代码中哪里可以找到 select() 源代码?

c++ - 为什么在使用 libusb 与 PIC 18F2550 通信时读取或写入超过 3 个字节时会出错?

python - Windows 7 上的 Pyusb 找不到任何设备