Skip to content

Instantly share code, notes, and snippets.

@renatoargh
Created August 29, 2016 02:57
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save renatoargh/7baec3926d93bf4a0d5876fc7aef334c to your computer and use it in GitHub Desktop.
Save renatoargh/7baec3926d93bf4a0d5876fc7aef334c to your computer and use it in GitHub Desktop.
Imprimindo com Bematech MP-4200 com node.js (sem cups nem módulos nativos)
var fs = require('fs');
String.prototype.toBytes = function() {
var arr = []
for (var i=0; i < this.length; i++) {
arr.push(this[i].charCodeAt(0))
}
return arr;
}
var printData = "Hello World!".toBytes().concat([0x01B, 0x64, 10, 0x1d, 0x56, 0x00]);
var wstream = fs.createWriteStream('/dev/ttyACM0', {
encoding: 'utf16le'
});
wstream.write(new Buffer(printData));
wstream.end();
@renatoargh
Copy link
Author

Exemplo mais completo

function fontA(doc) {
    doc.push(0x1b, 0x4d, 0x00);
}

function fontB(doc) {
    doc.push(0x1b, 0x4d, 0x01);
}

function alignLeft(doc) {
    doc.push(0x1b, 0x61, 0x00);
}

function alignCenter(doc) {
    doc.push(0x1b, 0x61, 0x01);
}

function alignRight(doc) {
    doc.push(0x1b, 0x61, 0x02);
}

function normal(doc, text) {
    for (var i = 0; i < text.length; i++) {
        doc.push(text[i].charCodeAt(0))
    }
}

function doubleHeight(doc, text) {
    doc.push(0x1b, 0x21, 0x10);
    normal(doc, text);
    doc.push(0x1b, 0x21, 0x00);
}

function doubleWidth(doc, text) {
    doc.push(0x1b, 0x21, 0x20);
    normal(doc, text);
    doc.push(0x1b, 0x21, 0x00);
}

function underline(doc, text) {
    doc.push(0x1b, 0x2d, 0x01);
    normal(doc, text);
    doc.push(0x1b, 0x2d, 0x00);
}

function underline2(doc, text) {
    doc.push(0x1b, 0x2d, 0x02);
    normal(doc, text);
    doc.push(0x1b, 0x2d, 0x00);
}

function bold(doc, text) {
    doc.push(0x1b, 0x45, 0x01);
    normal(doc, text);
    doc.push(0x1b, 0x45, 0x00);
}

function lineFeed(doc, length) {
    doc.push(0x01B, 0x64, length || 1);
}

function fullCut(doc) {
    lineFeed(doc, 5);
    doc.push(0x1d, 0x56, 0x00);
}

var doc = [];

fontA(doc);
normal(doc, 'normal');
lineFeed(doc, 1);

bold(doc, 'bold');
lineFeed(doc, 1);

fontB(doc);
normal(doc, 'fontB normal');
lineFeed(doc, 1);

bold(doc, 'fontB bold');
lineFeed(doc, 1);

doubleHeight(doc, 'double height');
lineFeed(doc, 1);

doubleWidth(doc, 'double width');
lineFeed(doc, 1);

underline(doc, 'underline 1');
lineFeed(doc, 1);

underline2(doc, 'underline 2');
lineFeed(doc, 1);

alignCenter(doc);
bold(doc, 'Renato Mendonca da Gama');
lineFeed(doc, 1);
bold(doc, '-----------------------');
lineFeed(doc, 2);

alignRight(doc);
normal(doc, 'Produto 1');
normal(doc, '123,90');
lineFeed(doc, 1);

alignLeft(doc);
normal(doc, 'Produto 2');
alignRight(doc);
normal(doc, '34,89');
lineFeed(doc, 1);

fullCut(doc);

var fs = require('fs');

var stream = fs.createWriteStream('/dev/ttyACM0', {
    encoding: 'utf16le'
});

stream.write(new Buffer(doc));
stream.end();

@leonardacs
Copy link

Você teve problemas ao imprimir caracteres especiais?

@ricardocjca
Copy link

Renato, eu to usando o windows, nessa parte do codigo fs.createWriteStream('/dev/ttyACM0', { eu teria q trocar né?

@Patrique
Copy link

Patrique commented Oct 6, 2020

@renatoargh no windows basta eu inserir COM01 no createwritestream? estou tentando e nada, estou fazendo algo de errado ou em windows não rola?

@renatoargh
Copy link
Author

renatoargh commented Oct 7, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment