【问题标题】:libusb basic example wanted想要 libusb 基本示例
【发布时间】:2011-01-27 07:31:11
【问题描述】:

我正在编写旨在通过 USB 控制某些设备的用户空间程序,因此我决定使用 libusb (libusb-1.0) 向该设备发送控制消息并接收来自该设备的响应。

但我经常从我的代码中收到以下一堆错误(即使它是使用 'sudo' 执行的):

USB error: could not set config 0: Device or resource busy
set configuration: failed
Check that you have permissions to write to 007/012 and, if you don't, that you set up hotplug (http://linux-hotplug.sourceforge.net/) correctly.
USB error: could not claim interface 0: Device or resource busy
claim interface: failed
USB error: error submitting URB: No such file or directory
bulk writing: failed
USB error: error submitting URB: No such file or directory
bulk reading: failed
response was: 

代码是:

usb_dev_handle* find_device ();

int 
main (int argc, char *argv[])
{
    usb_dev_handle* udev;
    int status;
    char request[] = "K1"; // 'ping' command used to check communication
    char response[256];

    udev = find_device ();
    // udev is successfully found here

    status = usb_set_configuration (udev, 0);
    printf ("set configuration: %s\n", status ? "failed" : "passed");

    status = usb_claim_interface (udev, 0);
    printf ("claim interface: %s\n", status ? "failed" : "passed");

    status = usb_bulk_write (udev, 3, request, sizeof (request), 500);
    printf ("bulk writing: %s\n", status ? "failed" : "passed");

    status = usb_bulk_read (udev, 2, response, sizeof (response), 500);
    printf ("bulk reading: %s\n", status ? "failed" : "passed");

    printf ("response was: %s\n", response);

    usb_close (udev);

    return 0;
}

代码有什么问题?以及如何解决?

操作系统:Ubuntu 10.10

【问题讨论】:

  • 您能否让它在您的设备上进行读写?您可以发布的代码是否有重大更改?

标签: linux libusb


【解决方案1】:

回答这个问题,因为我在同一个操作系统上遇到过这个问题,并且能够通过以下方式解决:

下载并编译最新的libusb源代码1.0.8。

以下是我用来声明 USB 接口 0 的一些 API 调用:

libusb_init(NULL);
libusb_open_device_with_vid_pid(NULL, vendor_id, product_id);
libusb_detach_kernel_driver(devh, 0);
libusb_claim_interface(devh, 0);
libusb_close(devh);
libusb_exit(NULL);

上例中的变量说明:

static struct libusb_device_handle *devh = NULL;
uint16_t vendor_id;
uint16_t product_id;

要获取供应商 ID 和产品 ID,您可以运行以下命令(例如我的设备信息)

$ lsusb
...
总线 001 设备 013:ID 0930:6544 Toshiba Corp. Kingston DataTraveler 2.0 Stick (2GB)
...

粗体冒号分隔的字符串分别包含供应商和产品ID。

如何编译代码:

我使用以下命令编译我的代码:

/bin/bash libtool --silent --tag=CC --mode=link g++ -Wall -Wundef -Wunused -Wshadow -D_DEBUG -I../libusb -g -O2 -o read read.cpp .. /libusb/libusb-1.0.la -lusb-1.0 -lrt

将解压出来的libusb-1.0.8目录下的libtool复制到编译区。

希望这会有所帮助。

【讨论】:

  • 要不要在认领接口前设置好配置?另外,描述符中的哪些数字是配置、接口和备用设置参数?
【解决方案2】:

在设置设备配置并声明它之前,您不需要 open() 设备吗?

【讨论】:

    【解决方案3】:

    这是一个通用的 libusb 程序示例,您可以根据需要进行调整。

    另见the API,太棒了!

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <libusb.h>
    #include <err.h>
    
    #define MFGR_ID 0 // given manufacturer ID 
    #define DEV_ID 0  // given device ID
    
    /* If device IDs are not known, use libusb_get_device_list() to see a 
    list of all USB devices connected to the machine. Follow this call with    
    libusb_free_device_list() to free the allocated device list memory.
    */
    
    
    int main() {
        int init = libusb_init(NULL); // NULL is the default libusb_context
    
        if (init < 0) {
            errx(1,”\n\nERROR: Cannot Initialize libusb\n\n”);  
        }
    
        struct libusb_device_handle *dh = NULL; // The device handle
        dh = libusb_open_device_with_vid_pid(NULL,MFGR_ID,DEV_ID);
    
        if (!dh) {
            errx(1,”\n\nERROR: Cannot connect to device %d\n\n”,DEV_ID);
        }
    
        // set fields for the setup packet as needed              
        uint8_t       bmReqType = 0;   // the request type (direction of transfer)
        uint8_t            bReq = 0;   // the request field for this packet
        uint16_t           wVal = 0;   // the value field for this packet
        uint16_t         wIndex = 0;   // the index field for this packet
        unsigned char*   data = ‘ ‘;   // the data buffer for the in/output data
        uint16_t           wLen = 0;   // length of this setup packet 
        unsigned int     to = 0;       // timeout duration (if transfer fails)
    
        // transfer the setup packet to the USB device
        int config =     
        libusb_control_transfer(dh,bmReqType,bReq,wVal,wIndex,data,wLen,to);
    
        if (config < 0) {
            errx(1,”\n\nERROR: No data transmitted to device %d\n\n”,DEV_ID);
        }
    
        // now you can use libusb_bulk_transfer to send raw data to the device
    
        libusb_exit(NULL);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-04
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2014-01-24
      相关资源
      最近更新 更多