add iconv module and change print method

This commit is contained in:
Jay 2017-08-24 10:51:22 +08:00
parent 748c021ff2
commit 2462c92c9b
2 changed files with 10 additions and 2 deletions

View File

@ -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))
}
}

View File

@ -6,6 +6,7 @@
"dependencies": {
"bleno": "^0.4.2",
"escpos": "^2.4.3",
"iconv-lite": "^0.4.18",
"uuid": "^3.1.0"
}
}