简体   繁体   English

使用 PyUSB 发送 HID 报告

[英]Send HID report with PyUSB


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.我拼命地尝试使用 PyUSB 向 HID 设备发送一个简单的报告。

Using "SimpleHIDwrite" I confirmed that the device works just as expected.使用“SimpleHIDwrite”我确认设备按预期工作。 I want to send this data:我想发送这个数据:

report ID: 00报告编号:00

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

Sending data using SimpleHIDwrite使用 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.我对 Python 和 USB 很陌生,我无法弄清楚如何使用 dev.ctrl_transfer 或 dev.write 来做到这一点。

Also, there are some posts about sending data to HID devices, but I couldn't figure out how to solve my problem.此外,还有一些关于将数据发送到 HID 设备的帖子,但我不知道如何解决我的问题。 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:这是所有你需要做的HIDPyUSB:

  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.我见过的所有四个 libusbhid 都是用可怕的 C 语言编写的,并且依赖于更多的库。 For what is essentially 10 lines of code.对于本质上是 10 行代码的内容。 Make your own decision.自己做决定。

Don't use PyUSB (unless you need other protocols too).不要使用 PyUSB(除非您也需要其他协议)。 Managing HID isn't difficult, but there is a much easier solution.管理 HID 并不困难,但有一个更简单的解决方案。

HIDAPI is a C-library which manages the protocol, and there is a Python wrapper available too. HIDAPI 是一个管理协议的 C 库,还有一个 Python 包装器可用。

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.此外,它还隐藏了从操作系统收回控制权的必要性,操作系统在连接时识别 HID 协议,并安装自己的驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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