Skip to content

Instantly share code, notes, and snippets.

@clayperez
Created November 22, 2013 01:27
Show Gist options
  • Save clayperez/7593217 to your computer and use it in GitHub Desktop.
Save clayperez/7593217 to your computer and use it in GitHub Desktop.
ZEBRA COMMAND LINE RFID PRINT UTILITY Needs some cleanup work, but this handy script lets you execute simple print commands to a Zebra RFID printer that is connected to your computer.
///////////////////////////////////////
// RFID PRINTER COMMAND LINE UTILITY //
///////////////////////////////////////
// REQUIRES: node-printer by tojocky
// USAGE: node zebra.js <printer> <start> <end> <file.csv> <print/null>
//
// RANGE EXAMPLE prints range to screen only:
// C:> node zebra zebra 1 100
//
// RANGE EXAMPLE prints range:
// C:> node zebra zebra 1 100 "" print
//
// CSV EXAMPLE displays CXSR.csv regardless of start or end, NO PRINT:
// C:> node zebra zebra 1 1 CXSR.csv
//
// CSV EXAMPLE PRINTS CXSR.csv regardless of start or end:
// C:> node zebra zebra 1 1 CXSR.csv print
var commandFormat = "node zebra.js <printer> <start> <end> <file.csv> <print/null>";
var fs = require("fs");
var printer = require("printer"); // tojocky/node-printer: https://github.com/tojocky/node-printer
console.log(commandFormat);
////////////////////////////
// COMMAND LINE ARGUMENTS //
////////////////////////////
// node zebra.js <printer> <start(#)> <end(#)> <noPrint(true/false, default:true)>
var GLOBAL = {};
GLOBAL.printFormat = "RAW";
GLOBAL.printerName = process.argv[2];
var start = parseInt(process.argv[3]) || 1;
var finish = parseInt(process.argv[4]) || 1;
GLOBAL.CSVfile = process.argv[5] || "";
var noPrint = process.argv[6] || "no";
//each printer command takes the following form where strings matching ##var## are replaced within loops
var ZPLtemplate = "^XA^RS8,B10,,1^RW15,15^FO50,50^A0N,165^FD##BIB##^FS^RFW,H^FD##EPC##^FS^XZ";
var color = {
r: "\x1b[31m",
c: "\x1b[36m",
m: "\x1b[35m",
reset: "\x1b[0m"
};
function dec2hex(d) { return (+d).toString(16); }
function pad(n, width, z) { z = z || '0'; n = n + ''; return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; }
function file2CSV(filename){
console.log(color.r+"\n=============\nZEBRA RFID PRINTER ACTIVATING\n=============="+color.reset);
console.log("PRESS CTRL+C TO ABORT AT ANY TIME\n\n");
console.log("Reading file: "+filename);
var fileData = fs.readFileSync(filename, 'utf8');
var rows = fileData.split("\n");
var returnArray = [];
rows.forEach(function(row,i,a){
returnArray.push(row.split(","));
});
return returnArray;
}
function printZebra(bibNumber) {
var EPC = pad(dec2hex(bibNumber), 24, 0);
if(bibNumber == "" || EPC == ""){/*DO NOT ACTIVATE PRINTER*/return;}
var ZPLcommand = ZPLtemplate.replace(/##BIB##/, bibNumber).replace(/##EPC##/, EPC);
sendToPrinter(ZPLcommand , bibNumber );
}
function printZebraCSVrow(CSVrow) {
var bibNumber = CSVrow[0]; //what gets printed
var EPC = CSVrow[1]; //what gets encoded
if(bibNumber == "" || EPC == ""){/*DO NOT ACTIVATE PRINTER*/return;}
var ZPLcommand = ZPLtemplate.replace(/##BIB##/, bibNumber).replace(/##EPC##/, EPC);
sendToPrinter(ZPLcommand , bibNumber );
}
function sendToPrinter(dataToSend , printedNumber){
if(noPrint == "print") {
printer.printDirect({
data: dataToSend,
printer: GLOBAL.printerName,
type: GLOBAL.printFormat,
success: function(){ console.log("PRINTING: "+printedNumber); },
error: function(err){ console.log(color.red+"ERROR: "+printedNumber+color.reset); }
});
}else {
console.log("NOPRINT: "+printedNumber);
}
}
function wait(ms){
var ms = ms || 1000;
var now = new Date().getTime();
while(new Date().getTime()-ms < now){ /* just hang out */ }
}
if(typeof GLOBAL.printerName == "undefined") { console.log("Please use the format: "+commandFormat); process.exit(1); }
var batchSize = 100;
var waitDurration = 120000;
if(GLOBAL.CSVfile == ""){
for(var i=start; i<=finish; i++){
printZebra(i);
if(i % batchSize == 0 && i > 0){
console.log(color.c+"Waiting "+waitDurration+"ms\n"+color.reset);
wait(waitDurration); //WAIT FOR THE PRINTER TO FINISH PRINTING EACH BATCH OF <batchSize>
}
}
}else{
var CSVdata = file2CSV(GLOBAL.CSVfile);
CSVdata.forEach(function(row,i,a){
printZebraCSVrow(row);
if(i % batchSize == 0 && i > 0){
console.log(color.c+"Waiting "+waitDurration+"ms\n"+color.reset);
wait(waitDurration); //WAIT FOR THE PRINTER TO FINISH PRINTING EACH BATCH OF <batchSize>
}
});
}
if(noPrint == "no") { process.exit(1); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment