From 2462c92c9b67a09d37b45bd89e9deebaf7111669 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 24 Aug 2017 10:51:22 +0800 Subject: [PATCH] add iconv module and change print method --- PrinterDev.js | 11 +++++++++-- package.json | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/PrinterDev.js b/PrinterDev.js index 73a180d..cf40a1e 100644 --- a/PrinterDev.js +++ b/PrinterDev.js @@ -2,7 +2,7 @@ const EventEmitter = require('events') const escpos = require('escpos') const config = require('./config.json') const fs = require('fs') - +const iconv = require('iconv-lite') // const device = new escpos.Serial(config.printer.serial) // const printer = new escpos.Printer(device) @@ -16,6 +16,11 @@ class PrinterDevice { this._type = null // type = serial or console } + encodeStr(str){ + if(!str || typeof str != 'string') return false + return iconv.encode(str, 'big5') + } + async connect() { let chkSerial = await new Promise((resolve, reject) => { fs.access(config.printer.serial, err => { @@ -66,11 +71,13 @@ class PrinterDevice { let size = tmp.substring(2) let sarr = size.trim().split(',') if (sarr != 2) break + if(!isFinite(sarr[0]) || !isFinite(sarr[1])) break + sarr = sarr.map(t => Math.floor(t)) this._printer.size(sarr[0], sarr[1]) break } } else { - this._printer.text(tmp, 'big5') + this._printer.print(this.encodeStr(tmp)) } } diff --git a/package.json b/package.json index ac0c481..3ff193f 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "dependencies": { "bleno": "^0.4.2", "escpos": "^2.4.3", + "iconv-lite": "^0.4.18", "uuid": "^3.1.0" } }