Search code examples
pythonprintingraspberry-piusbraspbian

How to fix the usb.core.USBError of thermal printer connected to raspberry pi using USB port


I'm having a problem in my thermal printer, I can't print but it's working. I'm using esc-pos python module https://pythonhosted.org/python-printer-escpos/modules/escpos.html

this is my code

from escpos.connections import getUSBPrinter
printer = getUSBPrinter()(idVendor=0x0fe6, idProduct=0x811e, interface=98)
printer.text("Hello World")
printer.lf()

here's the error I encountered.

Traceback (most recent call last): File "/home/pi/try thermal.py", line 4, in printer = getUSBPrinter()(idVendor=0x0fe6, idProduct=0x811e, interface=98) File "/usr/local/lib/python3.7/dist-packages/escpos/connections.py", line 87, in init self.open() File "/usr/local/lib/python3.7/dist-packages/escpos/connections.py", line 106, in __open if self._device.is_kernel_driver_active(0): File "/usr/local/lib/python3.7/dist-packages/usb/core.py", line 1061, in is_kernel_driver_active self._ctx.managed_open() File "/usr/local/lib/python3.7/dist-packages/usb/core.py", line 102, in wrapper return f(self, *args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/usb/core.py", line 120, in managed_open self.handle = self.backend.open_device(self.dev) File "/usr/local/lib/python3.7/dist-packages/usb/backend/libusb1.py", line 786, in open_device return _DeviceHandle(dev) File "/usr/local/lib/python3.7/dist-packages/usb/backend/libusb1.py", line 643, in __init _check(_lib.libusb_open(self.devid, byref(self.handle))) File "/usr/local/lib/python3.7/dist-packages/usb/backend/libusb1.py", line 595, in _check raise USBError(_strerror(ret), ret, _libusb_errno[ret]) usb.core.USBError: [Errno 13] Access denied (insufficient permissions)

The Thermal printer I'm using is, https://www.dfrobot.com/product-1799.html https://www.adafruit.com/product/2751

I hope you guys can help me, I badly needed this on thesis and I'm only new to raspberry-pi.


Solution

  • Your Linux user doesn't have access to the USB device, so you need to create an udev rule to grant access.

    Create a file printer.rules with the following contents:

    SUBSYSTEM=="usb", ATTRS{idVendor}=="0fe6", ATTRS{idProduct}=="811e", MODE="0666"
    

    And copy it into the rules directory via sudo:

    sudo cp printer.rules /etc/udev/rules.d
    

    Then reload the udev rules:

    udevadm control --reload-rules && udevadm trigger
    

    Now, every user should have access to the printer.