如何使用python/pywinusb将隐藏数据发送到设备? [英] How to send hid data to device using python / pywinusb?

查看:341
本文介绍了如何使用python/pywinusb将隐藏数据发送到设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pywinusb将输出报告发送到pic18f4550.该设备可以接收数据,并且我已经使用C#应用程序对其进行了测试,效果很好.另外,我可以使用pywinusb从设备读取数据,但是尝试发送数据时遇到问题.

I'm trying to use pywinusb to send output report to a pic18f4550. The device can receive data, and I've tested it with a C# application, which worked fine. Also, I can read data from the device with pywinusb just fine, but I have a problem trying to send data.

这是我正在运行的代码:

Here's the code I'm running:

from pywinusb import hid

filter = hid.HidDeviceFilter(vendor_id = 0x0777, product_id = 0x0077)
devices = filter.get_devices()

if devices:
    device = devices[0]
    print "success"

device.open()
out_report = device.find_output_reports()[0]

buffer= [0x00]*65
buffer[0]=0x0
buffer[1]=0x01
buffer[2]=0x00
buffer[3]=0x01

out_report.set_raw_data(buffer)
out_report.send()
dev.close()

它会产生此错误:

success
Traceback (most recent call last):
  File "C:\Users\7User\Desktop\USB PIC18\out.py", line 24, in <module>
    out_report.send()
  File "build\bdist.win32\egg\pywinusb\hid\core.py", line 1451, in send
    self.__prepare_raw_data()
  File "build\bdist.win32\egg\pywinusb\hid\core.py", line 1406, in __prepare_raw_data
    byref(self.__raw_data), self.__raw_report_size) )
  File "build\bdist.win32\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

推荐答案

这是我的代码,它可与运行TI数据管道USB堆栈的MSP430F芯片配合使用.这基本上是隐藏的输入和输出端点,它们充当自定义数据管道,允许我以所需的任何格式发送64个字节,但第一个字节是ID号(由TI定义)为十进制63,第二个字节为数据包中相关或有用字节的数量(最大64字节数据包),其中前两个字节为上述字节.我花了一些时间才解决这个问题,主要是因为缺少文档. pywinusb附带的几个示例充其量很难学习.无论如何,这是我的代码.它正在与我的micro配合使用,因此应该可以为您提供帮助.

Here is my code and it works with an MSP430F chip running TI's datapipe USB stack. This is basically hid input and output endpoints that act as a custom data pipe allowing me to send 64 bytes in any format I want with the exception of the first byte being an ID number (defined by TI) decimal 63 and the second byte being the number of pertinent or useful bytes in the packet (64 byte max packet) with the first two bytes described above. It took me a while to figure this out mostly because of the lack of documentation. The few examples that come with pywinusb are hard to learn from at best. Anyways here is my code. It is working with my micro so this should help you.

        filter = hid.HidDeviceFilter(vendor_id = 0x2048, product_id = 0x0302)
    hid_device = filter.get_devices()
    device = hid_device[0]
    device.open()
    print(hid_device)


    target_usage = hid.get_full_usage_id(0x00, 0x3f)
    device.set_raw_data_handler(sample_handler)
    print(target_usage)


    report = device.find_output_reports()

    print(report)
    print(report[0])

    buffer = [0xFF]*64
    buffer[0] = 63

    print(buffer)

    report[0].set_raw_data(buffer)
    report[0].send()

一个可能使您烦恼的地方在这里:

One area that may be screwing you up is here:

out_report = device.find_output_reports()[0]

尝试使用"out_report = device.find_output_reports()",但结尾不要加上"[0]". 然后使用

Try using "out_report = device.find_output_reports()" without the "[0]" at the end. Then use

out_report[0].set_raw_data(buffer)

最后

out_report[0].send()

希望这对您有所帮助.

这篇关于如何使用python/pywinusb将隐藏数据发送到设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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