首页 > 解决方案 > 如何使用 NodeJS 和 ESC/POS 在蓝牙(票务)打印机上打印

问题描述

对于一个项目,我想使用 NodeJS 在支持 ESC/POS 的蓝牙打印机上打印票证。我想使用一个提供蓝牙打印的 npm 包escpos (根据示例)。

该文档提到了一个蓝牙构造函数:

const bluetoothDevice = new escpos.Bluetooth('01:23:45:67:89:AB', 1);
const bluetoothPrinter = new escpos.Printer(bluetoothDevice);

但是当我尝试这样做时,它说没有蓝牙构造函数。

看起来它需要escpos-bluetooth包才能连接到蓝牙设备。但是,尝试安装它会导致错误:

npm ERR! code 1
npm ERR! path /home/pi/Code/NodeJS/Nodebox/node_modules/node-bluetooth
npm ERR! command failed
npm ERR! command sh -c node-gyp configure build
npm ERR! make: Entering directory '/home/pi/Code/NodeJS/Nodebox/node_modules/node-bluetooth/build'
npm ERR!   CXX(target) Release/obj.target/BluetoothSerialPort/src/linux/BluetoothSerialPort.o
npm ERR! make: Leaving directory '/home/pi/Code/NodeJS/Nodebox/node_modules/node-bluetooth/build'
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.2.0
npm ERR! gyp info using node@16.13.0 | linux | arm
npm ERR! gyp info find Python using Python version 3.7.3 found at "/usr/bin/python3"
npm ERR! gyp info spawn /usr/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   '/home/pi/.nvm/versions/node/v16.13.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args   'binding.gyp',
npm ERR! gyp info spawn args   '-f',
npm ERR! gyp info spawn args   'make',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/home/pi/Code/NodeJS/Nodebox/node_modules/node-bluetooth/build/config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/home/pi/.nvm/versions/node/v16.13.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/home/pi/.cache/node-gyp/16.13.0/include/node/common.gypi',
npm ERR! gyp info spawn args   '-Dlibrary=shared_library',
npm ERR! gyp info spawn args   '-Dvisibility=default',
npm ERR! gyp info spawn args   '-Dnode_root_dir=/home/pi/.cache/node-gyp/16.13.0',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=/home/pi/.nvm/versions/node/v16.13.0/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=/home/pi/.cache/node-gyp/16.13.0/<(target_arch)/node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=/home/pi/Code/NodeJS/Nodebox/node_modules/node-bluetooth',
npm ERR! gyp info spawn args   '-Dnode_engine=v8',
npm ERR! gyp info spawn args   '--depth=.',
npm ERR! gyp info spawn args   '--no-parallel',
npm ERR! gyp info spawn args   '--generator-output',
npm ERR! gyp info spawn args   'build',
npm ERR! gyp info spawn args   '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! gyp info spawn make
npm ERR! gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
npm ERR! ../src/linux/BluetoothSerialPort.cc:18:14: error: variable or field ‘InitAll’ declared void
npm ERR!  void InitAll(Handle<Object> exports) {
npm ERR!               ^~~~~~
npm ERR! ../src/linux/BluetoothSerialPort.cc:18:14: error: ‘Handle’ was not declared in this scope
npm ERR! ../src/linux/BluetoothSerialPort.cc:18:14: note: suggested alternative: ‘rand_r’
npm ERR!  void InitAll(Handle<Object> exports) {
npm ERR!               ^~~~~~
npm ERR!               rand_r
npm ERR! ../src/linux/BluetoothSerialPort.cc:18:27: error: expected primary-expression before ‘&gt;’ token
npm ERR!  void InitAll(Handle<Object> exports) {
npm ERR!                            ^
npm ERR! ../src/linux/BluetoothSerialPort.cc:18:29: error: ‘exports’ was not declared in this scope
npm ERR!  void InitAll(Handle<Object> exports) {
npm ERR!                              ^~~~~~~
npm ERR! In file included from ../src/linux/BluetoothSerialPort.cc:12:
npm ERR! ../src/linux/BluetoothSerialPort.cc:23:34: error: ‘InitAll’ was not declared in this scope
npm ERR!  NODE_MODULE(BluetoothSerialPort, InitAll)
npm ERR!                                   ^~~~~~~
npm ERR! /home/pi/.cache/node-gyp/16.13.0/include/node/node.h:821:36: note: in definition of macro ‘NODE_MODULE_X’
npm ERR!        (node::addon_register_func) (regfunc),                          \
npm ERR!                                     ^~~~~~~
npm ERR! ../src/linux/BluetoothSerialPort.cc:23:1: note: in expansion of macro ‘NODE_MODULE’
npm ERR!  NODE_MODULE(BluetoothSerialPort, InitAll)
npm ERR!  ^~~~~~~~~~~
npm ERR! make: *** [BluetoothSerialPort.target.mk:117: Release/obj.target/BluetoothSerialPort/src/linux/BluetoothSerialPort.o] Error 1
npm ERR! gyp ERR! build error 
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm ERR! gyp ERR! stack     at ChildProcess.onExit (/home/pi/.nvm/versions/node/v16.13.0/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
npm ERR! gyp ERR! System Linux 5.10.63-v7+
npm ERR! gyp ERR! command "/home/pi/.nvm/versions/node/v16.13.0/bin/node" "/home/pi/.nvm/versions/node/v16.13.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "build"
npm ERR! gyp ERR! cwd /home/pi/Code/NodeJS/Nodebox/node_modules/node-bluetooth
npm ERR! gyp ERR! node -v v16.13.0
npm ERR! gyp ERR! node-gyp -v v8.2.0
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/pi/.npm/_logs/2021-11-04T10_36_49_644Z-debug.log

第二种选择是尝试将蓝牙打印机设置为串行设备,并使用Serialescpos 的构造函数进行连接。因此我安装了escpos-serialport包。我还安装了一些软件包:

sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev

并使用 rfcomm 使用它的 MAC 地址将蓝牙打印机绑定到/dev/rfcomm0使用这个答案

sudo rfcomm bind 0 "57:4C:54:02:03:D5"

但是,我是否安装escpos-serialport包都没有关系,有一个错误:

TypeError: escpos.Serial is not a constructor

检查源代码,将 Serial 定义为一个类:

export class Serial implements Adapter {
...
}

所以主要问题是,如何使用 NodeJS 和 ESC/POS 命令集连接并打印到蓝牙打印机?我已经使用 Python 让它工作了,但我在 NodeJS 中需要它。

标签: node.jsbluetoothbluetooth-printing

解决方案


推荐阅读