I'm trying to create USB driver object using pyusb for my code, and the return from reading operation always return '1', I search the web for a while, and I couldn't find any solution, please try to help me understand what am I'm doing wrong.
Environmental properties: Using python 3.7 with pycharm on windows 10. Imported all necessary packages (usb.core, usb.util) of course after installing the pyusb module.
The object I'm trying to build:
class UsbDev:
def __init__(self, VID, PID):
self.output_ep = None
self.input_ep = None
self.VID = VID
self.PID = PID
self.dev = usb.core.find(idVendor = self.VID, idProduct = self.PID)
if self.dev is None:
raise AttributeError('USB device is not connected...')
def check_device(self, dev_name=None):
if dev_name is None:
raise ValueError("device name provided is None")
if self.dev.product != dev_name:
raise ValueError('Wrong type of product connected to host')
def config_device(self):
self.dev.set_configuration()
cfg = self.dev.get_active_configuration()
self.output_ep = usb.util.find_descriptor(cfg[(0, 0)], custom_match=lambda e:
usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
self.input_ep = usb.util.find_descriptor(cfg[(0, 0)], custom_match=lambda e:
usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
def read_string(self):
if self.input_ep is not None:
ret_array = self.dev.read(self.input_ep.bEndpointAddress,
self.input_ep.wMaxPacketSize)
self.dev.clear_halt(self.input_ep)
return ''.join([chr(x) for x in ret_array]) # always returns 1
the test case:
driver = ud(0x0403, 0xed72) # HAMEG HO720 Serial Port
driver.check_device('HAMEG HO720')
driver.config_device()
driver.send("*IDN?")
print(driver.read_string())
expected output:
HAMEG,HMP4040,055310003,HW50020001/SW2.41