在Python中简单地从USB HID设备读取/写入数据? [英] Simple reading/writing from/to a USB HID device in Python?

查看:2351
本文介绍了在Python中简单地从USB HID设备读取/写入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的USB HID设备,我一直在努力弄清楚如何使用Python进行读写.我已经可以使用PyWinUSB从中读取内容,但是当我尝试对其进行写入时,就会出现问题.尝试写它会使事情爆炸.

I've got a fairly simple USB HID device that I've been trying to figure out how to read from and write to using Python. I've been able to read from it using PyWinUSB, but the problem comes in when I try to write to it. Trying to write to it makes things explode.

例如:

device = hid.HidDeviceFilter(vendor_id = 0x0003, product_id = 0x1001).get_devices()[0]

这很好.然后用于读取原始数据,这就是我现在所关心的全部(一旦我能弄清楚如何写被诅咒的东西,我将使用该数据):

This works fine. Then for reading raw data, which is all that I care about right now (I'll work with that once I can figure out how to write to the cursed thing):

def readData(data):
    print(data)
    return None

这很好用(实际上,当我看到它起作用时,这非常令人兴奋).所以我会像这样分配数据处理程序:

This works fine (in fact, it was quite exciting when I got to see it work). So I would assign the data handler like so:

device.set_raw_data_handler(readData)

每当我按下一个按钮时,就可以了.数据如您所愿.太好了!

And every time I hit a button, it's fine. The data comes through as you would expect. Which is great!

当我要写入设备时出现问题. 按照样本simple_send文件作为模板(这可能不是最佳选择),我将执行以下操作:

The problem comes when I want to write to the device. Following the sample simple_send file as a template (which was probably not the best choice), I would do the following:

report = device.find_output_reports()[0]

这将返回带有包含4个条目的字典的报表对象.那是对的吗?您是否使用output_reports对象写入设备?尝试通过将报告值设置为ANYTHING:

Which would return a report object with a dictionary holding 4 entries. Is that correct? Do you write to a device using the output_reports object? Trying to do so by setting the report value to ANYTHING:

report[<key>] = "pneumonoultramicroscopicvolcanoconiosis"
report.send()

这将继续返回一些我无法解释的令人讨厌的错误:

This would keep returning some obnoxious error that I can't interpret:

    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

我正在使用Windows7.我设法(最终)找到了HID DLL导出功能的参考,但我没有(或者就此而言真的想)使用PyWinUSB库. .我只是想做这项工作,似乎没有那么困难,但是确实如此.

I'm using Windows 7. I've managed to find (finally) a reference for the HID DLL exported functions, and I don't HAVE to (or, for that matter even really WANT to) use the PyWinUSB library. I just want to make this work, and it didn't seem like it would be that tough, but it has been.

有人可以告诉我我在这里做错了什么吗

Can someone tell me what it is I've been doing wrong here?

谢谢.

此外,我尝试跟踪错误调用,并在程序关闭之前进行了很长时间,这实在令人沮丧.

Also, I tried tracing the error call, and made it so far before the program just closed which was kind of disheartening.

推荐答案

我对此进行了操作

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

这篇关于在Python中简单地从USB HID设备读取/写入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆