需要 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
I'm writing user-space program that is intended to control some device via usb so I decided to use libusb (libusb-1.0) to send control messages to and receive responses from that device.
But I constantly receive the following bunch of errors from my code (even when it's executed 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 is:
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's wrong with the code? And how it could be fixed?
OS: Ubuntu 10.10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答这个问题是因为我在同一操作系统上遇到过这个问题,并且能够通过以下方式解决:
下载并编译最新的 libusb 源代码 1.0.8。
以下是我用来声明 USB 接口 0 的一些 API 调用:
上例中的变量说明:
要获取供应商 ID 和产品 ID,您可以运行以下命令(例如我的设备信息)
粗体冒号分隔的字符串分别包含供应商和产品 ID。
如何编译代码:
我使用以下命令来编译我的代码:
将解压出来的libusb-1.0.8目录下的libtool复制到编译区。
希望这有帮助。
Answering this question as I had faced this issue on the same OS and was able to solve in the following manner:
Download and Compile the latest libusb source code 1.0.8.
Following are some API calls that I used in order to claim USB interface 0:
Description of the variables in the above example:
To get the vendor ID and product ID, you may run the following commands (Ex. my device info)
The bold colon delimited string contains vendor and product id respectively.
How to compile the code:
I used the following command to compile my code:
Copy libtool to the compilation area from the extracted libusb-1.0.8 directory.
Hope this helps.
在设置其配置并声明它之前,您不需要 open() 设备吗?
Don't you need to open() the device before you set its configuration and claim it?
这是 libusb 程序的通用示例,您可以根据需要进行调整。
另请参阅API,非常棒!
here's a generic example of a libusb program that you can adapt as needed.
also see the API, which is great!