Wednesday, April 27, 2011

Python and USB HID Device

(This is my post to pyusb mailing list on 27-April-2011)

Just a summary for the situation.

Firstly you may want to see if you really want to use
a generic HID device, in most cases, you can use
a custom device and then use libusb0.sys or winusb.sys
as the driver under Windows and then use pyusb with it.

Using a custom device will also make it possible to use
pyusb under Mac OS X. Recent Mac OS X makes it
very difficult (or impossible) to detach the kernel HID driver.
In that case, it is not possible to use libusb (0.1 or 1.0)
and thus pyusb with the device.

So if you really want to use pyusb and care about
cross-platfrom, then you should forget about generic
HID device and use a custom device instead.

If you really need to use HID device, there are a few
options.

1) If you only cares about Linux, then you can use
pyusb with no issues. libusb under Linux can detach
the kernel HID driver.

2) If you only care about Windows, you may want
to look at pywinusb.
http://code.google.com/p/pywinusb/

3) If you really want to use pyusb under Windows with the
HID device, you can use libusb-win32 filter driver for
that particular HID device. Please use the latest
libusb-win32 for this purpose. And take note this is
not a recommended solution.

4) If you want to have cross-platform support for the
HID device, then you need to look at HIDAPI and
use the python binding for it. Take note the python
binding for HIDAPI is not mature yet.
http://libusb.6.n5.nabble.com/Opinion-HID-and-Windows-back-end-td3716872.html
http://comments.gmane.org/gmane.comp.python.pyusb.user/749

5) Another option under Windows is to use the HID branch
of the libusb-pbatard git repo or older version of libusb-pbatard
(up to pbr332). This is not recommended as the official stand
of libusb-1.0 admin is not to support HID native backend under
Windows.

2 comments:

Андрей said...

Really informative summary for those just starting out with USB. Thanks!

I would greatly appreciate, if you would give some comments on the situation now, when libusbx has appeared. Is option 5) really feasible now?

At home on my Linux box I used option 1) - detaching the driver and then accessing the device through libusb-1.0.

But when I began to read on about Windows (which I have to use at work), I found out that situation is not that simple as with Linux.

I still would better prefer "driverless" HID variant 5). But the big question is: is it really ready for use in libusbx (through PyUSB)?

Xiaofan said...

Yes you can use option 5 now that libusbx is released.