apmorton / pyhidapi

hidapi bindings in ctypes
MIT License
111 stars 42 forks source link

Unable to open device #19

Closed Qbicz closed 5 years ago

Qbicz commented 5 years ago

Hi, I'm trying to use pyhidapi to send feature reports. This is a device normally claimed by the Windows operating system (HID mouse). I needed to apply that patch to the HIDAPI C library: https://github.com/signal11/hidapi/pull/335 With it, I can open the device with C code and send feature reports, however with pyhidapi I can enumerate devices, but trying to create a hid.Device(vid, pid) results in

  File "C:\Python27\lib\site-packages\hid-0.1.1-py2.7.egg\hid\__init__.py", line 91, in __init__
    raise HIDException('unable to open device')
hid.HIDException: unable to open device

I use the DLL with the mentioned patch. Any ideas?

apmorton commented 5 years ago

The version on pypi is outdated - it doesn't include the fixes for correct operation on Windows, which I finally merged today. Can you try with the code from master and see if that works?

Qbicz commented 5 years ago

I use the git master version with merged #12 which is functionally the same as #13. The problem is not that I cannot locate the DLL, but that the DLL does not work the same in Python as it does when used by C code. I'll check my hidapi.dll twice, maybe there's something wrong with it.

apmorton commented 5 years ago

Was guessing based on the file path in your error message - I guess you installed it system wide from a git clone.

Double check that the correct hidapi.dll is actually being loaded, you may have a stray version of hidapi.dll somewhere on your path that is higher in the search order.

apmorton commented 5 years ago

Also, fwiw, prior to #12/#13 it will still locate and load hidapi.dll on Windows, it just uses the incorrect calling convention and nothing works.

Qbicz commented 5 years ago

I have Python2.7 [MSC v.1500 32 bit (Intel)] on win32 so I compiled the hidapi.dll with Visual Studio 2008. However, it didn't work with opening the device. When I compiled it with Visual Studio 2017, it works!

Since we are at it, there is no conversion of arrays from Python to C for calls like write() or send_feature_report(). Should I just import ctypes and do something like

arr = (ctypes.c_int * len(pyarr))(*pyarr)
apmorton commented 5 years ago

You are expected to pass strings for calls to write or send_feature_report. If you have some structured report you need to build up, the struct module that ships with python is your friend.

Qbicz commented 5 years ago

I just want to pass an array of bytes. When passing a python string, I get hid.HIDException: <exception str() failed>. Ended up with ctypes.c_ubyte which works fine for sending reports.

Thanks for help!