Python PyUSB HID Feature Report

2019-08-14 02:43发布

I am accessing a USB HID Device using python hidapi from a Mac OSX 10.10.5 doing:

import hid
import time

hidraw = hid.device(0x1a67, 0x0004)
hidraw.open(0x1a67, 0x0004)

#                           Rpt, GnS, Tgt, Size, Index LSB, Index MSB, Data
# Blink 4 pulses
hidraw.send_feature_report([0x00, 0x00, 0x00,0x01, 0x01, 0x00, 0x03])

hidraw.get_feature_report(33,33)
time.sleep(3)

The HID Feature Report works nicely without problems. However, I am trying to port this code to PyUSB and trying to do the same thing (on a RaspberryPi)

import usb.core
import usb.util

# find our device
dev = usb.core.find(idVendor=0xfffe, idProduct=0x0004)

# was it found?
if dev is None:
    raise ValueError('Device not found')

# get an endpoint instance
for interface in dev.get_active_configuration():
   if dev.is_kernel_driver_active(interface.bInterfaceNumber):
      # Detach kernel drivers and claim through libusb
      dev.detach_kernel_driver(interface.bInterfaceNumber)
      usb.util.claim_interface(dev, interface.bInterfaceNumber)

# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()

ret = dev.ctrl_transfer(0x00, 0x00, 0x01, 0x01, [0x00, 0x03])

But I get a Broken Pipe when executed with root permissions. It is not very clear how to map the parameters that I used in the send_feature_report of Hidapi to how it is actually used from ctrl_transfer in PyUSB.

Any help on how this mapping should be made?

Thanks !!!

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-14 03:44

Your parameters in the dev.ctrl_transfer command looks wrong.

The fact is that dev.ctrl_transfer will set several parameters like the direction of the message, the lenght and the content of your control message (everything is well explain in this link: http://www.beyondlogic.org/usbnutshell/usb6.shtml#SetupPacket)

So you have to set the parameters in function of your device and what you want to do. For exemple in my code and for my device I have this command:

dev.ctrl_transfer(0x21, 0x09, 0x200, 0x00, command)
查看更多
登录 后发表回答