Wimbley
Posts: 4
Joined: Mon Oct 09, 2023 1:57 pm

Getting intermittent errno 16 "resource busy" with USB printer

Wed Feb 21, 2024 3:20 am

I'm trying to print to a USB printer on a Raspberry Pi 3 running Bullseye. Sometimes the script works fine, but usually I get "Errno 16, resource busy". dmesg shows:
usb 1-1.3: usbfs: interface 0 claimed by usblp while 'python3' sets config #1
I've added the user to lp group, tried different ports, claiming and releasing the interface, rebooted, and updated.

Code: Select all

import usb.core
import usb.util

def find_printer(vid, pid):
    dev = usb.core.find(idVendor=vid, idProduct=pid)  
    dev.reset()	
    if dev is None:
        raise ValueError('Printer not found')
    return dev

def send_to_printer(dev, data):
    try:
        interface = 0
        endpoint = dev[0][(0,0)][0]
        dev.set_configuration()
        usb.util.claim_interface(dev, interface)
        dev.write(endpoint, data.encode(), 1000)
        usb.util.release_interface(dev, interface)
    except usb.core.USBError as e:
        print("USB error:", e)

def main():
    # Vendor ID (VID) and Product ID (PID) of the USB printer
    vid = 0x04b8  
    pid = 0x0E15  
    try:
        printer = find_printer(vid, pid)       
        send_to_printer(printer, "Hello World")
        print("Printed successfully.")
    except ValueError as e:
        print(e)

if __name__ == "__main__":
    main()
I can't seem to fix or trigger the error reliably. What could I be missing?

DS256
Posts: 425
Joined: Mon Jul 26, 2021 7:44 pm

Re: Getting intermittent errno 16 "resource busy" with USB printer

Wed Feb 21, 2024 1:00 pm

I suspect that this is an condition being reported from the state of the printer if it is intermittent. I've seen this with a number of attached devices. What I've done in this case is:
  • Wrap the command in a while loop testing for maximum number of retries
  • Put the command in a try/except
  • In the Except test for intermittent errors. Sleep. Increment number of retries

Wimbley
Posts: 4
Joined: Mon Oct 09, 2023 1:57 pm

Re: Getting intermittent errno 16 "resource busy" with USB printer

Sat Mar 02, 2024 12:40 am

Thanks for the advice. It's still acting unreliably, but I'll leave in the while loop since it seems like good practice.

Return to “Python”