使用libusb的输出不正确 [英] Improper output using libusb

查看:102
本文介绍了使用libusb的输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have made a program using libusb. I doubt if the output is correct as all the entries show the same vendor and product id. Following is the code:

#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显示的内容:

this is what lsusb shows:

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

需要帮助找到错误(如果有).

Need help to locate the error(if any).

推荐答案

必须在某处意外跳过了一行...我建议您添加以下行,否则 ret 会被统一化:

A line must have been accidentally skipped somewhere... I suggest you add the following line, otherwise ret is unitialized :

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 以打印 idVendor idProduct 使用十六进制格式.

To compare the output to the one of lsusb, change for %x to print idVendor and idProduct using hexadecimal format.

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

以下问题 libusb半工作,但未声明libusb_device_descriptor吗?有助于查找缺失的行.这帮助我将 libusb Libusb未定义引用链接到通过 gcc main.c -o main -lusb-1.0 进行编译.

The following question libusb semi-working, but libusb_device_descriptor undeclared? was helpful to find the missing line. And this one helped me linking libusb Libusb undefined reference to it advices to compile by gcc main.c -o main -lusb-1.0.

这篇关于使用libusb的输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆