add printer func

This commit is contained in:
Jay 2017-08-14 23:17:03 +08:00
parent 31a2bc9b76
commit 32af5548e6
3 changed files with 27 additions and 1 deletions

View File

@ -8,5 +8,8 @@
"data":"00000002-8f05-4e30-beb0-6fa7ea3dfc3e"
}
}
},
"printer": {
"serial": "/dev/ttyUSB0"
}
}

View File

@ -1,5 +1,13 @@
const bleno = require('bleno')
const config = require('./config.json')
const escpos = require('escpos')
const device = escpos.Serial(config.printer.serial)
const printer = escpos.Printer(device)
let isOpen = false
device.open(()=>{
isOpen = true
})
var tmp = []
@ -26,7 +34,8 @@ class DataCharacteristic extends bleno.Characteristic {
console.log(data[0])
} else if (data.length == 1 && data[0] == 0xff) {
//console.log(tmp)
console.log(new Buffer(tmp).toString())
console.log(Buffer.from(tmp).toString())
printString(Buffer.from(tmp).toString())
} else {
tmp = [...tmp, ...data.slice(2, data.length)]
}
@ -37,4 +46,17 @@ class DataCharacteristic extends bleno.Characteristic {
}
}
function printString(str){
if(!isOpen) return
let strs = str.split(/\n/)
printer.font('a')
.align('ct')
.size(1,1)
for(let i in strs){
printer.text(strs[i])
}
printer.cut(true, 8)
}
module.exports = DataCharacteristic

View File

@ -5,6 +5,7 @@
"license": "MIT",
"dependencies": {
"bleno": "^0.4.2",
"escpos": "^2.4.3",
"uuid": "^3.1.0"
}
}