Skip to content

Instantly share code, notes, and snippets.

@elsholz
Last active April 30, 2025 20:38
Show Gist options
  • Save elsholz/44643ede809a6c4a3875beaae26bff2f to your computer and use it in GitHub Desktop.
Save elsholz/44643ede809a6c4a3875beaae26bff2f to your computer and use it in GitHub Desktop.
Find out the values you need to know to connect a thermal printer via USB from a python script.

Find EP-Ids and Device ID of Thermal Printer by name

When using the Python Library python-escpos to use a thermal printer via usb, you need the device id, vendor id, endpoint out id, and endpoint in id.

Finding these values took me some time, so here is a script that automates the process.

All you need to supply to the script is the name of the printer device. Find it out by running lsusb with the printer disconnected. Then connect the printer and run lsusb again. Look out for the device that has been added and remember its name (or some part of it, doesn't really matter).

Run ./get_thermal_printer_info.sh "device name" and there you'll find the values you need to create the escpos.printer.Usb object. Your Python code might look something like this:

from escpos.printer import Usb

printer = Usb(0x0416, 0x5011, in_ep=0x81, out_ep=0x03)
# ... do something with printer like `printer.Text("It works!")`
#!/bin/bash
device=$(lsusb | grep $1)
echo Device: "$device"
vendor_id=$(echo "$device" | sed -n "s/.*ID \([0-9]\{4\}\).*/\1/p")
device_id=$(echo "$device" | sed -n "s/.*ID [0-9]\{4\}:\([0-9]\{4\}\).*/\1/p")
bus=$(echo "$device" | sed "s/.*Bus \([0-9]\{3\}\) .*/\1/")
devnum=$(echo "$device" | sed "s/.*Device \([0-9]\{3\}\):.*/\1/")
echo VendorID: "0x$vendor_id"
echo DeviceID: "0x$device_id"
echo Bus: "0x$bus"
echo DevNum: "0x$devnum"
echo
devinfo=$(lsusb -s $bus:$devnum -v)
ep_in=$(echo $devinfo | sed -n "s/.*bEndpointAddress.*\([0-9]\{2\}\).*EP.*IN.*/\1/p")
ep_out=$(echo $devinfo | sed -n "s/.*bEndpointAddress.*\([0-9]\{2\}\).*EP.*OUT.*/\1/p")
echo EndpointIN: "0x$ep_in"
echo EndpointOUT: "0x$ep_out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment