Reputation: 397
I can't read hid data by using pywinusb in python.
I referred to this page(https://www.reddit.com/r/learnpython/comments/3z346p/reading_a_usb_data_stream_on_windows/)
and I have question.
def sample_handler(data): print("Raw data: {0}".format(data))
sample_handler function needs to data.
but
device.set_raw_data_handler(sample_handler)
this code do not give data to sample_handler. is it not error?
and below is my code. my code don't catch read_handler function. how can i fix it. could you help me?
from pywinusb import hid
import time
class PIC18f:
def __init__(self, VID = 0x04D8, PID=0x003f):
filter = hid.HidDeviceFilter(vender_id = VID, product_id = PID)
self.devices = filter.get_devices()
self.device = self.devices[0]
self.device.open()
def write(self, args):
out_report = self.device.find_output_reports()
out_report[0].set_raw_data(args)
out_report[0].send()
time.sleep(1)
def read_handler(self, data):
print("Raw data: {0}".format(data))
print("done")
def I2C_Init(self):
buf = [0x00]
buf = buf + [0 for i in range(65-len(buf))]
buf[1] = 0xF1
buf[2] = 0x1D
self.write(buf)
self.device.set_raw_data_handler(read_handler)
test = PIC18f()
test.I2C_Init()
this is error.
Traceback (most recent call last): File "d:/1. Siliconmitus/python/test2.py", line 35, in test.I2C_Init() File "d:/1. Siliconmitus/python/test2.py", line 32, in I2C_Init self.device.set_raw_data_handler(read_handler) NameError: name 'read_handler' is not defined
Upvotes: 0
Views: 4231
Reputation: 109
read_handler is not defined because the "read_handler" should be defined inside I2C_Init.
The following is an example:
from pywinusb import hid
filter = hid.HidDeviceFilter(vendor_id = 0x0001, product_id = 0x0002)
devices = filter.get_devices()
device = devices[0]
def readData(data):
print(data)
return None
device.set_raw_data_handler(readData)
device.open()
Upvotes: 1