我有一个相当简单的 USB HID 设备,我一直在尝试弄清楚如何使用 Python 读取和写入。我已经能够使用 PyWinUSB 读取它,但是当我尝试写入它时出现问题。尝试写入它会使事情爆炸。
例如:
device = hid.HidDeviceFilter(vendor_id = 0x0003, product_id = 0x1001).get_devices()[0]
这很好用。然后读取原始数据,这是我现在关心的所有内容(一旦我弄清楚如何写入该诅咒的东西,我就会使用它):
def readData(data):
print(data)
return None
这很好用(事实上,当我看到它工作时,它非常令人兴奋)。所以我会像这样分配数据处理程序:
device.set_raw_data_handler(readData)
每次我按下一个按钮,都很好。数据如您所料。这太棒了!
当我想写入设备时,问题就来了。 按照示例 simple_send 文件作为模板(这可能不是最佳选择),我将执行以下操作:
report = device.find_output_reports()[0]
这将返回一个包含 4 个条目的字典的报告对象。那是对的吗?您是否使用 output_reports 对象写入设备?尝试通过将报告值设置为任何东西来做到这一点:
report[<key>] = "pneumonoultramicroscopicvolcanoconiosis"
report.send()
这会不断返回一些我无法解释的令人讨厌的错误:
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
report.send()
File "C:\Python27\lib\site-packages\pywinusb-0.3.1-py2.7.egg\pywinusb\hid\core.py", line 1446, in send
self.__prepare_raw_data()
File "C:\Python27\lib\site-packages\pywinusb-0.3.1-py2.7.egg\pywinusb\hid\core.py", line 1401, in __prepare_raw_data
byref(self.__raw_data), self.__raw_report_size) )
File "C:\Python27\lib\site-packages\pywinusb-0.3.1-py2.7.egg\pywinusb\hid\winapi.py", line 382, in __init__
raise helpers.HIDError("hidP error: %s" % self.error_message_dict[error_code])
HIDError: hidP error: data index not found
我正在使用 Windows 7。我已经(最终)找到了 HID DLL 导出函数的参考,而且我不必(或者,就此而言,甚至真的想要)使用 PyWinUSB 库。我只想完成这项工作,它似乎并没有那么难,但它一直如此。
有人能告诉我我在这里做错了什么吗?
谢谢。
此外,我尝试跟踪错误调用,并且在程序刚刚关闭之前就已经完成了,这有点令人沮丧。
我让它与这个一起工作
buffer= [0xFF]*33 # 33 = report size + 1 byte (report id)
buffer[0]=0x0 # report id
buffer[1]=0xFE
buffer[2]=0x00
buffer[3]=0xFF
out_report.set_raw_data(buffer)
out_report.send()
dev.close()
对我来说只有这个:
report.send([0x70, ..., 0x73 ])
带有
set_raw_data([0x70, ..., 0x73)
和后续send()
的函数调用序列对我不起作用。
我遇到了同样的问题
我的代码:
device = devices[0] # Assuming there is only one device found
device.open()
# Find the input report object for receiving data
input_report = device.find_input_reports()[0]
exit_flag=False
# Define a callback function to handle received data
def handle_data(data):
# Process and handle the received data
# Example: Print the received data as hexadecimal values
print(data)
return None
# Start the read loop to continuously receive data
device.set_raw_data_handler(handle_data)
# Wait for data to be received (you can use your own waiting mechanism)
# Close the device
device.close()
代码只是执行,不打印任何东西。请帮助我完成这项任务