Search code examples
pythonusbreverse-engineeringlibusbpyusb

Pyusb: Ressource busy / USB bar code Scanner


I'm trying to get value from a barcode scanner, this with pyusb on Ubuntu. After investigations, I found that the barcode scanner need to receive an activation data to be able to scan barcode. I found this data, modify my rules.d file in order to detect my device and then I ran this code to send the data through USB:

import usb.core
import usb.util

# find our device
dev = usb.core.find(idVendor=0x05f9, idProduct=0x1203)

# was it found?
if dev is None:
    raise ValueError('Device not found')

# Attach and detach the usb
if dev.is_kernel_driver_active(0):
    dev.detach_kernel_driver(0)

# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()

# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]

ep = usb.util.find_descriptor(
    intf,
    # match the first OUT endpoint
    custom_match = \
    lambda e: \
        usb.util.endpoint_direction(e.bEndpointAddress) == \
        usb.util.ENDPOINT_OUT)

assert ep is not None

# write the data
ep.write('\x0b')

I get the following error:

usb.core.USBError: [Errno 16] Resource busy

But normally the following code must made the device available:

# Attach and detach the usb
    if dev.is_kernel_driver_active(0):
        dev.detach_kernel_driver(0)

I tryed this code with another device (printer) and it's working.

Do you have any idea concerrning the problem?


Solution

  • I found the solution.

    The problem was that usbhid driver use directly the device. So after desactivation of the drive I'm able to use it.