1

I am new to Python and have been teaching myself how to write in it and use the PyUSB module properly. The past month, I have been trying to create a Python script using PyUSB to communicate with a USB device. The script is designed to find my device using it's product and vendor id's, then list out some of the information (e.g. interface number, endpoint address, etc) about the device. Then, I have designed the program to perform a bulk transfer to read data values that the device is receiving. My device is a Standard USB Evaluation Board made by Smarte-Sensors, and we are using it to read values of multiple capacitors. I am using Zadig to install a libusb driver onto the device, so the device is able to be read and get in communication with the libusb backend.

The program can find the device correctly (using the Find_EvalB function below), but when I try to detach the kernel driver it gives me Errno 40 which says that this request is not supported and couldn't be performed. When I did further digging, I found out that, when I tried to illuminate and find the device handle, that operation failed as well. For some reason, it appears that my device either doesn't have a handle or cannot find it, and thus cannot perform I/O operations (which is what I need to properly read the device).

Here's some of my code:

# Function for finding the evaluation board (device) and detaching the kernel driver to start communication (I/O) 

def Find_EvalB(vendor_id, product_id):
global backend

#---
# Find the Microcontroller
devi = usb.core.find(find_all = False, backend = backend, custom_match = None, idVendor = vendor_id, idProduct = product_id)
# Note if the Microcontroller was not found or found
if devi is None:
    raise ValueError('The device you searched for was not found')
else:
    print("FOUND DEVICE")
    print( )
#---

#---
# Detach kernel from the interface (needed to perform I/O operations) ---- !!!!!!!!!!!!!!!!!!!!CURRENT PROBLEM IS HERE!!!!!!!!!!!!!!!!!!!! -----
# Right now, we do not have permission to detach the kernel driver - PROBLEM = OUR DEVICE HAS NO HANDLE
#-------------------
try:
    if devi.is_kernel_driver_active(interface = 0):
        devi.detach_kernel_driver(interface = 0)
except usb.core.USBError as e:
        sys.exit("Kernel detachment failed, please fix this and try again: %s" % str(e))
#------------------    
#---    

#---   
# Use the first/default configuration that we find (this will be in Interface 0)
devi.set_configuration()
# Get an interface number and endpoint instance - first instance
cfg = devi.get_active_configuration()
interface_number = cfg[(0,0)].bInterfaceNumber
alternate_setting = usb.control.get_interface(devi, interface_number)
intf = usb.util.find_descriptor(cfg,
                                bInterfaceNumber = interface_number,
                                bAlternateSetting = alternate_setting)
ep = usb.util.find_descriptor(
    intf,

    custom_match = \
    lambda e: \
        usb.util.endpoint_direction(e.bEndpointAddress) == \
        usb.util.ENDPOINT_OUT)  
assert ep is not None

#---------------------------------------------------------------------------

# Function to print all device descriptor information -- NOTE: ENDPOINT TYPES - ( 0=ctrl transfer, 1=iso, 2=bulk, 3=intr) - I have gotten a bulk transfer type
def dev_descriptors(x):

# List the Device Settings
_name = usb.util.get_string(x, 256,2)
print( )
print("Device Name=", _name)
print("    Device Class:", x.bDeviceClass)
print("    Max Packet Size:", x.bMaxPacketSize0)
print("    Number of Configurations:", x.bNumConfigurations)
print("    idVendor:", hex(x.idVendor))
print("    idProduct:", hex(x.idProduct))
#print("    Device Handle: ", x.handle)
print( )

# List the Configurations Settings    
for config in devi:
    print("Configuration Value:", config.bConfigurationValue)
    print("    Configuration Length:", config.bLength)
    print("    Total Length:", config.wTotalLength)
    print( )

    # List the Interface Settings   
    for intf in config:
        print("Interface Number:", intf.bInterfaceNumber)
        print("    Alternate Setting:", intf.bAlternateSetting)
        print("    Interface Class:", intf.bInterfaceClass)
        print("    Interface Sub Class:", intf.bInterfaceSubClass)
        print("    Interface Protocol:", intf.bInterfaceProtocol)
        print("    Interface Length:", intf.bLength) 
        print( )

        # List the Endpoint Settings       
        for ep in intf:
            print("Endpoint:", hex(ep.bEndpointAddress))
            print("    Type:", ep.bmAttributes)
            print("    Max Packet Size:", ep.wMaxPacketSize)
            print("    Interval:", ep.bInterval)  
            print("    Synch Address:", ep.bSynchAddress)
            print( )  

#----------------------------------------------------------------------------

# ----  Bulk Transfer  ----
# Create a list that we will add to over the course of the transfer
data_list = []

# Read the device and get all the data values
data = devi.read(endpoint = ep_two, size = size, interface = 0, timeout = None) # I CHANGED THE TIMEOUT AND THE INTERFACE (INTERFACE WAS 00, TIMEOUT WAS 5000)
print( )
if num_of_samples != 0:
print('Capacitance Data:')

sample_number = 0

while num_of_samples > 0:
    # Now, we will read the data value of whichever sample number we are on (this loop will continuously go through each sample)
    # --     We are using str.format to format our decimal values of the data to whatever number of significant figures we need     --

    data_value = data[sample_number]
    print(toStr(str.format('{0:.15f}', data_value)))

    # Finally, we will increase/decrease appropriately
    sample_number = sample_number + 1
    num_of_samples = num_of_samples - 1
    data_list = add_item(data_list, data_value)
    # Once the number of samples reaches 0, the while loop will end and all of the data will have been taken

print( )
#----  End of Bulk Transfer  ----

Note: Not all functions were given to allow more space

Much help is needed and appreciated. If anymore information is needed, please ask and I will try to supply it. Thanks!

0 Answers0