apmorton / pyhidapi

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

ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type #39

Open mikics opened 4 years ago

mikics commented 4 years ago

Hi, I'm trying to use pyhidapi for controlling a Digital Micromirror Device. I'm trying to send to the device its stop command, but I receive the following error:

CRITICAL:root:Unhandled exception:Traceback (most recent call last): File "C:\Users\OPT\anaconda3\envs\ScopeFoundry\lib\site-packages\ScopeFoundry\hardware.py", line 208, in enable_connection raise err File "C:\Users\OPT\anaconda3\envs\ScopeFoundry\lib\site-packages\ScopeFoundry\hardware.py", line 204, in enable_connection self.connect() File "D:\LabPrograms\ScopeFoundry_POLIMI\DMD_ScopeFoundry\DMDHardware.py", line 57, in connect self.dmd.stopsequence() File "D:\LabPrograms\ScopeFoundry_POLIMI\DMD_ScopeFoundry\DMDDeviceHID.py", line 171, in stopsequence self.command('w',0x00,0x1a,0x24,[0]) File "D:\LabPrograms\ScopeFoundry_POLIMI\DMD_ScopeFoundry\DMDDeviceHID.py", line 69, in command self.device.write(buffer) _File "C:\Users\OPT\anaconda3\envs\ScopeFoundry\lib\site-packages\hid__init.py", line 155, in write return self.hidcall(hidapi.hidwrite, self.dev, data, len(data)) File "C:\Users\OPT\anaconda3\envs\ScopeFoundry\lib\site-packages\hid\init.py", line 142, in hidcall ret = function(*args, **kwargs) ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

The arguments that I pass at ret = function(*args, *kwargs) are in this figure (args and **kwargs). image

Why do I get this error? How can I solve it? Thank you.

apmorton commented 4 years ago

write('\x00\x64\x00\x03\x00...') is the correct way to call the API versus what you currently have write([0, 64, 0, 3, 0, ...])

IE, write needs a binary string, not a list of numbers

micropolimi commented 4 years ago

Even with your suggestion, the error still remains. Other solutions?

apmorton commented 4 years ago

without seeing the code in question and/or the resulting datatype being passed to write at runtime it is hard to say - are you able to get trivial examples working?

write('hello') or something

jiaxin96 commented 4 years ago

I have the same problem. here is my test code and the ERR message.

import hid
vid = 0xFEED# Change it for your device
pid = 0x6060# Change it for your device
h = hid.Device(vid, pid)
h.send_feature_report('\x66\x44')

ArgumentError Traceback (most recent call last)

in ----> 1 h.send_feature_report('\x66\x44') ~/anaconda3/lib/python3.6/site-packages/hid/__init__.py in send_feature_report(self, data) 168 def send_feature_report(self, data): 169 return self.__hidcall(hidapi.hid_send_feature_report, --> 170 self.__dev, data, len(data)) 171 172 def get_feature_report(self, report_id, size): ~/anaconda3/lib/python3.6/site-packages/hid/__init__.py in __hidcall(self, function, *args, **kwargs) 140 raise HIDException('device closed') 141 --> 142 ret = function(*args, **kwargs) 143 144 if ret == -1: ArgumentError: argument 2: : wrong type
apmorton commented 4 years ago

can you try b"\x66\x44"?

tunstek commented 4 years ago

Here's an example:

byte_array = bytearray();

# 64 bytes of 0's as an example
for i in range(64):
    byte_array.append(0)

with hid.Device(hid_vendor_id, hid_product_id) as h:
      h.write(bytes(byte_array))

I think it would be helpful if something like this was added to the readme @apmorton

mikics commented 4 years ago

Thank you very much for all your answers, I was able to communicate in another way (without using pyhidapi).

I think that for the correct understanding of the functions, adding some examples to the readme like @tunstek suggested could be totally useful.