Issue 59 in pywws: OSX support with hidapi

478 views
Skip to first unread message

py...@googlecode.com

unread,
Oct 28, 2011, 7:50:45 AM10/28/11
to py...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 59 by jarkko.s...@gmail.com: OSX support with hidapi
http://code.google.com/p/pywws/issues/detail?id=59

With OS X the libusb wil not work with weather station , because the device
is claimed by generic hid driver. It is possible to prevent this by writing
an empty device driver for weather station, but this is rumoured not to
work anymore.

OS X contains libraries to access hid usb devices, but the API is Apple
specific. I suggest using a wrapper library called 'hidapi'
(https://github.com/signal11/hidapi/downloads) and a python wrapper to call
the library. There may be alternatives, but I found the hidapi library
simple to use and sufficient for this purpose. Hidapi should work with
Linux and Windows too, but it did try it.

To use hidapi I suggest separating the usb code from WeatherStation.py . I
attached the version I am using.

Attachments:
WeatherStation.py 24.2 KB
DevicePyUsb.py 2.3 KB
DeviceHidapi.py 1.1 KB
hidapi.py 11.7 KB

py...@googlecode.com

unread,
Oct 28, 2011, 8:36:30 AM10/28/11
to py...@googlegroups.com
Updates:
Labels: -Type-Defect Type-Enhancement

Comment #1 on issue 59 by a...@jim-easterbrook.me.uk: OSX support with
hidapi
http://code.google.com/p/pywws/issues/detail?id=59

I'm in the process of rewriting WeatherStation.py at the moment, and have
already split off the USB code into its own class. Putting this class in a
separate module makes sense, but I can't promise to use your api.

'hidapi' is not available for the version of OpenWRT that's running on the
router I use with my weather station, so I won't be able to test any module
that uses it.

py...@googlecode.com

unread,
Oct 28, 2011, 2:19:05 PM10/28/11
to py...@googlegroups.com

Comment #2 on issue 59 by jarkko.s...@gmail.com: OSX support with hidapi
http://code.google.com/p/pywws/issues/detail?id=59

Just for the record: the hidapi wrapper is from this discussion
http://comments.gmane.org/gmane.comp.python.pyusb.user/749

py...@googlecode.com

unread,
Jan 13, 2012, 4:51:45 AM1/13/12
to py...@googlegroups.com

Comment #3 on issue 59 by huangyua...@gmail.com: OSX support with hidapi
http://code.google.com/p/pywws/issues/detail?id=59

Hello, can you send me the hidapi.dylib on mac.

My email address is huang...@gmail.com.

Thanks.

py...@googlecode.com

unread,
Jan 13, 2012, 4:57:52 AM1/13/12
to py...@googlegroups.com

Comment #4 on issue 59 by a...@jim-easterbrook.me.uk: OSX support with
hidapi
http://code.google.com/p/pywws/issues/detail?id=59

I assume you get hidapi files from
https://github.com/signal11/hidapi/downloads.

py...@googlecode.com

unread,
Jan 26, 2012, 2:35:10 PM1/26/12
to py...@googlegroups.com
Updates:
Status: Started

Comment #5 on issue 59 by a...@jim-easterbrook.me.uk: OSX support with
hidapi
http://code.google.com/p/pywws/issues/detail?id=59

I'm pleased to report that I've made some progress this week.

First I split off the lowest level USB interface code into its own Python
module (device_libusb.py), then created a second module (device_hidapi)
with a compatible USBDevice class.

Secondly I was able to compile hidapi on my Ubuntu machine and install it
as a shared library which can be imported by the ctypes based hidapi.py
wrapper. It uses libusb-1.0, unlike pyusb which uses libusb-0.1.

Lastly, it all seems to work!

I'll check in the new files in a few minutes, and then I need to write some
documentation. Leading novice users through the compilation and
installation of hidapi is not going to be easy.

py...@googlecode.com

unread,
Jan 27, 2012, 4:50:31 AM1/27/12
to py...@googlegroups.com

Comment #6 on issue 59 by a...@jim-easterbrook.me.uk: OSX support with
hidapi
http://code.google.com/p/pywws/issues/detail?id=59

I've found another Python interface to hidapi that is even simpler to use.
It claims to work on Linux, Windows and Mac. I've got it working with pywws
on Linux. Could a Mac owner try it for me?

1/ Get cython-hidapi from its git repository:
git clone http://github.com/gbishop/cython-hidapi.git

2/ Compile it:
cd cython-hidapi
python setup-mac.py build

3/ Install it:
sudo setup-mac.py install

4/ Test it:
python try.py

Mark

unread,
Jan 27, 2012, 5:04:28 AM1/27/12
to py...@googlegroups.com
Hmmm, I get the following:

Traceback (most recent call last):
File "setup-mac.py", line 3, in <module>
from Cython.Distutils import build_ext
ImportError: No module named Cython.Distutils


Clearly there is no module called Cython.Distutils, so maybe I am suppose
to install something else first. Cython?

Mark

>--
>You received this message because you are subscribed to the Google Groups
>"pywws" group.
>To post to this group, send email to py...@googlegroups.com.
>To unsubscribe from this group, send email to
>pywws+un...@googlegroups.com.
>For more options, visit this group at
>http://groups.google.com/group/pywws?hl=en.
>


Jim Easterbrook

unread,
Jan 27, 2012, 5:16:07 AM1/27/12
to py...@googlegroups.com
On 27/01/12 10:04, Mark wrote:
> Hmmm, I get the following:
>
> Traceback (most recent call last):
> File "setup-mac.py", line 3, in<module>
> from Cython.Distutils import build_ext
> ImportError: No module named Cython.Distutils
>
>
> Clearly there is no module called Cython.Distutils, so maybe I am suppose
> to install something else first. Cython?

Yes, I hope it's in the Mac equivalent of a standard repository.
--
Jim Easterbrook <http://www.jim-easterbrook.me.uk/>

Mark

unread,
Jan 27, 2012, 5:42:58 AM1/27/12
to py...@googlegroups.com
Okay, so I installed Cython (Python is not my strong point, but it seemed to install okay.)

After the "sudo python setup-mac.py build" command I got, which does not look good.
running build
running build_ext
cythoning hid.pyx to hid.c
building 'hid' extension
creating build
creating build/temp.macosx-10.7-intel-2.7
llvm-gcc-4.2 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -framework IOKit -framework CoreFoundation -arch i386 -arch x86_64 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c hid.c -o build/temp.macosx-10.7-intel-2.7/hid.o
i686-apple-darwin11-llvm-gcc-4.2: -framework: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: IOKit: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: -framework: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: CoreFoundation: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: -framework: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: IOKit: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: -framework: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: CoreFoundation: linker input file unused because linking not done
llvm-gcc-4.2 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -framework IOKit -framework CoreFoundation -arch i386 -arch x86_64 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c hid-mac.c -o build/temp.macosx-10.7-intel-2.7/hid-mac.o
i686-apple-darwin11-llvm-gcc-4.2: -framework: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: IOKit: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: -framework: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: CoreFoundation: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: -framework: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: IOKit: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: -framework: linker input file unused because linking not done
i686-apple-darwin11-llvm-gcc-4.2: CoreFoundation: linker input file unused because linking not done
creating build/lib.macosx-10.7-intel-2.7
llvm-gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup -Wl,-F. -arch i386 -arch x86_64 -framework IOKit -framework CoreFoundation build/temp.macosx-10.7-intel-2.7/hid.o build/temp.macosx-10.7-intel-2.7/hid-mac.o -o build/lib.macosx-10.7-intel-2.7/hid.so

But I ran "sudo python setup-mac.py install" anyway and got, 
running install
running build
running build_ext
skipping 'hid.c' Cython extension (up-to-date)
running install_lib
copying build/lib.macosx-10.7-intel-2.7/hid.so -> /Library/Python/2.7/site-packages
running install_egg_info
Writing /Library/Python/2.7/site-packages/UNKNOWN-0.0.0-py2.7.egg-info


Which looked okay, to me.  So then I ran "python try.py" and got:
[{'usage_page': 1, 'product_id': 780, 'manufacturer_string': u'A\x00p\x00p', 'vendor_id': 1452, 'release_number': 512, 'serial_number': u'0\x000\x00-\x001\x004\x00-\x005\x001\x00-', 'usage': 2, 'path': 'Bluetooth_05ac_030c_0x7fc492602580', 'product_string': u'm\x00a\x00r\x00k\x00e\x00t\x00a\x00y\x00l\x00o'}, {'usage_page': 12, 'product_id': 33346, 'manufacturer_string': u'A\x00p\x00p\x00l\x00e\x00 \x00C\x00o\x00m\x00p\x00', 'vendor_id': 1452, 'release_number': 22, 'serial_number': u'', 'usage': 1, 'path': 'USB_05ac_8242_0x7fc492600790', 'product_string': u'A\x00p\x00p\x00l\x00'}, {'usage_page': 1, 'product_id': 782, 'manufacturer_string': u'A\x00p\x00p', 'vendor_id': 1452, 'release_number': 352, 'serial_number': u'd\x008\x00-\x00a\x002\x00-\x005\x00e\x00-', 'usage': 2, 'path': 'Bluetooth_05ac_030e_0x7fc492601340', 'product_string': u'A\x00p\x00p\x00l\x00e\x00 \x00W\x00i\x00r\x00e\x00l\x00e'}, {'usage_page': 1, 'product_id': 570, 'manufacturer_string': u'A\x00p\x00p', 'vendor_id': 1452, 'release_number': 80, 'serial_number': u'e\x008\x00-\x000\x006\x00-\x008\x008\x00-', 'usage': 6, 'path': 'Bluetooth_05ac_023a_0x7fc492600ba0', 'product_string': u'm\x00a\x00r\x00k\x00e\x00t\x00a\x00y\x00l\x00o\x00r\x00'}]
Traceback (most recent call last):
File "try.py", line 6, in <module>
h = hid.device(0x461, 0x20)
File "hid.pyx", line 42, in hid.device.__cinit__ (hid.c:957)
raise IOError('open failed')
IOError: open failed


Which does not look so good.

Mark


On 27/01/2012 09:50, "py...@googlecode.com" <py...@googlecode.com> wrote:

Jim Easterbrook

unread,
Jan 27, 2012, 7:16:08 AM1/27/12
to py...@googlegroups.com
On 27/01/12 10:42, Mark wrote:
> Okay, so I installed Cython (Python is not my strong point, but it
> seemed to install okay.)
>
> After the "sudo python setup-mac.py build" command I got, which does not
> look good.

You shouldn't need 'sudo' when building the Python extension, just when
installing it.

I think those are warnings rather than errors.

Nope, this is exactly what I'd expect to see. I don't know what device
the test script is trying to open, but it wouldn't have got that far if
the extension hadn't installed OK.

This looks very promising. I'll check into pywws svn a module that uses
it to access the weather station.

Mark

unread,
Jan 27, 2012, 7:27:46 AM1/27/12
to py...@googlegroups.com
Okay, great glad to help.

One small question.  My weather station has been running non-stop for 400 odd days now (give or take a few server reboots and a set of batteries). Clearly I am not really bothered by some of the issues discussed in this forum.

Will the changes you plan to make be compulsory?  In other words if I upgrade in the future will I be expected to use this new form of USB interface driver?

Mark

Jim Easterbrook

unread,
Jan 27, 2012, 7:42:20 AM1/27/12
to py...@googlegroups.com
On 27/01/12 12:27, Mark wrote:
>
> One small question. My weather station has been running non-stop for 400
> odd days now (give or take a few server reboots and a set of batteries).
> Clearly I am not really bothered by some of the issues discussed in this
> forum.

Hooray! Touch wood.

> Will the changes you plan to make be compulsory? In other words if I
> upgrade in the future will I be expected to use this new form of USB
> interface driver?

No, my plan is to support at least two - the original pyusb (which is
the only one available on my OpenWRT router, without a lot of extra
work) and an hidapi based one. I'm hoping this cython-hidapi interface
will be easy enough to install and work for enough people that I don't
need to support the hidapi.py ctypes based interface. pywws should
automatically use whichever is installed.

py...@googlecode.com

unread,
Jan 28, 2012, 6:14:44 AM1/28/12
to py...@googlegroups.com

Comment #7 on issue 59 by jarkko.s...@gmail.com: OSX support with hidapi
http://code.google.com/p/pywws/issues/detail?id=59

Cython is missing by default (at least in OS X 10.6.8):
$ python setup-mac.py build


Traceback (most recent call last):

File "setup-mac.py", line 3, in <module>
from Cython.Distutils import build_ext
ImportError: No module named Cython.Distutils

Install it from source or from Mac Ports:
$ sudo port install py26-cython

After that:
$ python setup-mac.py build


running build
running build_ext
skipping 'hid.c' Cython extension (up-to-date)

$ sudo python setup-mac.py install
Password:


running install
running build
running build_ext
skipping 'hid.c' Cython extension (up-to-date)
running install_lib

copying build/lib.macosx-10.6-i386-2.6/hid.so ->
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
running install_egg_info
Writing
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/UNKNOWN-0.0.0-py2.6.egg-info
$ python try.py
[{'usage_page': 1, 'product_id': 521, 'manufacturer_string':

u'A\x00p\x00p', 'vendor_id': 1452, 'release_number':

272, 'serial_number': ..., 'usage':
6, 'path': 'Bluetooth_05ac_0209_0x21ddf0', 'product_string':
u'v\x00i\x00e\x00r\x00a\x00s\x00\u2019\x00s\x00 '}, {'usage_page':
12, 'product_id': 33344, 'manufacturer_string': u'A\x00p\x00p\x00l\x00e\x00

\x00C\x00o\x00m\x00p\x00', 'vendor_id': 1452, 'release_number':

272, 'serial_number': u'', 'usage':
1, 'path': 'USB_05ac_8240_0x21e920', 'product_string':

u'A\x00p\x00p\x00l\x00'}, {'usage_page': 1, 'product_id':

45065, 'manufacturer_string': u'U\x00n\x00k\x00n', 'vendor_id':
1133, 'release_number': 1046, 'serial_number': ..., 'usage':
2, 'path': 'Bluetooth_046d_b009_0x21d2a0', 'product_string':
u'L\x00o\x00g\x00i\x00t\x00e\x00c\x00h\x00
\x00B\x00l\x00u\x00e\x00t\x00o\x00'}, {'usage_page': 65440, 'product_id':
32801, 'manufacturer_string': u'', 'vendor_id': 6465, 'release_number':
256, 'serial_number': u'', 'usage':
1, 'path': 'USB_1941_8021_0x21c670', 'product_string': u''}]


Traceback (most recent call last):
File "try.py", line 6, in <module>
h = hid.device(0x461, 0x20)

File "hid.pyx", line 42, in hid.device.__cinit__ (hid.c:951)


raise IOError('open failed')
IOError: open failed

I don't have the tried device, so I modified the try.pl:
$ diff try.py*
6,7c6
< #h = hid.device(0x461, 0x20)
< h = hid.device(0x1941, 0x8021)
---
> h = hid.device(0x461, 0x20)
13,20d11
< h.set_nonblocking(0)
<
< cmd = [0x00, 0xA1, 0, 0, 0x20, 0xA1, 0, 0, 0x20]
< h.write(cmd)
< for k in range(4):
< print h.read(8)
< print
<
$ python try.py
...

[85, 170, 255, 255, 255, 255, 255, 255]
[255, 255, 255, 255, 255, 255, 255, 255]
[15, 32, 1, 48, 17, 0, 0, 0]
[1, 0, 0, 240, 15, 0, 48, 166]

[85, 170, 255, 255, 255]
...


Note that to use HID devices in OS X, you must be the current console user
or root. So if you use cron or launchd job, you must be root. I don't know
if there is a special group or something for HID access.


py...@googlecode.com

unread,
Jan 28, 2012, 6:26:47 AM1/28/12
to py...@googlegroups.com

Comment #8 on issue 59 by a...@jim-easterbrook.me.uk: OSX support with
hidapi
http://code.google.com/p/pywws/issues/detail?id=59

Thanks for that. Would you be happy if I dropped support for hidapi.py
leaving a choice of two: pyusb or cython-hidapi?

I'd prefer to use something that's in a repository and is actively
maintained. I don't think hidapi.py meets those requirements.

py...@googlecode.com

unread,
Jan 28, 2012, 7:15:51 PM1/28/12
to py...@googlegroups.com

Comment #9 on issue 59 by sonnika...@gmail.com: OSX support with hidapi
http://code.google.com/p/pywws/issues/detail?id=59

That's ok with me.

py...@googlecode.com

unread,
Feb 1, 2012, 9:43:02 PM2/1/12
to py...@googlegroups.com

Comment #10 on issue 59 by clawsi...@gmail.com: OSX support with hidapi
http://code.google.com/p/pywws/issues/detail?id=59

I have a Mac and recently got a WH3081. I used the approach outlined above
which was to:
1. Download and install Cython.
2. Download and install cython-hidapi
3. Download latest pywws using svn checkout (not the older gtar from the
pywws downloads section)
4. Ran through the instructions on the pywws getting started page.

I confirm that the latest changes work for me on a Mac running OS X 10.7.2
and Python2.7.


py...@googlecode.com

unread,
Feb 18, 2012, 5:05:26 AM2/18/12
to py...@googlegroups.com
Updates:
Status: Fixed

Comment #11 on issue 59 by a...@jim-easterbrook.me.uk: OSX support with
hidapi
http://code.google.com/p/pywws/issues/detail?id=59

(No comment was entered for this change.)

Reply all
Reply to author
Forward
0 new messages