Skip to content

Commit bcd07f9

Browse files
committed
Add zpl printer status support
1 parent 10af4e3 commit bcd07f9

4 files changed

Lines changed: 225 additions & 21 deletions

File tree

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
class EscposCommands {
22
constructor(configs) {
3-
this.getStatusCommand = "\u0010\u0004\x01";
4-
this.getOfflineCauseCommand = "\u0010\u0004\x02";
5-
this.getErrorCauseCommand = "\u0010\u0004\x03";
6-
this.getRollPaperStatusCommand = "\u0010\u0004\x04";
73
this.configs = configs;
4+
this.commands = {
5+
getStatusCommand: "\u0010\u0004\x01",
6+
getOfflineCauseCommand: "\u0010\u0004\x02",
7+
getErrorCauseCommand: "\u0010\u0004\x02",
8+
getRollPaperStatusCommand: "\u0010\u0004\x04",
9+
};
10+
}
11+
12+
matchCommand(data) {
13+
return Object.values(this.commands).includes(data);
14+
}
15+
16+
getResponse(data) {
17+
switch (data) {
18+
case this.commands.getStatusCommand:
19+
return Buffer.from(this.getEscposStatus());
20+
case this.commands.getOfflineCauseCommand:
21+
return Buffer.from(this.getOfflineCause());
22+
case this.commands.getErrorCauseCommand:
23+
return Buffer.from(this.getErrorCause());
24+
case this.commands.getRollPaperStatusCommand:
25+
return Buffer.from(this.getRollPaperStatus());
26+
}
827
}
928

1029
/**

ZplEscPrinter/js/main.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let clientSocketInfo;
99
let server;
1010
let configs = {};
1111
const escposCommands = new EscposCommands(configs);
12+
const zplCommands = new ZplCommands(configs);
1213

1314
const defaults = {
1415
isZpl: true,
@@ -25,6 +26,14 @@ const defaults = {
2526
filetype: '3',
2627
path: null,
2728
counter: 0,
29+
zplHeadOpen: false,
30+
zplPaperOut: false,
31+
zplRibbonOut: false,
32+
zplCutterFault: false,
33+
zplHeadTooHot: false,
34+
zplMotorOverheat: false,
35+
zplPrinterPaused: false,
36+
zplPaperJam: false,
2837
escposOnline: true,
2938
escposPaperFeedPressed: false,
3039
escposCoverOpen: false,
@@ -126,6 +135,13 @@ function notify(text, glyphicon, type, delay) {
126135
async function zpl(data){
127136
data = data.toString('utf8');
128137
try{ data = base64DecodeUnicode(data.trim()); }catch(e){}
138+
139+
if (zplCommands.matchCommand(data)) {
140+
console.log('Command: ' + zplCommands.getResponse(data).toString('utf8'));
141+
notify('A response has been sent to the received internal command.');
142+
return zplCommands.getResponse(data);
143+
}
144+
129145
const zpls = data.split(/\^XZ|\^xz/);
130146
const factor = configs.unit === '1' ? 1 : (configs.unit === '2' ? 2.54 : (configs.unit === '3' ? 25.4 : 96.5));
131147
const width = Math.round(parseFloat(configs.width) * 1000 / factor) / 1000;
@@ -163,6 +179,7 @@ async function zpl(data){
163179
await saveLabel(zpl, "raw", counter);
164180
}
165181
}
182+
return null;
166183
}
167184

168185
/**
@@ -175,14 +192,10 @@ async function escpos(data,b64){
175192
let dataAux = data.toString('utf8');
176193
try{ dataAux = base64DecodeUnicode(dataAux.trim()); b64=true; }catch(e){}
177194

178-
if (dataAux === escposCommands.getStatusCommand) {
179-
return Buffer.from(escposCommands.getEscposStatus());
180-
} else if (dataAux === escposCommands.getOfflineCauseCommand) {
181-
return Buffer.from(escposCommands.getOfflineCause())
182-
} else if (dataAux === escposCommands.getErrorCauseCommand) {
183-
return Buffer.from(escposCommands.getErrorCause())
184-
} else if (dataAux === escposCommands.getRollPaperStatusCommand) {
185-
return Buffer.from(escposCommands.getRollPaperStatus())
195+
if (escposCommands.matchCommand(dataAux)) {
196+
console.log('Command: ' + escposCommands.getResponse(data).toString('utf8'));
197+
notify('A response has been sent to the received internal command.');
198+
return escposCommands.getResponse(dataAux);
186199
}
187200

188201
//console.log(dataAux);
@@ -222,7 +235,7 @@ async function escpos(data,b64){
222235
await saveLabel(b64 ? base64DecodeUnicode(data) : data, "raw", getCounter());
223236
}
224237
}
225-
return null
238+
return null;
226239
}
227240
async function displayEscPosLabel (data){
228241
let frame = $('<iframe class="label-esc w-100"></iframe>');
@@ -324,12 +337,13 @@ function startTcpServer() {
324337
}
325338

326339
try{
327-
if ($('#isZpl').is(':checked')) {
328-
zpl(code);
329-
} else {
330-
const response = await escpos(code);
331-
if (response) sock.write(response);
332-
}
340+
let response;
341+
if ($('#isZpl').is(':checked'))
342+
response = await zpl(code);
343+
else
344+
response = await escpos(code);
345+
if (response) sock.write(response);
346+
sock.end();
333347
}catch(err){
334348
console.error(err);
335349
notify('ERROR: {0}'.format(err.message), 'print', 'danger', 0);
@@ -489,7 +503,10 @@ function initEvents() {
489503
});
490504

491505
ipcRenderer.on('app-version-response', (event, version) => {
492-
if (version) $('#app-version').html(' v' + version);
506+
if (version) {
507+
$('#app-version').html(' v' + version);
508+
configs['version'] = ' v' + version;
509+
}
493510
});
494511

495512
ipcRenderer.send('get-app-version');

ZplEscPrinter/js/zpl_commands.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class ZplCommands {
2+
constructor(configs = {}) {
3+
this.configs = configs;
4+
this.commands = {
5+
getStatus: '~HS', // Host Status
6+
getInfo: '~HI', // Host Identification
7+
getConfig: '~WC', // Configuration Label
8+
getDirectory: '~WD' // Directory listing
9+
};
10+
}
11+
12+
matchCommand(data) {
13+
return Object.values(this.commands).includes(data.trim().toUpperCase());
14+
}
15+
16+
getResponse(data) {
17+
const cmd = data.trim().toUpperCase();
18+
switch (cmd) {
19+
case this.commands.getStatus:
20+
return Buffer.from(this.getPrinterStatus(), 'utf8');
21+
case this.commands.getInfo:
22+
return Buffer.from(this.getPrinterInfo(), 'utf8');
23+
case this.commands.getConfig:
24+
return Buffer.from(this.getPrinterConfig(), 'utf8');
25+
case this.commands.getDirectory:
26+
return Buffer.from(this.getPrinterDirectory(), 'utf8');
27+
}
28+
}
29+
30+
getPrinterStatus() {
31+
const string1 = '100,111,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0';
32+
const string3 = '24,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0';
33+
const string2Arr = new Array(32).fill(0);
34+
string2Arr[0] = [1, "1", true, "true"].includes(this.configs.zplHeadOpen) ? 1 : 0;
35+
string2Arr[1] = [1, "1", true, "true"].includes(this.configs.zplPaperOut) ? 1 : 0;
36+
string2Arr[2] = [1, "1", true, "true"].includes(this.configs.zplRibbonOut) ? 1 : 0;
37+
string2Arr[3] = [1, "1", true, "true"].includes(this.configs.zplCutterFault) ? 1 : 0;
38+
string2Arr[4] = [1, "1", true, "true"].includes(this.configs.zplHeadTooHot) ? 1 : 0;
39+
string2Arr[5] = [1, "1", true, "true"].includes(this.configs.zplMotorOverheat) ? 1 : 0;
40+
string2Arr[6] = [1, "1", true, "true"].includes(this.configs.zplPrintheadFault) ? 1 : 0;
41+
string2Arr[7] = [1, "1", true, "true"].includes(this.configs.zplPrinterPaused) ? 1 : 0;
42+
string2Arr[8] = [1, "1", true, "true"].includes(this.configs.zplRewindFault) ? 1 : 0;
43+
string2Arr[9] = [1, "1", true, "true"].includes(this.configs.zplPaperJam) ? 1 : 0;
44+
45+
return (
46+
'\x02' + string1 + '\x03\r\n' +
47+
'\x02' + string2Arr.join(',') + '\x03\r\n' +
48+
'\x02' + string3 + '\x03\r\n'
49+
);
50+
}
51+
52+
getPrinterInfo() {
53+
return (
54+
'\x02EN SYSTEMS CORP.\x03\r\n' +
55+
'\x02ZEBRA ' + this.configs.density + 'dpmm EMULATOR\x03\r\n' +
56+
'\x02FIRMWARE' + (this.configs.version||'') + ' (JS EMULATOR)\x03\r\n'
57+
);
58+
}
59+
60+
getPrinterConfig() {
61+
return '^XA^MMT^PR5,5,5^MD0^LH0,0^JMA^XZ';
62+
}
63+
64+
getPrinterDirectory() {
65+
return (
66+
'\x02R:FILE1.ZPL,1024\r\n' +
67+
'R:LABEL.ZPL,512\r\n' +
68+
'R:LOGO.GRF,8192\r\n\x03\r\n'
69+
);
70+
}
71+
}
72+
73+
module.exports = ZplCommands;

ZplEscPrinter/main.html

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<head>
44
<title>Printer Emulator</title>
55
<link href="css/style.css" type="text/css" rel="stylesheet"/>
6-
<script src="js/commands.js" type="text/javascript"></script>
6+
<script src="js/esc_commands.js" type="text/javascript"></script>
7+
<script src="js/zpl_commands.js" type="text/javascript"></script>
78
<script src="js/main.js" type="module"></script>
89
</head>
910

@@ -196,6 +197,100 @@ <h5 id="mdlPrinterTest" class="modal-title modal-title">
196197
<button id="btn-path" type="button" class="btn btn-secondary">Choose</button>
197198
</div>
198199
</fieldset>
200+
<fieldset class="border rounded p-2 is-zpl">
201+
<legend class="float-none w-auto px-2 mb-0 fs-6 fw-semibold">ZPL Status</legend>
202+
<div class="row row-cols-3 escpos-col">
203+
<div class="col">
204+
<div class="input-group">
205+
<span class="input-group-text">
206+
<div class="form-check">
207+
<input id="zplHeadOpen" type="checkbox" class="form-check-input _big">
208+
<label class="form-check-label" for="zplHeadOpen">Head Open</label>
209+
</div>
210+
</span>
211+
</div>
212+
</div>
213+
<div class="col">
214+
<div class="input-group">
215+
<span class="input-group-text">
216+
<div class="form-check">
217+
<input id="zplPaperOut" type="checkbox" class="form-check-input _big">
218+
<label class="form-check-label" for="zplPaperOut">Paper Out</label>
219+
</div>
220+
</span>
221+
</div>
222+
</div>
223+
<div class="col">
224+
<div class="input-group">
225+
<span class="input-group-text">
226+
<div class="form-check">
227+
<input id="zplRibbonOut" type="checkbox" class="form-check-input _big">
228+
<label class="form-check-label" for="zplRibbonOut">Ribbon Out</label>
229+
</div>
230+
</span>
231+
</div>
232+
</div><div class="col">
233+
<div class="input-group">
234+
<span class="input-group-text">
235+
<div class="form-check">
236+
<input id="zplPaperJam" type="checkbox" class="form-check-input _big">
237+
<label class="form-check-label" for="zplPaperJam">Paper Jam</label>
238+
</div>
239+
</span>
240+
</div>
241+
</div>
242+
<div class="col">
243+
<div class="input-group">
244+
<span class="input-group-text">
245+
<div class="form-check">
246+
<input id="zplPrinterPaused" type="checkbox" class="form-check-input _big">
247+
<label class="form-check-label" for="zplPrinterPaused">Printer Paused</label>
248+
</div>
249+
</span>
250+
</div>
251+
</div>
252+
<div class="col">
253+
<div class="input-group">
254+
<span class="input-group-text">
255+
<div class="form-check">
256+
<input id="zplCutterFault" type="checkbox" class="form-check-input _big">
257+
<label class="form-check-label" for="zplCutterFault">Cutter Fault</label>
258+
</div>
259+
</span>
260+
</div>
261+
</div>
262+
<div class="col">
263+
<div class="input-group">
264+
<span class="input-group-text">
265+
<div class="form-check">
266+
<input id="zplHeadTooHot" type="checkbox" class="form-check-input _big">
267+
<label class="form-check-label" for="zplHeadTooHot">Head Too Hot</label>
268+
</div>
269+
</span>
270+
</div>
271+
</div>
272+
<div class="col">
273+
<div class="input-group">
274+
<span class="input-group-text">
275+
<div class="form-check">
276+
<input id="zplMotorOverheat" type="checkbox" class="form-check-input _big">
277+
<label class="form-check-label" for="zplMotorOverheat">Motor Overheat</label>
278+
</div>
279+
</span>
280+
</div>
281+
</div>
282+
<div class="col">
283+
<div class="input-group">
284+
<span class="input-group-text">
285+
<div class="form-check">
286+
<input id="zplRewindFault" type="checkbox" class="form-check-input _big">
287+
<label class="form-check-label" for="zplRewindFault">Rewind Fault</label>
288+
</div>
289+
</span>
290+
</div>
291+
</div>
292+
</div>
293+
</fieldset>
199294
<fieldset class="border rounded p-2 is-esc">
200295
<legend class="float-none w-auto px-2 mb-0 fs-6 fw-semibold">ESC/POS Status</legend>
201296
<div class="row row-cols-3 escpos-col">

0 commit comments

Comments
 (0)