lsongdev / node-escpos

🖨️ ESC/POS Printer driver for Node.js
https://npmjs.org/escpos
Other
1.38k stars 423 forks source link

Find usb device by path? #354

Closed peteruithoven closed 2 years ago

peteruithoven commented 3 years ago

We have 3 printers connected, and we've used udev rules based on serial numbers to create handy symlinks (e.g. /dev/leg1-printer). But how do we use these paths to connect to a specific printer, while using the usb protocol?

Currently we've found a workaround by using node-usb, opening the device, and reading the serial number. But opening the device to read the serial adds extra permission challenges for example.

zakaria-chahboun commented 3 years ago
const escpos = require("escpos");
// install escpos-usb adapter module manually
escpos.USB = require("escpos-usb");
// Select the adapter based on your printer type
const device  = new escpos.Serial('/dev/usb/lp0');
// encoding is optional
const options = { encoding: "GB18030" /* default */ };

const printer = new escpos.Printer(device, options);

on Linux you have to give the USB permission to access to it without sudo:

Add a new file in: /etc/udev/rules.d named usb.rules It should have the following contents: SUBSYSTEM=="usb", MODE="0666" You can do this in terminal quickly: sudo nano /etc/udev/rules.d/usb.rules

Reload rules: sudo udevadm control --reload

This will make all USB devices readable and writable by all users.