C++ 在 Windows 上与 USB HID 设备通信的最佳 USB 库是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/280199/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:18:29  来源:igfitidea点击:

What is the best usb library to communicate with usb HID devices on Windows?

c++windowsusbhid

提问by Torleif

The library should;

图书馆应该;

-Be easy to use and few lines of client code should accomplish much
-Be as platform independent as possible. (In case of future ports to other platforms)
-Have C++ bindings.
-Be mature and stable

- 易于使用,几行客户端代码就可以完成很多工作
- 尽可能独立于平台。(如果将来
移植到其他平台) -有 C++ 绑定。
-成熟稳定

I would also like to be notified of most HID events through callbacks.

我还想通过回调收到大多数 HID 事件的通知。

I have considered the following alternatives:

我考虑了以下替代方案:

*libhid - (Unfortunately?) this is GPL and cannot be used in my application.
*WDK - Seems to be a bit low-level for my use. I don`t need that kind of control
*atusbhid - This has an appropriate level of abstraction but is firmly tied to the windows messaging loop

*libhid -(不幸的是?)这是 GPL,不能在我的应用程序中使用。
*WDK - 对我来说似乎有点低级。我不需要那种控制
*atusbhid - 它具有适当的抽象级别,但与 Windows 消息传递循环紧密相关

Do any of you have any other alternatives to offer?

你们中的任何人有其他选择吗?

回答by Femi

Take a look at hidapi: it is C, which answers the C++ bindings question (effectively :)), is cross platform and has a very permissive license. Doesn't appear to have the callbacks, but...

看看hidapi:它是 C,它回答了 C++ 绑定问题(有效地:)),是跨平台的,并且拥有非常宽松的许可证。似乎没有回调,但是......

回答by Michael Kohne

If libhid works for you, then perhaps the thing to do would be to write an app (which you would GPL), which uses libhid to talk to devices, then provides whatever you need via a TCP connection. Your real app would connect via TCP to do what it needs. This would obviously be a massive performance hit.

如果 libhid 适合您,那么也许要做的事情是编写一个应用程序(您将使用 GPL),它使用 libhid 与设备通信,然后通过 TCP 连接提供您需要的任何内容。您的真实应用程序将通过 TCP 连接以执行所需的操作。这显然会是一个巨大的性能损失。

This app would effectively be a 'shim' between libhid and your app. In this case, the shim would exist for legal, not technical reasons.

这个应用程序实际上是 libhid 和您的应用程序之间的“垫片”。在这种情况下,垫片将出于法律而非技术原因而存在。

I'm not saying it's a good idea, just that it's an idea.

我不是说这是个好主意,只是说这是一个主意。

回答by Michael Kohne

HIDmaker software suite from Trace systems is an option.

来自 Trace 系统的 HIDmaker 软件套件是一个选项。

Pros: - Easy to use (Excelent for learning how to program for USB HID) - Generates working applications source code in a various project formats (Visual Studio, Borland) - Generates stable example code for both host and device (stable in my experience) - High performance (if HID can even be said to have high performance in the first place)

优点: - 易于使用(非常适合学习如何为 USB HID 编程) - 以各种项目格式(Visual Studio、Borland)生成工作应用程序源代码 - 为主机和设备生成稳定的示例代码(在我的经验中稳定) - 高性能(如果 HID 甚至可以说首先具有高性能)

Cons: - Only works on Microsoft Windows - Uses it's own USB library I think (ActiveX)

缺点: - 仅适用于 Microsoft Windows - 我认为使用它自己的 USB 库(ActiveX)

回答by Dave Van den Eynde

Consider rolling your own. You'll have total control over the interface, the level of platform independence, and such. Even though a project is GPL, you can use it as a recipe for your own, and as a testbed to find issues with your own.

考虑自己滚动。您将完全控制界面、平台独立级别等。即使项目是 GPL,您也可以将其用作您自己的配方,并用作查找您自己的问题的测试平台。

回答by Coderer

Look at this code:

看看这段代码:

http://geekswithblogs.net/tamir/archive/2008/12/30/read-and-use-fm-radio-or-any-other-usb-hid.aspx

http://geekswithblogs.net/tamir/archive/2008/12/30/read-and-use-fm-radio-or-any-other-usb-hid.aspx

It gives you some simple classes to talk to a HID device. What it boils down to is, get the alias for the device (something like \?\HID#Vid_nnnn&Pid_nnn#.......) and use CreateFile to open it. You can get the device's alias under HKML\SYSTEM\CCS\Control\DeviceClasses{4d1e55...}\

它为您提供了一些与 HID 设备通信的简单类。它归结为,获取设备的别名(类似于 \?\HID#Vid_nnnn&Pid_nnn#......)并使用 CreateFile 打开它。您可以在 HKML\SYSTEM\CCS\Control\DeviceClasses{4d1e55...}\ 下获取设备的别名

The Vid and Pid are the vendor ID and product ID of the device (check Device Manager).

Vid 和 Pid 是设备的供应商 ID 和产品 ID(检查设备管理器)。

回答by Potter

There are several USB HID Host drivers for Windows. An easy to use Dynamic Link Library is from http://embedded24.net. There are also several example applications included for Visual Studio 2010 (C++, C# and VB).

有几个适用于 Windows 的 USB HID 主机驱动程序。一个易于使用的动态链接库来自http://embedded24.net。还有几个用于 Visual Studio 2010(C++、C# 和 VB)的示例应用程序。