简体   繁体   English

无法理解 libusb API

[英]Unable to understand libusb API

I am working in libusb and have minimal knowledge on C++. I am trying to understand the API of libusb and write code accordingly.我在 libusb 工作,对 C++ 知之甚少。我试图理解libusb 的 API并相应地编写代码。 But I am unable to understand how to declare and use variables as variable or pointer or double or triple pointer.但是我无法理解如何将变量声明和使用为变量或指针或双指针或三指针。

  1. The below code is from question .以下代码来自问题 How to find what level of pointers to be used while declaring these variables from the API documentation.如何在 API 文档中声明这些变量时找到要使用的指针级别。 Is there any documentation or tutorial videos that explains these things.是否有任何文档或教程视频可以解释这些内容。

     libusb_context *context = NULL; libusb_device_handle *dev_handle = NULL; libusb_device **devs; int rc = 0; ssize_t count; //holding number of devices in list

For example, consider libusb_device_handle .例如,考虑libusb_device_handle How to declare this and use it?如何声明和使用它?

typedef struct libusb_device_handle libusb_device_handle
  1. The syntax of libusb_strerror() is const char * libusb_strerror (int errcode) . libusb_strerror()的语法是const char * libusb_strerror (int errcode) The function returns constant string. function 返回常量字符串。 Should I declare a char or char array or string to read the returned value.我应该声明一个 char 或 char 数组或字符串来读取返回值。 If the below way of usage right?如果下面的使用方式正确?

     char char *err_code;//declaration err_code = libusb_strerror(rc);

if the returned value is a string, then how can a characer pointer hold it?如果返回值是一个字符串,那么字符指针如何保存它呢?

An example would be really helpful.一个例子会很有帮助。

3)Here is my entire code. 3)这是我的全部代码。 I can open the device.我可以打开设备。 But the bulk transfer command returns 5 and fails.但是批量传输命令返回 5 并失败。 I am not sure at which part I am making mistake.我不确定我在哪一部分犯了错误。

#include <iostream>
#include "libusb.h"

#define IN_EP  0x81
#define OUT_EP 0x02

int main(){
    libusb_context *context = NULL;
    libusb_device_handle *dev_handle = NULL ;
    libusb_device **devs ;
    int rc = 100 ;
    //ssize_t count ; //holding number of devices in list
    unsigned int vid=0x1234;
    unsigned int pid=0x5678;
    unsigned char data[10];
    data[0]=128;
    int transferred = 0;
    unsigned int timeout = 5000;
    //std::string str[100];
    const char* err_code;
    

    rc = libusb_set_option(context, LIBUSB_OPTION_LOG_LEVEL,2);
    if (rc==0){    std::cout<<"libusb_setOption worked:"<<rc<<"\n"; }
    else{   std::cout<<"libusb_setOption_Failed:"<<rc<<"\n";    }


    rc = libusb_init(&context);
    if (rc==0){    std::cout<<"libusb_init worked:"<<rc<<"\n"; }
    else{   std::cout<<"libusb_init Failed"<<rc<<"\n";    }


    dev_handle = libusb_open_device_with_vid_pid(context,vid,pid);
    if (dev_handle == NULL){
        std::cout<<"libusb_open Failed"<<"\n";
        libusb_exit(context);std::cout<<"libusb_exit"<<"\n";exit(1);    }
    else{    std::cout<<"libusb_opened"<<"\n";    }

    
    rc = libusb_bulk_transfer(dev_handle,OUT_EP,data,1,&transferred, timeout);  //Send data to device
    if (rc==0){        std::cout<<"libusb_write worked:"<<rc<<"; Wrote "<<transferred<<" bytes\n";    }
    else{        std::cout<<"libusb__Write failed"<<rc<<"; Wrote "<<transferred<<" bytes\n";    }
    err_code = libusb_strerror(rc);
    
    rc = libusb_bulk_transfer(dev_handle,IN_EP,data,3,&transferred, timeout);   //Read data from device
    if (rc==0){        std::cout<<"libusb_read worked:"<<rc<<" ; Read"<<transferred<<" bytes; Data:"<<data<<"\n";
    std::cout<<data[0]<<" "<<data[1]<<" "<<data[2]<<"\n";  }
    else{        std::cout<<"libusb__read failed"<<rc<<"; Read "<<transferred<<" bytes\n";    }

    
    libusb_close(dev_handle);
    std::cout<<"libusb_close"<<"\n";
    
    
    libusb_exit(context);
    std::cout<<"libusb_close"<<"\n";
    
    
    return 0;
    }

Any help will be appreciated.任何帮助将不胜感激。

  1. [...] For example, consider libusb_device_handle . [...] 例如,考虑libusb_device_handle How to declare this and use it?如何声明和使用它?

You declare it exactly like in your code:您在代码中声明它完全一样:

libusb_device_handle *dev_handle = NULL;

Because libusb_open_device_with_vid_pid() returns a pointer to libusb_device_handle , your dev_handle must also be a pointer to that.因为libusb_open_device_with_vid_pid()返回指向libusb_device_handle的指针,所以您的dev_handle也必须是指向它的指针。

You might be confused because of devs being a pointer-to-pointer.您可能会感到困惑,因为devs是指针对指针。 This is because it is actually returning a pointer to an array, but since you are not even using that in your code, I would forget about it for now.这是因为它实际上返回一个指向数组的指针,但由于您甚至没有在代码中使用它,所以我现在暂时忘记它。

  1. The syntax of libusb_strerror() is const char * libusb_strerror (int errcode) . libusb_strerror()的语法是const char * libusb_strerror (int errcode) The function returns constant string. function 返回常量字符串。 Should I declare a char or char array or string to read the returned value?我应该声明一个 char 或 char 数组或字符串来读取返回值吗?

You should declare a variable exactly the same as the return type of libusb_strerror() , thus:您应该声明一个与libusb_strerror()的返回类型完全相同的变量,因此:

const char *err_string;
err_string = libusb_strerror(rc);

If the returned value is a string, then how can a characer pointer hold it?如果返回值是一个字符串,那么字符指针如何保存它呢?

The returned value is a pointer, you just make a copy of the pointer.返回值是一个指针,你只需复制一个指针。 The pointer points to some part of memory where the string is stored.指针指向存储字符串的 memory 的某个部分。 You don't have to worry about how libusb allocated it.你不必担心 libusb 是如何分配它的。

  1. I can open the device.我可以打开设备。 But the bulk transfer command returns 5 and fails.但是批量传输命令返回 5 并失败。 I am not sure at which part I am making mistake.我不确定我在哪一部分犯了错误。

The code looks mostly fine, except you want to call libusb_strerror() after the second call to libusb_bulk_transfer() , not right before it.代码看起来基本没问题,除非您想在第二次调用libusb_strerror()之后调用libusb_bulk_transfer() ,而不是在它之前。 Error code -5 is LIBUSB_ERROR_NOT_FOUND .错误代码 -5 是LIBUSB_ERROR_NOT_FOUND This might mean it didn't find the endpoint.这可能意味着它没有找到端点。 Are you sure IN_EP and OUT_EP are set correctly?您确定IN_EPOUT_EP设置正确吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM