更新
我成功地发送了数据。对于遇到同样问题的人,我使用了以下代码:
data=[0x00, 0x04, 0x04, 0xFF, 0xFF, 0xFF, 0x00, 0x00]
result=dev.ctrl_transfer(0x21, 0x9, wValue=0x200, wIndex=0x00, data_or_wLength=data)
这是基于此处发布的答案(链接)。
但我不详细理解,为什么我必须使用它。
bmRequestType=0x21
bRequest=0x9
wValue=0x200
什么是解释?
初始请求:
我正在尝试使用PyUSB向HID设备发送简单报告。
使用“SimpleHIDwrite”,我确认设备正常工作。我想发送以下数据:
报告ID:00
数据:[00,04,04,FF,FF,FF,00,00]
我对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
report
只是一个字节数组;它的格式在HID描述符中有描述。例如,在这里中,你基本上需要将各个部分(坐标、按钮、报告ID)打包到report
字节数组中,并通过hid_set_report()
发送。一个好的方法是:使用USB嗅探设置Wireshark,查看真实的HID报告,并尝试弄清楚每个字节的含义。然后模拟它。 - ulidtkohid_set_report()
和从hid_get_report()
获取的内容完全取决于设备。上面评论中有一个鼠标示例。如需进一步帮助,请私信联系我(我的用户名 @ gmail)。 - ulidtko