如何在Windows操作系统使用libusb访问USB设备
2017-08-09 13:44:15 来源:华启智能
简介
英文原文地址:https://github.com/libusb/libusb/wiki
这个项目为开源的USB库 libusb 添加了对Windows 操作系统的支持, libusb 帮助开发者很容易的在Windows操作系统访问USB设备。现在支持使用WinUSB 和HID 驱动程序的通过USB设备,也支持使用libusb-win32 和 libusbK 驱动程序的USB设备。
请注意 libusb-win32 和 libusbK 是不同的项目. libusb-win32 仅支持Windows,提供了和libusb-0.1 API 兼容的库和内核驱动libusb0.sys。libusbK 是一个Windows 项目,为Windows和内核驱动程序libusbK.sys提供了一套新的API 。
二进制文件
在 Sourceforge files 提供了预编译的二进制文件和相关的源代码. 对于新的版本1.0.21 release, 也可以从 github 下载。
Windows版本的预编译的二进制文件是使用下面的工具生成的:
- Microsoft Visual Studio and DDK/WDK → MS32(32 bit) and MS64 (64 bit) directories
- MinGW -> MinGW32 (32 bit) and MinGW64 (64 bit) directories
支持的操作系统
支持从Windows XP开始的所有的Windows操作系统,包括32位和64位操作系统。以下几种情况是例外:
- Windows 2003 (Windows 2003 32bit/64bit 不支持WinUSB )
- Windows XP 64 bit (Windows XP 64bit 系统微软官方不支持WinUSB )
支持USB 3.0
libusb 支持Windows操作系统下的 USB 3.0 控制器和设备。 供应商为Windows 7 以及以前的版本,和为Windows 8提供的Microsoft controller xHCI 驱动也是支持的。如果你不是使用Windows 8 或者更新的操作系统, 你需要使用厂家提供的驱动程序. 请确保你使用了最新版的驱动程序。
如果你使用了NEC/Renesas USB 3.0 控制器, 例如uPD720200
/uPD720200A
, 请确保控制的驱动程序版本是 version 2.1.16.0 or later. 老版本的驱动程序有BUG。
支持Windows 8.x/10 (包括32位和64位)
在 Windows 8 and Windows 8.1(包括 32位 和 64位)操作系统,libusb 都可以正常工作。.
支持.NET开发
LibUsbDotNet 是.NET
版本的libusb, 基于 libusb 1.0, 由 Travis Lee Robinson开发。如果你计划在.NET项目中使用libusb,你可以访问http://libusbdotnet.sourceforge.net 或者 http://sourceforge.net/projects/libusbdotnet/, 请注意 LibUsbDotNet'最新版本是 v2.2.8, 2010-10-29发布。
Travis Lee Robinson 页创建了 libusbK 项目, C# 封装了"libusbK.dll"。最新版本(v3.0.7, 2014-04-27) 二进制文件, 源代码和C#封装类文件可以从 its SourceForge directory下载.
如何在Windows操作系统使用libusb
安装USB驱动程序
如果你的目标设备不是类似鼠标、键盘的HID设备,在使用libusb之前,必须安装驱动程序。现在可以安装微软的 Microsoft's WinUSB
, libusb-win32 或者 libusbK 驱动. 有两个选择:
- 强烈推荐: 使用最新版本的 Zadig, 一个图形化的软件,可以自动安装
WinUSB、
libusb-win32
和libusbK
.。 - 也可以直接选择选择
WinUSB
, 你可以下载 WinUSB driver files,并且为你的设备编辑inf
文件。.
1.0.21版本或者更新的版本 , 你可以使用usbdk 。 usbdk 提供了另外一种驱动。
开发者需要考虑的内容
The handling of composite devices under Windows is done with multiple drivers, that are children of the usbccgp.sys
driver (Composite Generic Parent), as this is the default for the OS. For more info, see "2. How Windows handles a composite device" at http://forum.silabs.com/ubbthreads.php?ubb=showflat&Number=39797. If needed, it is also possible to replace the composite parent driver to access the device.
Because Windows does not provide a native poll()
function, and cygwin is the only development environment that provides such a call, the use of libusb file descriptors with poll()
on cygwin isNOT supported. In a future version of libusb, we should provide better handling of native Windows events, but this will require a redesign of the libusb API, so it probably won't occur before libusb 2.0.
已知的一些限制
WinUSB
不能发送真正的复位命令给USB设备。 这是WinUSB的限制。WinUSB
和libusbK
不能设置USB设备的配置文件。 different from the first one.. 这是KMDF USB I/O Target的限制。.WinUSB
不支持 多个程序并发访问 (as per the MSDN documentation).WinUSB
在 Windows XP/Vista/7/8不支持同步传输. WinUSB 在 Windows 8.1 或者更新的版本支持同步传输。libusb Windows 支持使用usbdk backend (version 1.0.21 and later)实现同步传输.- With
WinUSB
, whenLIBUSB_RECIPIENT_INTERFACE
is used for the transfer, theWinUSB
DLL forces the low byte ofwIndex
to the interface number, regardless of what you set it to.
- HID keyboards and mice cannot be accessed using the native HID driver as Windows reserves exclusive access to them.
- Multiple HID top level collections are currently not supported (only the first top level collection will be used).
- The HID Report Descriptors provided by libusb are recomposed and may present minor differences from the actual ones, as the Windows HID API does not allow to read them directly from the device.
- Because there is no native
poll()
on Windows, the ability to return externally pollable file descriptors on Windowslibusb_get_pollfd()
returns an error. - If you use a composite device, and plan to install a libusb compatible driver for any of the interfaces, you should ensure that your driver package adds a Device Interface GUID in the registry, as proper enumeration of composite devices in libusb depends on it. This is typically achieved by adding something like the following in your inf:
HKR,,DeviceInterfaceGUIDs,0x00010000,{12345678-1234-1234-1234-123456789ABC}
This is in particular a problem with libusb-win32's inf-wizard which will be deprecated by libusb-win32 project. Please use Zadig instead. libusb0.sys and libusbk.sys
access is done through the libusbK DLL, therefore, if you plan to use the libusb-win32/libusb0.sys or libusbK/libusbk.sys driver in libusb, you must have that library installed. If using a recent version of Zadig, you should not have to do anything, at it will install the library for you. However the support of libusb0.sys is not ready, therefore you should not use libusb-win32/libusb0.sys for now.
开发者使用的一些有用链接
- How to Use WinUSB to Communicate with a USB Device & WinUSB (Winusb.sys) Installation.
Note that the inf file given in the howto has a typo. If you don't changeSourceDisksFiles.NTamd64
toSourceDisksFiles.amd64
, the driver installation will fail to copy the required DLLs on 64 bit systems... - Using WinUSB for User-Mode to USB Device Communication
- WinUSB User-Mode Client Support Routines
- Microsoft's USB Core Team Blog (including resources on instrumenting the USB stack in Windows 7)
- additional information about Windows Co-Installers
- Finding Memory Leaks Using the CRT Library
- libusbK documentation
- libwdi/Zadig Wiki