PyUSB发送HID报告

问题描述 投票:2回答:2

UPDATE


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

data=[0x00, 0x04, 0x04, 0xFF, 0xFF, 0xFF, 0x00, 0x00]
result=dev.ctrl_transfer(0x21, 0x9, wValue=0x200, wIndex=0x00, data_or_wLength=data)

(这是基于这里发布的答案:link

但我不明白,为什么我要使用

bmRequestType=0x21
bRequest=0x9
wValue=0x200

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


初步要求:


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

使用“SimpleHIDwrite”我确认,该设备正常工作。我想发送这些数据:

报告编号:00

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

sending data using SimpleHIDwrite

我是python和USB的新手,我无法弄清楚如何使用dev.ctrl_transfer或dev.write来做到这一点。

此外,还有一些关于向HID设备发送数据的帖子,但我无法弄清楚如何解决我的问题。

请有人帮我指点正确的方向吗?

非常感谢你!

以下是一些更多细节:

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

以上脚本的结果如下:

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
python windows usb hid pyusb
2个回答
3
投票

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

管理协议的HIDAPI is a C-library,也有a Python wrapper

此外,它隐藏了从操作系统恢复控制的必要性,操作系统识别连接时的HID协议,并安装自己的驱动程序。


1
投票

这就是你需要用PyUSB做的所有HID:

  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, 0x00,
          report)

  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, 0x00,
          64)

没有必要跳到图书馆 - 包装 - 围绕图书馆的潮流。你是工程师还是什么?只是read the doc。该协议不会很快改变。

最后,是的。我见过的所有4个libusbhid都是用灾难性的C编写的,依赖于更多的库。基本上是10行代码。做出自己的决定。

© www.soinside.com 2019 - 2024. All rights reserved.