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

问题描述

更新

我管理它可以正确发送数据.对于遇到同样问题的任何人,我都使用以下代码:

I managed it to send the data properly. For anyone how 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

如果有人能更详细地解释它,我将不胜感激.我只想学习.

If anyone could explain it in more detail, I would be grateful. I just want to learn.

初始请求:

我正拼命尝试使用PyUSB向HID设备发送简单的报告.

I'm desperately trying to send a simple report to a HID-device using PyUSB.

使用"SimpleHIDwrite",我确认该设备可以按预期工作.我要发送此数据:

Using "SimpleHIDwrite" I confirmed, that the device works just as expected. I want to send this data:

报告ID:00

数据:[00,04,04,FF,FF,FF,00,00]

data: [00, 04, 04, FF, FF, FF, 00, 00]

使用SimpleHIDwrite发送数据

我对python和USB还是很陌生,我不知道如何使用dev.ctrl_transfer或dev.write来做到这一点.

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.

此外,还有一些有关将数据发送到HID设备的帖子,但我不知道该如何解决我的问题.

Also, there are some posts about sending data to HID devices, but I couldn't figure out how to solve my problem.

任何人都可以请我指出正确的方向吗?

Could anybody please be so kind to point me in the right direction?

非常感谢!

以下是更多详细信息:

 # 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

推荐答案

请勿使用PyUSB(除非您也需要其他协议).管理HID并不困难,但是有一个更简单的解决方案.

Don't use PyUSB (unless you need other protocols too). Managing HID isn't difficult, but there is a much easier solution.

HIDAPI是管理协议的C库,并且有也可以使用Python包装器.

此外,它隐藏了从操作系统中夺回控制权的必要性,后者可以识别连接时的HID协议,并安装自己的驱动程序.

Also, it hides the necessity to take control back from the operating system, which recognizes the HID protocol on connection, and install its own driver.

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

10-13 01:22