libusb基本例子想要的

我正在写用户空间程序,旨在通过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

回答这个问题,因为我在同一个操作系统上面对这个问题,并能够以下列方式解决:

下载并编译最新的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东芝公司金士顿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

将libtool从解压缩的libusb-1.0.8目录复制到编译区域。

希望这可以帮助。

在设置配置并声明之前,您是否需要打开()设备?