Python PyUSB HID功能报告 [英] Python PyUSB HID Feature Report

查看:917
本文介绍了Python PyUSB HID功能报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Mac OSX 10.10.5中使用python hidapi访问USB HID设备,操作如下:

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)

HID功能报告运行良好,没有任何问题. 但是,我试图将此代码移植到PyUSB并尝试在RaspberryPi上做同样的事情

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])

但是在以root权限执行时,我遇到了管道破裂的问题.尚不清楚如何将我在Hidapi的send_feature_report中使用的参数映射到PyUSB中的ctrl_transfer中实际使用的参数.

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?

谢谢!!!

  • http://libusb.sourceforge.net/api-1.0/group__syncio.html
  • https://github.com/walac/pyusb/blob/master/docs/tutorial.rst

推荐答案

您在dev.ctrl_transfer命令中的参数看起来不正确.

Your parameters in the dev.ctrl_transfer command looks wrong.

事实是dev.ctrl_transfer将设置几个参数,例如消息的方向,长度和控制消息的内容 (此链接中的所有内容都得到了很好的解释: http://www.beyondlogic.org/usbnutshell/usb6.shtml#SetupPacket )

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)

这篇关于Python PyUSB HID功能报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆