USB device connectivity in Python

I want to communicate and send data to a USB device. I can find the device, but when I connect the device with the kernel driver, it gives USB Error: Resource Busy . Below is my code:

  dev = usb.core.find(idVendor=0x0403, idProduct=0x6001) dev.set_configuration() cfg = dev.get_active_configuration() dev.attach_kernel_driver(interface) interface_number = cfg[(0,0)].bInterfaceNumber alternate_settting = usb.control.get_interface(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) dev.detach_kernel_driver(interface) ep.write("\r"+linea1[:19]+"\n\r"+" "*(20-len(linea2))+linea2) 
+4
source share
1 answer

Suppose you are using Linux and libusb-1.0 in quality .

According to the libusb documentation :

 // Detach a kernel driver from an interface. // If successful, you will then be able to claim the interface and perform I/O. int libusb_detach_kernel_driver (libusb_device_handle *dev, int interface_number) // Re-attach an interface kernel driver, which was previously // detached using libusb_detach_kernel_driver(). int libusb_attach_kernel_driver(libusb_device_handle *dev, int interface_number) 

Basically, you need to call detach_kernel_driver first to disconnect the already connected kernel driver (if any) from the device interface so that you can communicate with it in your code (this is either your code or some kernel driver talking to the device interface). When you're done, you can call attach_kernel_driver to reconnect the kernel driver.

I believe that there is no need to call any of these Python C functions / methods if you can make sure that the kernel driver is not loaded for this device (or manually unload it before running your code).

Edit:

I just got this piece of code (based on your sample). Note: for simplicity, I hardcoded 0 as the interface number for detach_kernel_driver and attach_kernel_driver - you should do it smarter, I suppose.

 import usb dev = usb.core.find(idVendor=0x0403, idProduct=0x6001) reattach = False if dev.is_kernel_driver_active(0): reattach = True dev.detach_kernel_driver(0) dev.set_configuration() cfg = dev.get_active_configuration() interface_number = cfg[(0,0)].bInterfaceNumber alternate_settting = usb.control.get_interface(dev, interface_number) intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number, bAlternateSetting = alternate_settting) ep = usb.util.find_descriptor(intf,custom_match = \ lambda e: \ usb.util.endpoint_direction(e.bEndpointAddress) == \ usb.util.ENDPOINT_OUT) ep.write("test\n\r") # This is needed to release interface, otherwise attach_kernel_driver fails # due to "Resource busy" usb.util.dispose_resources(dev) # It may raise USBError if there eg no kernel driver loaded at all if reattach: dev.attach_kernel_driver(0) 
+7
source

Source: https://habr.com/ru/post/1435649/


All Articles