C++ 如何最正确地使用 libusb 与连接的 USB 设备通话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17239565/
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 most properly use libusb to talk to connected USB devices?
提问by user473973
How do I most properly use libusbto talk to connected USB devices?
我如何最正确地使用libusb与连接的 USB 设备通信?
Specifically, how do I transfer data to the USB devices, receive information from the devices, find out the name of the connected device, if they have storage, etc.
具体来说,我如何将数据传输到 USB 设备、从设备接收信息、找出所连接设备的名称、它们是否有存储等。
More specifically, I will be running this on a Mac OS X machine, so I know I can't just use Windows header files.
更具体地说,我将在 Mac OS X 机器上运行它,所以我知道我不能只使用 Windows 头文件。
If there is a good explanation on libusb and USB devices, that would be helpful too.
如果对 libusb 和 USB 设备有很好的解释,那也会有帮助。
采纳答案by user2469202
I have found here a textual howto about using libusb-1.0: http://www.dreamincode.net/forums/topic/148707-introduction-to-using-libusb-10/
我在这里找到了关于使用 libusb-1.0 的文字说明:http: //www.dreamincode.net/forums/topic/148707-introduction-to-using-libusb-10/
回答by eatonphil
Hereis a post on a similar question that might be useful to you. I include plenty of links.
这是关于类似问题的帖子,可能对您有用。我包括了很多链接。
But maybe you'd rather see it here. So in that case, here it goes!
但也许你更愿意在这里看到它。所以在这种情况下,就这样吧!
Libusb allows you to enumerate devices and select the one you want based on a specific Vendor/Product id (V/P Id). If you don't know this and can't find it online with the product's description then you can easily find it.
Libusb 允许您枚举设备并根据特定的供应商/产品 ID (V/P Id) 选择您想要的设备。如果您不知道这一点并且无法通过产品说明在线找到它,那么您可以轻松找到它。
If it is not online you will need to use an app similar to lsusb on Linux. (I don't believe it is on Mac.) When you run lsusb it lists connected devices and their V/P Ids. You can easily find your device by unplugging, running lsusb, and plugging the device back in and comparing. It's a piece of cake. Any usb list app on Mac will hopefully display the V/P ID like lsusb does.
如果它不在线,您将需要在 Linux 上使用类似于 lsusb 的应用程序。(我不相信它在 Mac 上。)当您运行 lsusb 时,它会列出连接的设备及其 V/P Id。您可以通过拔下、运行 lsusb 并重新插入设备并进行比较来轻松找到您的设备。很简单的。Mac 上的任何 USB 列表应用程序都有望像 lsusb 一样显示 V/P ID。
Then once you have this V/P ID you will use libusb (if using 0.1) to enumerate all devices and find the device that matches that id. (I support using libusbx which happens to have a single find device function based on V/P id - in fact, libusbx is a whole lot more concise all around.)
然后一旦你有了这个 V/P ID,你将使用 libusb(如果使用 0.1)来枚举所有设备并找到与该 ID 匹配的设备。(我支持使用 libusbx,它碰巧有一个基于 V/P id 的查找设备功能——事实上,libusbx 在各方面都更加简洁。)
After selecting your device you will send a packet using either Feature or Output Reports. This is the most complicated part because the packet you send is dependent on the individual device I believe. It is 8 bytes of data and only one of which is a single character you wish to send to the usb device. (If you wanted to send 8 characters, you would have to loop through an array of chars and send a feature or output report for each character.)
选择您的设备后,您将使用功能或输出报告发送数据包。这是最复杂的部分,因为您发送的数据包取决于我认为的单个设备。它是 8 个字节的数据,其中只有一个是您希望发送到 USB 设备的单个字符。(如果您想发送 8 个字符,则必须遍历一个字符数组并为每个字符发送一个特征或输出报告。)
As an example feel free to reference a rather specific terminal exampleI wrote for controlling two LEDS. If it's helpful, great! It contains a libusbx and libusb-0.1 example.
作为示例,请随意参考我为控制两个 LED 编写的相当具体的终端示例。如果有帮助,那就太好了!它包含一个 libusbx 和 libusb-0.1 示例。
I hope this helps!
我希望这有帮助!
回答by freeworld
The process that you can follow is:
您可以遵循的过程是:
- Get the VID, PID for the device that you want to communicate using
lsusb
- Try to open the device and read the device descriptor
- If you want name of the device use string descriptor to get that
- Check if any kernel driver is attached. If it is, then detach it and do some raw data transfer
- After getting the response again re-attach the driver.
- 获取要使用的设备的 VID、PID
lsusb
- 尝试打开设备并读取设备描述符
- 如果您想要设备名称,请使用字符串描述符来获取
- 检查是否附加了任何内核驱动程序。如果是,则将其分离并进行一些原始数据传输
- 再次收到响应后,重新连接驱动程序。
回答by studgeek
The official site for libusb 1.0 (the newer and recommended version) is https://libusb.info/. The API documentation is at http://api.libusb.info. Click on the Modules section to walk through the different function areas. The source is at https://github.com/libusb/libusband you can see some working examples at https://github.com/libusb/libusb/tree/master/examples. Hope that helps!
libusb 1.0(较新的推荐版本)的官方站点是https://libusb.info/。API 文档位于http://api.libusb.info。单击模块部分以浏览不同的功能区域。该人士是在https://github.com/libusb/libusb,你可以看到一些工作示例https://github.com/libusb/libusb/tree/master/examples。希望有帮助!
The article from @user2469202 is a good basic intro also.
来自@user2469202 的文章也是一个很好的基本介绍。