简体   繁体   中英

Python and Hidapi: argument error when trying to write a usb device

I'm using python 3.9.6 to manage a hid relay board enter image description here

I can read vendor id 1305 and product id 8216 of my board using enumerate command, then I tried some commands:

>>> device = hid.Device(1305,8216)
>>> device.manufacturer
'Ucreatefun.com'
>>> device.product
'HIDRelay'
>>> device.serial
'A0001'

Now I'd like to manage the relays on the board using "device.write", how can I do this? From the manual of relay board:

" Channel 1 open: 0x00 0xf1, close: 0x00 0x01 "

Unfortunately it doesn't work, when I try this:

>>> device.write([0x00,0xF1])
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
device.write([0x00,0xF1])
File 
"C:\Users\trevisan\AppData\Local\Programs\Python\Python39\lib\site- 
packages\hid\__init__.py", line 155, in write
return self.__hidcall(hidapi.hid_write, self.__dev, data, len(data))
File 
"C:\Users\trevisan\AppData\Local\Programs\Python\Python39\lib\site- 
packages\hid\__init__.py", line 142, in __hidcall
ret = function(*args, **kwargs)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type
>>> 

Why python raises a type error when I pass a list in device.write method?

Thanks a lot! Max

I solved using the class bytes:

>>> device.write(bytes([0x00,0xf1]))

I'm very happy! Max

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM