A ESC/POS library for node
$ npm install --save nposParse ESC/POS binary buffer.
varnpos=require('npos');varparser=npos.parser();varbuffer=fs.readFileSync('sample.bin');parser.parse(buffer).then(function(ast){console.log(ast);});The ast form parse is like:
{
raw: <Buffer ...>,
error: <error>,
tree: [
{
code: 27, // command code in decimal hex: '1B', // command code in hex
ascii: 'ESC', // command code in ascii name
offset: 0, // data offset from buffer
length: 2, // data length fn: '@', // command fn charater
},
....
],
entries: [ // decoded entries
{
type: 'raster',
nodes: [ // nodes handled by codec from ast
...
],
data: <Object> // data decoded by codec
},
...
]
}
rules is used to parse esc/pos binary to ast tree.
Example rules:
varnpos=require('npos');varrules={};rules[npos.ESC]={// 'ESC' commands'!': 1,// 'ESC !' command. Swallow one parameter byte'$': 2,'%': 1,'&': [0,ycc],// 'ESC &' command. Using 'ycc' function to decode from offset 0'*': [0,'bitimage'],// 'ESC *' command. Using builtin decoder to decode from offset 0.'-': 1,'2': 0,'3': 1,'=': 1,'?': 1,'@': 0,'D': [0,'escd'],'E': 1,'G': 1,'J': 1,'L': 0,'M': 1,'R': 1,'S': 0,'T': 1,'V': 1,'W': 8,'\\': 2,'a': 1,'c': 2,'d': 1,'p': 3,'r': 0,// Select print color't': 1,'{': 1,'B': 2,// Unknown Command'Z': [3,'d16']};// Custom splitter with parameter buffer and offset specified with rule.functionycc(buf,offset){if(buf.length-offset<3){returnbuf.length;}vark=buf[offset+2]-buf[offset+1]+1;varnum=3;for(vari=0;i<k;i++){num+=buf[offset+num]*buf[offset]+1;}returnnum;};or extend the builtin rules:
varnpos=require('npos');npos.rules[npos.ESC]['&']=[0,ycc];Here are builtin rules and builtin splitters.
npos use codec to decode from ast tree parsed from binary.
Here are builtin codecs
We can extend codecs as need. For example:
// sampleCodec.js// commands supportedexports.commands=['GSv0'];// decode from raw data and nodesexports.decode=function(raw,nodes){// traverse nodes to decode and return result decoded}varnpos=require('npos');npos.codecs['sampleCodec']=require('./sampleCodec');See: example
© taoyuan