Driving a USB Thermal Printer with Linux/Raspberry Pi

Driving a USB Thermal Printer with Linux/Raspberry Pi

This is a demo program to print to the POS58 (aka ZJ-5890K) USB thermal receipt printer using Python under Linux (tested on a Raspberry Pi). This is printer is sold under different companies, but is made by Zjiang.

This has been tested on LinuxMint 18, a Raspberry Pi 3 B+ running Raspbian Stretch Lite, and a Raspberry Pi Zero W (pictured below).

The Zjian Linux Driver Has Problems

Their Linux driver has problems:

  1. Their installer connects the printer to the wrong port so it never prints. (You need to go into CUPS manager and fix the port path.)

  2. Their driver uses the printer in graphics mode and their font rendering looks really terrible for small text.

  3. Using CUPS adds unnecessarily complication, like adding left, right and bottom margins

Direct USB Printing

This demo program turns it into a very simple text printer by opening the USB port and writing directly to the USB endpoint of the printer device. You can include Epson ESC/POS sequences and graphics yourself to get fancy.

Download Demo Code

Download my demo program at my github site.

Linux Configuration

Special configurations are usually handled by an installation script. I offer no install script, but you can configure this stuff manually:

Add your user to the Linux group “lp” (line printer), otherwise you will get a user permission error when trying to print.

sudo addgroup <myusername> lp

Add a udev rule to allow all users to use a USB device that matches this vendor ID and product ID. Without this rul, you will get a permissions error.

Example: in /etc/udev/rules.d create a file ending in .rules, such as 33-receipt-printer.rules with the contents:

# Set permissions to let anyone use the thermal receipt printer
SUBSYSTEM=="usb", ATTR{idVendor}=="0416", ATTR{idProduct}=="5011", MODE="666"

Unplug and re-plug the USB connection of the printer to run the new udev rule.

Python setup

This code is mainly from the example code at https://github.com/pyusb/pyusb

See that site to install pyusb and a USB backend. This was tested using usblib 1.0. Quick reference:

Install pyusb to your Python (suggest using virtualenv):

pip install pyusb

Install libusb 1.0:

sudo apt install libusb-1.0-0-dev

And that’s it. Happy printing!

Vince's Picture

About Vince

Vince has loved to tinker since he was a kid. He became an engineer so he can play all the time.

California, USA https://www.facebook.com/vince.patron

Comments