更新
我设法正确地发送了数据。对于遇到同样问题的任何人,我使用了以下代码:
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“,我确认了该设备的工作方式与预期完全相同。我想发送这个数据:
报表ID: 00
数据: 00,04,04,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
发布于 2018-09-17 21:08:22
这就是你只需要用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, # "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
)
没有必要跳到库包装的库周围的大潮中。你是工程师还是什么?只有read the documentation。协议不会很快改变。
最后,是的。我见过的所有四个libusbhid都是用可怕的C编写的,并且依赖于更多的库。本质上是10行代码。做你自己的决定。
发布于 2016-06-27 04:28:47
不要使用PyUSB (除非您还需要其他协议)。管理HID并不困难,但有一个简单得多的解决方案。
管理协议的HIDAPI is a C-library,还有可用的a Python wrapper。
此外,它隐藏了从操作系统夺回控制权的必要性,操作系统在连接时识别HID协议,并安装自己的驱动程序。
https://stackoverflow.com/questions/37943825
复制相似问题