A basic libusb example is required.

I am writing a user space program designed to control some device via usb, so I decided to use libusb (libusb-1.0) to send control messages and receive responses from this device.

But I constantly get the following bunch of errors from my code (even when it runs using '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: 

The code:

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

What is wrong with the code? And how can this be fixed?

OS: Ubuntu 10.10

+3
source share
2 answers

Answering this question, as I ran into this problem on the same OS and was able to solve the following:

Download and compile the latest libusb 1.0.8 source code.

Below are some API calls that I used to request the USB 0 interface:

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;

, (, )

$lsusb
...
Bus 001 013: 0930: 6544 Toshiba Corp. Kingston DataTraveler 2.0 Stick (2GB)
...

.

:

:

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

libtool libusb-1.0.8.

, .

+9

() , ?

+3

Source: https://habr.com/ru/post/1788120/


All Articles