I have three usb receipt printers from the same vendor (thus the same usb vendor and product id) plugged into the same server and I need to be able to issue separate printing commands to each of them. The printers register themselves to /dev/usb/lp* so I can just write ESC/POS commands into those files and the printers print the appropriate receipts. The issue however is that upon reboot sometimes the order changes, for instance /dev/usb/lp0 now refers to the printer that was /dev/usb/lp1 before. Is there a way to assign a per-device id or at least per usb port id so that it will always refer to the same printer?
-
What OS you are on ? I guess linux as you state /dev/ device, but it would be nice to know the OS/version– yagmoth555 ♦Feb 21, 2019 at 18:11
-
@yagmoth555 Ubuntu, kernel version 4.18.0-15-generic– user1494162Feb 21, 2019 at 18:14
-
for the future you might want to add the exact ubuntu version as well, not just "ubuntu", there are ~40 or so versions, though the kernel info limits the possibility quite much.– Dennis NolteFeb 21, 2019 at 21:45
-
18.10, but it doesn't matter in this case– user1494162Feb 21, 2019 at 23:11
2 Answers
I found an ugly solution, if you have a more elegant solution please post your answer.
I wrote a script that uses a regex to parse the output of dmesg | grep usblp | grep 0x0416
(0x0416 is the vendor id for my usb printers).
The output looks as follows:
[ 9.875206] usblp 2-2:1.0: usblp0: USB Bidirectional printer dev 2 if 0 alt 0 proto 2 vid 0x0416 pid 0x5011
[ 9.879408] usblp 3-1:1.0: usblp1: USB Bidirectional printer dev 2 if 0 alt 0 proto 2 vid 0x0416 pid 0x5011
[ 9.883841] usblp 3-2:1.0: usblp2: USB Bidirectional printer dev 3 if 0 alt 0 proto 2 vid 0x0416 pid 0x5011
I use a simple regex: /usblp ([0-9]\-[0-9])\:1\.0\: usblp([0-9])/
to isolate the usb port (2-2, 3-1 or 3-2) with it's corresponding usblp id, then I just have an hashmap that maps each usb port into a friendly name.
It's ugly, it requires parsing dmesg, but it seems to work so far.
-
do the printer have serial numbers you can read out via usb? there was(is?) a tool named usb-devices and a virtual file in /proc/ somewhere which should be able to do that. additionally lsusb -v might already help you at least get you better info additionally udev should be able to distinquish between the physical port you attach. so a udev rule which defines the name based on the port should in theory work. Feb 21, 2019 at 21:43
-
usb-devices doesn't seem to report any information related to this Feb 21, 2019 at 23:10
Create a symlink I did it on one of my machines in Ubuntu 22.04 5.19.0-32-generic and I was able to echo to the symlink to print.
#! /usr/bin/bash
## checks for symlink and removes it if needed
if [ -L /dev/usb/lp0 ]; then
rm /dev/usb/lp0
fi
# create symlink if it is /dev/usb/lp0 is not present to link printer to /dev/usb/lp0
if ! [ -f /dev/usb/lp0 ]; then
prport=$(ls -l /dev/usb/lp* |awk '{print $10}')
ln -s $prport /dev/usb/lp0
fi