USB设备:无法分离内核驱动程序或查找设备句柄

时间:2017-08-01 16:50:07

标签: python-3.x handle libusb detach pyusb

我是Python的新手,一直在教自己如何写入并正确使用PyUSB模块。在过去的一个月里,我一直在尝试使用PyUSB创建一个Python脚本来与USB设备进行通信。该脚本旨在使用它的产品和供应商ID查找我的设备,然后列出有关该设备的一些信息(例如接口号,端点地址等)。然后,我设计了程序来执行批量传输以读取设备正在接收的数据值。我的设备是由Smarte-Sensors制造的标准USB评估板,我们使用它来读取多个电容器的值。我正在使用Zadig在设备上安装libusb驱动程序,因此可以读取设备并与libusb后端进行通信。

程序可以正确找到设备(使用下面的Find_EvalB函数),但是当我尝试分离内核驱动程序时,它给了我Errno 40,它说该请求不受支持且无法执行。当我进一步挖掘时,我发现当我试图照亮并找到设备句柄时,该操作也失败了。由于某种原因,我的设备似乎没有句柄或找不到它,因此无法执行I / O操作(这是我正确读取设备所需的)。

以下是我的一些代码:

# 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  ----

注意:并非所有功能都允许更多空间

需要并提供很多帮助。如果需要更多信息,请询问,我会尝试提供。谢谢!

0 个答案:

没有答案