Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mikics opened this issue Sep 3, 2020 · 7 comments
Open

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

mikics opened this issue Sep 3, 2020 · 7 comments

Comments

@mikics
Copy link

mikics commented Sep 3, 2020

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.hid_write, 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
Copy link
Owner

apmorton commented Sep 3, 2020

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
Copy link

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

@apmorton
Copy link
Owner

apmorton commented Sep 7, 2020

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
Copy link

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: <class 'TypeError'>: wrong type

@apmorton
Copy link
Owner

can you try b"\x66\x44"?

@tunstek
Copy link

tunstek commented Oct 16, 2020

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
Copy link
Author

mikics commented Oct 17, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants