-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
可运行的版本
- Loading branch information
0 parents
commit 8ea80fa
Showing
5 changed files
with
598 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
chn-escpos | ||
= | ||
|
||
## 介绍 | ||
|
||
chn-escpos是一个nodejs的window POS机打印程序,使用`printer`的C组件与打印机通信,使用ESC/POS命令控制打印机 | ||
|
||
## 安装 | ||
|
||
``` | ||
npm install chn-escpos --save | ||
``` | ||
|
||
## 测试 | ||
你可以使用chn-escpos自带的测试程序进行测试 | ||
``` | ||
cd node_modules/chn-escpos | ||
npm test | ||
``` | ||
测试程序默认监听2520端口,你可以通过向`http://127.0.0.1:2520?action=print&printer=`POST raw数据进行打印测试(推荐使用POSTMAN进行测试) | ||
``` | ||
POST HTTP/1.1 | ||
Host: 127.0.0.1:2520?action=print&printer= | ||
Content-Type: application/javascript | ||
Cache-Control: no-cache | ||
Postman-Token: 3ae7055f-8dd9-5386-3cfe-4355d7b7645a | ||
[ | ||
{ | ||
"id": "1",//打印任务ID | ||
"group_id": "1",//打印队列ID | ||
"printer": "XP-80C",//打印机别名 | ||
"content": "<% setAlign:c %>测试居中" | ||
} | ||
] | ||
``` | ||
## 使用方法 | ||
``` | ||
var printer=require('chn-escpos'), | ||
printer_name='XP-80C'; | ||
new printer(printer_name,function(err,msg){ | ||
//调用this方法进行打印 | ||
}); | ||
``` | ||
### 打印方法 | ||
####text(text,[inline]) 打印文字内容 | ||
string `text`:打印内容,单行数据,如果超出自动换行 | ||
boolen `inline`:是否自动换行,如果为true,则不会自动换行 | ||
``` | ||
this.text('测试打印'); | ||
``` | ||
|
||
####line(number) 空行 | ||
number `number`:空行数 | ||
``` | ||
this.line(2); | ||
``` | ||
|
||
####setAlign(align) 设置对齐 | ||
string `align`:`C/L/R`分别代表居中/居左/居右,不区分大小写 | ||
``` | ||
this.setAlign('c').text('这个是居中文字'); | ||
``` | ||
|
||
###setLineheight(hex) 设置行高 | ||
string `hex`:16进制数字,如'\x05' | ||
``` | ||
this.setLineheight('\x05'); | ||
``` | ||
|
||
###setStyle(type) 设置样式 | ||
string `type`:`B/U/U2/BU/BU2`分别代表加粗/下划线/下划线样式2/加粗+下划线/加粗+下划线样式2,不区分大小写 | ||
``` | ||
this.setStyle('b').text('加粗'); | ||
``` | ||
|
||
###setSize(size) 设置文字大小 | ||
number `type`:2/1/null,2代表2倍字体,1/null均为正常 | ||
``` | ||
this.setSize(2).text('大字体'); | ||
``` | ||
|
||
###compile(string) 编译 | ||
string `string`:编译整个字符串 | ||
使用<% 方法名:[参数] %>进行快速设置`\n`或`\r`表示换行. | ||
``` | ||
this.compile('<% setAlign:c %><% setSize:2 %>这里开始是放大\n<% setSize:1 %>恢复正常大小'); | ||
``` | ||
|
||
###print(callback) 打印当前内容 | ||
function `callback`:回传err以及msg,当成功时,err为null | ||
``` | ||
this.print(function(err,msg){ | ||
if(err){ | ||
console.log('打印出错,回传信息:'); | ||
} | ||
console.log(msg); | ||
}); | ||
``` | ||
###empty() 清空当前内容 | ||
``` | ||
this.empty(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/** | ||
* 测试请求 | ||
*/ | ||
|
||
var url = require("url"); | ||
var querystring = require("querystring"); | ||
|
||
/** | ||
* 设置打印状态 | ||
* @param {Function} callback 回调函数,当打印成功后执行该函数 | ||
*/ | ||
function setPrinterStatus(callback){ | ||
this.return_status=[];//返回状态 | ||
this.return_list=[];//需返回的列表 | ||
if(callback){ | ||
this.callback=callback; | ||
} | ||
} | ||
setPrinterStatus.prototype={ | ||
/** | ||
* 增加状态列表 | ||
* @param {object} data 增加数据 | ||
* @return {number} 列表编号 | ||
*/ | ||
addList:function(data){ | ||
if(data){ | ||
this.return_list.push(data); | ||
}else{ | ||
this.return_list.push({}); | ||
} | ||
return this.return_list.length-1; | ||
}, | ||
/** | ||
* 更新列表 | ||
* @param {number} number 列表编号 | ||
* @param {object} data 更新数据 | ||
* @return {boolen} 是否已全部更新 | ||
*/ | ||
updateList:function(number,data){ | ||
this.return_status.push(1); | ||
for (var i in data) { | ||
if (data.hasOwnProperty(i)) { | ||
this.return_list[number][i] = data[i]; | ||
} | ||
} | ||
if(this.return_status.length===this.return_list.length){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
}, | ||
/** | ||
* 获取列表 | ||
* @return {object} 列表数据 | ||
*/ | ||
getList:function(){ | ||
return this.return_list; | ||
}, | ||
callEnd:function(){ | ||
if(this.callback){ | ||
this.callback.call(this); | ||
} | ||
} | ||
} | ||
|
||
//http请求响应 | ||
var app = require('http').createServer(function(req,res){ | ||
|
||
var objectUrl = url.parse(req.url); | ||
var objectQuery = querystring.parse(objectUrl.query); | ||
//接受数据 | ||
var chunk=''; | ||
req.on('data',function(data){ | ||
chunk+=data; | ||
}); | ||
//回传内容 | ||
req.on('end', function(){ | ||
var data={ | ||
status:0 | ||
} | ||
if(!objectQuery.action){ | ||
data.msg='没有指定动作'; | ||
} | ||
else if(chunk){ | ||
switch(objectQuery.action){ | ||
case 'print': | ||
var printer=require('./lib/printer.js'); | ||
var print_data=JSON.parse(chunk); | ||
//生成回调函数 | ||
var print_status=new setPrinterStatus(function(){ | ||
data={ | ||
status:1, | ||
msg:this.getList() | ||
} | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(data)); | ||
}); | ||
//执行批量打印 | ||
for(var i=0;i<print_data.length;i++){ | ||
var list_number=print_status.addList({ | ||
'id':print_data[i].id, | ||
'group_id':print_data[i].group_id | ||
}); | ||
//添加返回列表 | ||
new printer(print_data[i].printer,function(err,msg){ | ||
//查找打印机出错 | ||
if(err){ | ||
print_status.updateList(list_number,{ | ||
status:0, | ||
msg:msg+':'+err.toString() | ||
}); | ||
//查找打印机成功 | ||
}else{ | ||
//开始打印 | ||
this.compile(print_data[i].content).print(function(err,msg){ | ||
//打印失败 | ||
if(err){ | ||
print_status.updateList(list_number,{ | ||
status:0, | ||
msg:msg+':'+err.toString() | ||
}); | ||
//打印成功 | ||
}else{ | ||
print_status.updateList(list_number,{ | ||
status:1, | ||
msg:msg | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
print_status.callEnd(); | ||
break; | ||
default: | ||
data={ | ||
status:0, | ||
msg:'错误的动作' | ||
} | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(data)); | ||
} | ||
}else{ | ||
data.msg='没有数据'; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(data)); | ||
} | ||
}); | ||
}); | ||
app.listen(process.env.PORT || 2520,function(){ | ||
console.log('打印测试程序已运行,请参考github说明测试打印'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// SEE: https://www.sparkfun.com/datasheets/Components/General/Driver%20board.pdf | ||
// | ||
|
||
module.exports = { | ||
INITIAL_PRINTER : '\x1B\x40' , //Initial paper | ||
NEW_LINE : '\x0A' , //Add new line | ||
PAPER_CUTTING : '\x1d\x56\x41', //Cut paper | ||
LINE_HEIGHT : '\x1b\x32', //Normal line height | ||
LINE_HEIGHT_B : '\x1b\x33\x6e', //Normal line height large | ||
CHN_TEXT : '\x1b\x52\x0f', //CHN text | ||
// text style | ||
TXT_NORMAL : '\x1b\x21\x00', // Normal text | ||
TXT_2HEIGHT : '\x1d\x21\x01', // Double height text | ||
TXT_2WIDTH : '\x1d\x21\x10', // Double width text | ||
|
||
TXT_UNDERL_OFF : '\x1b\x2d\x00', // Underline font OFF | ||
TXT_UNDERL_ON : '\x1b\x2d\x01', // Underline font 1-dot ON | ||
TXT_UNDERL2_ON : '\x1b\x2d\x02', // Underline font 2-dot ON | ||
TXT_BOLD_OFF : '\x1b\x45\x00', // Bold font OFF | ||
TXT_BOLD_ON : '\x1b\x45\x01', // Bold font ON | ||
|
||
TXT_ALIGN_L : '\x1b\x61\x00', // Left justification | ||
TXT_ALIGN_C : '\x1b\x61\x01', // Centering | ||
TXT_ALIGN_R : '\x1b\x61\x02', // Right justification | ||
|
||
//字体 | ||
TXT_FONT_A : '\x1b\x4d\x00', // Font type A | ||
TXT_FONT_B : '\x1b\x4d\x01', // Font type B | ||
TXT_FONT_C : '\x1b\x4d\x02', // Font type C | ||
TXT_FONT_D : '\x1b\x4d\x48', // Font type D | ||
TXT_FONT_E : '\x1b\x4d\x31', // Font type E | ||
|
||
//barcode | ||
BARCODE_TXT_OFF : '\x1d\x48\x00' , // HRI barcode chars OFF | ||
BARCODE_TXT_ABV : '\x1d\x48\x01' , // HRI barcode chars above | ||
BARCODE_TXT_BLW : '\x1d\x48\x02' , // HRI barcode chars below | ||
BARCODE_TXT_BTH : '\x1d\x48\x03' , // HRI barcode chars both above and below | ||
|
||
BARCODE_FONT_A : '\x1d\x66\x00' , // Font type A for HRI barcode chars | ||
BARCODE_FONT_B : '\x1d\x66\x01' , // Font type B for HRI barcode chars | ||
|
||
BARCODE_HEIGHT : '\x1d\x68\x64' , // Barcode Height [1-255] | ||
BARCODE_WIDTH : '\x1d\x77\x03' , // Barcode Width [2-6] | ||
|
||
//一维码 | ||
BARCODE_UPC_A : '\x1d\x6b\x00' , // Barcode type UPC-A | ||
BARCODE_UPC_E : '\x1d\x6b\x01' , // Barcode type UPC-E | ||
BARCODE_EAN13 : '\x1d\x6b\x02' , // Barcode type EAN13 | ||
BARCODE_EAN8 : '\x1d\x6b\x03' , // Barcode type EAN8 | ||
BARCODE_CODE39 : '\x1d\x6b\x04' , // Barcode type CODE39 | ||
BARCODE_ITF : '\x1d\x6b\x05' , // Barcode type ITF | ||
BARCODE_NW7 : '\x1d\x6b\x06' , // Barcode type NW7 | ||
} |
Oops, something went wrong.