UPDATE
I managed to send the data properly. For anyone who ran into the same problem, I used the following code:
data=[0x00, 0x04, 0x04, 0xFF, 0xFF, 0xFF, 0x00, 0x00]
result=dev.ctrl_transfer(0x21, 0x9, wValue=0x200, wIndex=0x00, data_or_wLength=data)
(This is based on the answer posted here: link)
But I don't understand in detail, why I have to use
bmRequestType=0x21
bRequest=0x9
wValue=0x200
What is the explanation?
Initial request:
I'm desperately trying to send a simple report to a HID-device using PyUSB.
Using "SimpleHIDwrite" I confirmed that the device works just as expected. I want to send this data:
report ID: 00
data: [00, 04, 04, FF, FF, FF, 00, 00]
Sending data using SimpleHIDwrite
I'm quite new to Python and USB and I can't figure out how to do this using dev.ctrl_transfer or dev.write.
Also, there are some posts about sending data to HID devices, but I couldn't figure out how to solve my problem. How can I fix it?
Here are some more details:
# Based on https://github.com/walac/pyusb/blob/master/docs/tutorial.rst
import usb.core
import usb.util
# Find our device
# dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)
dev = usb.core.find(idVendor=0x1781, idProduct=0x8c0)
# Was it found?
if dev is None:
raise ValueError('Device not found')
dev.set_configuration()
cfg = dev[0]
intf = cfg[(0,0)]
ep = intf[0]
# dev.write(ep.bEndpointAddress, [0x00, 0x00,0x04,0x04,0xFF,0xFF,0xFF,0x00, 0x00], 1000)
# dev.ctrl_transfer(bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength=None, timeout=None)
print("print ep")
print(ep)
print("print cfg")
print(cfg)
print("print intf")
print(intf)
And the result of the script above is this:
print ep
ENDPOINT 0x81: Interrupt IN ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x81 IN
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x8 (8 bytes)
bInterval : 0xa
print cfg
CONFIGURATION 1: 100 mA ==================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x2 Configuration
wTotalLength : 0x22 (34 bytes)
bNumInterfaces : 0x1
bConfigurationValue : 0x1
iConfiguration : 0x0
bmAttributes : 0x80 Bus Powered
bMaxPower : 0x32 (100 mA)
INTERFACE 0: Human Interface Device ====================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x0
bAlternateSetting : 0x0
bNumEndpoints : 0x1
bInterfaceClass : 0x3 Human Interface Device
bInterfaceSubClass : 0x0
bInterfaceProtocol : 0x0
iInterface : 0x0
ENDPOINT 0x81: Interrupt IN ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x81 IN
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x8 (8 bytes)
bInterval : 0xa
print intf
INTERFACE 0: Human Interface Device ====================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x0
bAlternateSetting : 0x0
bNumEndpoints : 0x1
bInterfaceClass : 0x3 Human Interface Device
bInterfaceSubClass : 0x0
bInterfaceProtocol : 0x0
iInterface : 0x0
ENDPOINT 0x81: Interrupt IN ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x81 IN
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x8 (8 bytes)
bInterval : 0xa
Process finished with exit code 0
This is all you need to do HID with just PyUSB:
def hid_set_report(dev, report):
""" Implements HID SetReport via USB control transfer """
dev.ctrl_transfer(
0x21, # REQUEST_TYPE_CLASS | RECIPIENT_INTERFACE | ENDPOINT_OUT
9, # SET_REPORT
0x200, # "Vendor" Descriptor Type + 0 Descriptor Index
0, # USB interface № 0
report # the HID payload as a byte array -- e.g. from struct.pack()
)
def hid_get_report(dev):
""" Implements HID GetReport via USB control transfer """
return dev.ctrl_transfer(
0xA1, # REQUEST_TYPE_CLASS | RECIPIENT_INTERFACE | ENDPOINT_IN
1, # GET_REPORT
0x200, # "Vendor" Descriptor Type + 0 Descriptor Index
0, # USB interface № 0
64 # max reply size
)
There isn't any need to jump onto the library-wrappers-around-libraries bandwagon. Are you an engineer or what? Just read the documentation. The protocol is not going to change anytime soon.
Finally, yeah. All the four libusbhid's I've seen are written in disastrously horrible C and depend on yet even more libraries. For what is essentially 10 lines of code. Make your own decision.