linux下使用小票打印

linux下使用小票打印

打印机: Xprinter XP-58IIH
指令支持: ESC/POS
接口: USB, 蓝牙

Linux系统: Centos7

蓝牙配对很快, 配对好后就是连接状态. 但很快变为断开, 且连接灰显, 无法点击.
USB连接后, 设备下添加找不到该打印机.

命令可以访问.
# dmesg |grep usb    可以找到这个usb设备.
[27129.204196] usb 5-1: USB disconnect, device number 2
[27131.204164] usb 4-1: USB disconnect, device number 23
[27131.204810] usblp0: removed
[27131.661125] usb 4-1: new full-speed USB device number 24 using uhci_hcd
[27132.175092] usb 4-1: device not accepting address 24, error -71
[27132.885079] usb 4-1: new full-speed USB device number 26 using uhci_hcd
[27133.410080] usb 4-1: new full-speed USB device number 27 using uhci_hcd
[27146.910034] usb 4-1: new full-speed USB device number 28 using uhci_hcd
[27147.063419] usb 4-1: New USB device found, idVendor=0483, idProduct=070b
[27147.063428] usb 4-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[27147.063433] usb 4-1: Product: USB Printing Support
[27147.063439] usb 4-1: Manufacturer: Printer-58
[27147.063443] usb 4-1: SerialNumber: ÿ
[27147.070697] usblp 4-1:1.0: usblp0: USB Bidirectional printer dev 28 if 0 alt 0 proto 2 vid 0x0483 pid 0x070B


# ls /dev/usb/
hiddev0 lp0
这里找到的lp0就是打印机了.

# stat /dev/usb/lp0 可以查看状态.
文件:"/dev/usb/lp0"
大小:0 块:0 IO 块:4096 字符特殊文件
设备:5h/5d Inode:362047 硬链接:1 设备类型:b4,0
权限:(0660/crw-rw----) Uid:( 0/ root) Gid:( 7/ lp)
环境:system_u:object_r:printer_device_t:s0
最近访问:2018-11-30 19:29:30.747413683 +0800


# echo "Hello" > /dev/usb/lp0
尝试打印.
# echo "$(date)" > /dev/usb/lp0

$ echo "Hello" > /dev/usb/lp0
bash: /dev/usb/lp: Permission denied

cat group |grep lp    // 查看用户组信息
sudo cat gshadow
sudo gpasswd -a toma lp   //将当前用户toma添加到用户组 lp 里
sudo cat gshadow
cat group |grep lp
echo "Hello" > /dev/usb/lp0
sudo echo "Hello" > /dev/usb/lp0

将用户添加到 lp 组,再试,若仍然有问题,重启后再试。

理论是不用安装驱动就可以通过这种方式打印,至少打印英文和数字。如下有使用脚本按格式打印的参考。
https://mike42.me/blog/2015-03-getting-a-usb-receipt-printer-working-on-linux
https://github.com/mike42/escpos-php

=============

https://mrchi.cc/p/ea7c782e774e508f88fb16023ed51c7e/
使用Python操作ESCPOS协议热敏打印机  2017-10-30

硬件:芯烨(Xprinter) XP-58IIQ USB接口热敏打印机,兼容ESC/POS打印协议;
软件:python-escpos==3.0a3,是一个alpha测试版本,接口在将来可能会变动。
pip install python-escpos
如果在树莓派上使用,需要先安装 libjpeg8-dev 包
sudo apt-get install libjpeg8-dev

把当前用户加入到lp组,使其能够访问打印机
sudo usermod -a -G lp pi

定义打印机实例
所有的打印机类都定义于escpos.printer文件中。

USB打印机
class escpos.printer.Usb(idVendor, idProduct, timeout=0, in_ep=130, out_ep=1, *args, **kwargs)

在创建打印机实例之前,你需要获取一些打印机的参数。

使用lsusb命令,在输出中得到VendorID和Product ID,它们的格式是xxxx:xxxx,位置在设备名之前。
$ lsusb
Bus 005 Device 011: ID 0483:070b STMicroelectronics

根据Vendor ID和Product ID,可以得到“EndPoint”地址。

# lsusb -vvv -d 0483:070b | grep bEndpointAddress
bEndpointAddress 0x81 EP 1 IN
bEndpointAddress 0x02 EP 2 OUT

$ lsusb -vvv -d 0483:070b | grep bEndpointAddress
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
bEndpointAddress 0x81 EP 1 IN
bEndpointAddress 0x02 EP 2 OUT
得到:“EndPoint”地址IN方向为0x81,OUT方向为0x02。
用这些参数可以新建一个Usb类实例。timeout参数表示等待USB接口超时时间,默认为0。
from escpos import printer
p = printer.Usb(0x1a2b, 0x1a2b, timeout=0, in_ep=0x81, out_ep=0x02)
该类使用pyusb和libusb与USB接口打印机通信,不适用于USB转串口适配器设备,只适用于原生USB驱动。

ESC/POS API详解
所有的打印机类都继承自escpos.escpos.Escpos,该Escpos抽象基类定义了打印时的各种方法。
先来个demo吧:
from escpos.printer import Usb
p = Usb(0x0483, 0x070b, 0, 0x81, 0x02)
p.hw('INIT')
p.textln('Hello, world!')
p.image('mafengwo.png')
p.set(align='center')
p.qr('https://i.senguo.cc', size=7,)
p.barcode('9787111436737', 'EAN13')
p.close()

====================

更快找到并识别USB设备的办法: 

$ dmesg --color=always | tail
---------- //拔掉USB线:----------
[255085.496255] usb 4-1: USB disconnect, device number 7
[255085.496447] usblp1: removed
---------- //插回USB线:----------
[255106.739497] usb 4-1: new full-speed USB device number 8 using uhci_hcd
[255106.922452] usb 4-1: New USB device found, idVendor=0483, idProduct=070b, bcdDevice= 2.54
[255106.922455] usb 4-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[255106.922458] usb 4-1: Product: USB Printing Support
[255106.922459] usb 4-1: Manufacturer: Printer-58
[255106.922461] usb 4-1: SerialNumber: ÿ
[255106.929662] usblp 4-1:1.0: usblp1: USB Bidirectional printer dev 8 if 0 alt 0 proto 2 vid 0x0483 pid 0x070B
$

==============


方式二:需要安装cups服务,需要打印机的ppd文件。

 ppd文件:数据库生成的PPD文件包含有关重要打印机功能,可用选项以及如何构建渲染器(通常为Ghostscript)命令行的所有信息,具体取决于用户对选项的选择。

参见:https://www.cnblogs.com/sztom/p/10686925.html

----------------------------

====================

https://github.com/klirichek/zj-58
Zj-58, Zj-80 and other receipt printers
CUPS filter for cheap thermal receipt printers as Zijiang ZJ-58, XPrinter XP-58, JZ-80 with cutter, Epson TM-T20, and may be any other printers understanding ESC/POS commands.
Originally it was reverse-engineered filter for Zijiang zj-58 with it's specific PPD, but later it is revealed that it actually works with many other cheap 58mm printers, like Xprinter XP-58.
https://github.com/klirichek/zj-58/blob/master/zj58.ppd

==========

原文地址:https://www.cnblogs.com/sztom/p/10046157.html