C++ 如何使用 libusb 和 libusb_get_device_descriptor()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14722083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use libusb and libusb_get_device_descriptor()?
提问by Stéphane
I'm learning to use libusb v1.0.0 for the first time on Ubuntu 12.10. Here is some small test code I'm using to try and understand how to use this API:
我正在学习在 Ubuntu 12.10 上第一次使用 libusb v1.0.0。这是我用来尝试了解如何使用此 API 的一些小测试代码:
#include <libusb-1.0/libusb.h>
...
libusb_device **list;
libusb_get_device_list(ctx, &list); // Returns 11 USB devices which is correct.
for (size_t idx = 0; list[idx] != NULL; idx ++)
{
libusb_device *dev = list[idx];
libusb_device_descriptor desc = {0};
int rc = libusb_get_device_descriptor(dev, &desc);
At this point, rc == 0, meaning it should have completed successfully. Source: documentation for *libusb_get_device_descriptor()*.
此时,rc == 0,意味着它应该已经成功完成。来源:*libusb_get_device_descriptor()* 的文档。
But the structure desc
is always empty. None of the fields ever get set. If I change the last two lines above to this:
但是结构desc
总是空的。没有任何字段被设置。如果我将上面的最后两行更改为:
libusb_device_descriptor desc = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int rc = libusb_get_device_descriptor(dev, &desc);
...then when libusb_get_device_descriptor()
returns, I see desc
remains unchanged, confirming for me that I'm not getting what I expect from this API.
...然后当libusb_get_device_descriptor()
返回时,我看到desc
保持不变,为我确认我没有从这个 API 中得到我期望的东西。
I've also tried to run a.out
as root just in case this requires elevated privileges. Doing a Google search on libusb_get_device_descriptor
hasn't gotten me anywhere.
我还尝试以a.out
root身份运行,以防万一这需要提升权限。进行谷歌搜索libusb_get_device_descriptor
并没有让我在任何地方。
Relevant commands I ran to try this code:
我运行的相关命令尝试此代码:
sudo apt-get install libusb-1.0.0-dev
g++ -ggdb test.cpp -lusb-1.0
./a.out
Ah! Crazy user error! sharth's code helped me figure it out. Here is the code I was actually using -- see if you can spot the error:
啊! 疯狂的用户错误!sharth 的代码帮助我弄清楚了。这是我实际使用的代码——看看你是否能发现错误:
std::cout << "rc == " << libusb_get_device_descriptor(dev, &desc) << std::endl
<< "vendor == " << desc.idVendor << std::endl;
I guess the way the compiler evaluates this, it is free to evaluate desc.idVendor
before the call to libusb_get_device_descriptor()
has actually been made. My bad.
我猜编译器评估这个的方式,desc.idVendor
在实际调用之前可以自由评估libusb_get_device_descriptor()
。我的错。
回答by Bill Lynch
You didn't include a full, compilable test case. So I built one. This works for me on CentOS 6 x64. I'm also running this as a normal user account.
您没有包含完整的、可编译的测试用例。所以我建了一个。这在 CentOS 6 x64 上对我有用。我也以普通用户帐户的身份运行它。
Source
来源
#include <cassert>
#include <cstdio>
#include <libusb-1.0/libusb.h>
int main() {
libusb_context *context = NULL;
libusb_device **list = NULL;
int rc = 0;
ssize_t count = 0;
rc = libusb_init(&context);
assert(rc == 0);
count = libusb_get_device_list(context, &list);
assert(count > 0);
for (size_t idx = 0; idx < count; ++idx) {
libusb_device *device = list[idx];
libusb_device_descriptor desc = {0};
rc = libusb_get_device_descriptor(device, &desc);
assert(rc == 0);
printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);
}
libusb_free_device_list(list, count);
libusb_exit(context);
}
Output
输出
Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0002
Vendor:Device = 8087:0020
Vendor:Device = 8087:0020
Vendor:Device = 0424:2514
Vendor:Device = 10c4:ea60
Vendor:Device = 051d:0002
Vendor:Device = 0624:0248
回答by lexXxel
This is not an standalone answer, it's more a comment to Bill Lynch's post. I'm not able to add a comment to Bill Lynch's post (missing reputation) so I decided to do it this way ;-).
这不是一个独立的答案,它更像是对比尔林奇帖子的评论。我无法在 Bill Lynch 的帖子中添加评论(缺少声誉),所以我决定这样做 ;-)。
There's a little detail missing in the code above:
上面的代码中缺少一些细节:
you have to free the list you got filled from libusb_get_device_list by hand.
您必须手动释放从 libusb_get_device_list 填充的列表。
From the libusb docu:
来自libusb 文档:
You are expected to unreference all the devices when you are done with them, and then free the list with libusb_free_device_list(). Note that libusb_free_device_list() can unref all the devices for you. Be careful not to unreference a device you are about to open until after you have opened it.
完成后,您应该取消引用所有设备,然后使用 libusb_free_device_list() 释放列表。请注意,libusb_free_device_list() 可以为您取消引用所有设备。小心不要取消引用您将要打开的设备,直到您打开它。
回答by Goddard
Just to add to Bill's answer, to avoid the multiple warnings you will likely recieve from this line
只是为了添加比尔的答案,以避免您可能会从这一行收到多个警告
libusb_device_descriptor desc = {0};
Simply remove the assignment.
只需删除分配。
libusb_device_descriptor desc;
Not a huge deal, but these things bother me.
没什么大不了的,但这些事情困扰着我。