如何修复使用 USB 端口连接到 raspberry pi 的热敏打印机的 usb.core.USBError

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

我的热敏打印机出现问题,无法打印,但可以正常使用。 我正在使用 esc-pos python 模块 https://pythonhosted.org/python-printer-escpos/modules/escpos.html

这是我的代码

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

这是我遇到的错误。

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)

我使用的热敏打印机是,https://www.dfrobot.com/product-1799.html https://www.adafruit.com/product/2751

我希望你们能帮助我,我在论文中非常需要这个而且我只是 raspberry-pi 的新手。

您的 Linux 用户无权访问 USB 设备,因此您需要创建一个 udev 规则来授予访问权限。

使用以下内容创建一个文件printer.rules

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

并通过sudo:

复制到规则目录
sudo cp printer.rules /etc/udev/rules.d

然后重新加载 udev 规则:

udevadm control --reload-rules && udevadm trigger

现在,每个用户都应该可以访问打印机。