Skip to content

Commit

Permalink
- added experimental getStatus for epson printer,
Browse files Browse the repository at this point in the history
- added wait for response flag to network interface,
- added example for get status with a network printer
  • Loading branch information
Klemen Kastelic committed Sep 13, 2023
1 parent bd1bde4 commit 0da1761
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
20 changes: 20 additions & 0 deletions examples/status.js
@@ -0,0 +1,20 @@
const ThermalPrinter = require('../node-thermal-printer').printer;
const Types = require('../node-thermal-printer').types;

async function testConnection() {
const printer = new ThermalPrinter({
type: Types.EPSON,
interface: process.argv[2],
});

printer.getStatus();

try {
const status = await printer.execute({ waitForResponse: true });
console.log('Printer status:', status);
} catch (e) {
console.error('Print failed:', e);
}
}

testConnection();
5 changes: 5 additions & 0 deletions lib/core.js
Expand Up @@ -363,6 +363,11 @@ class ThermalPrinter {
return this.Interface.isPrinterConnected(exists);
}

// ----------------------------------------------------- GET PRINTER STATUS -----------------------------------------------------
async getStatus() {
this.append(this.printer.getStatus());
}

// ----------------------------------------------------- BEEP -----------------------------------------------------
beep() {
this.append(this.printer.beep());
Expand Down
42 changes: 26 additions & 16 deletions lib/interfaces/network.js
Expand Up @@ -12,57 +12,67 @@ class Network extends Interface {

async isPrinterConnected() {
return new Promise((resolve) => {
const printer = Net.connect(
const networkConnection = Net.connect(
{
host: this.host,
port: this.port,
timeout: this.timeout,
},
() => {
resolve(true);
printer.destroy();
networkConnection.destroy();
},
);

printer.on('error', (error) => {
networkConnection.on('error', (error) => {
console.error('Printer network connection error:', error);
resolve(false);
printer.destroy();
networkConnection.destroy();
});

printer.on('timeout', () => {
networkConnection.on('timeout', () => {
console.error('Printer network connection timeout.');
resolve(false);
printer.destroy();
networkConnection.destroy();
});
});
}

async execute(buffer, options = {}) {
async execute(buffer, options = { waitForResponse }) {
return new Promise((resolve, reject) => {
const name = `${this.host}:${this.port}`;
const printer = Net.connect(
const networkConnection = Net.connect(
{
host: this.host,
port: this.port,
timeout: this.timeout,
},
() => {
printer.write(buffer, null, () => {
resolve(`Data sent to printer: ${name}`);
printer.destroy();
networkConnection.write(buffer, null, () => {
console.log(`Data sent to printer: ${name}`);
if (!options.waitForResponse) {
networkConnection.destroy();
}
});
},
}
);

printer.on('error', (error) => {
networkConnection.on('data', function (data) {
if (options.waitForResponse) {
console.log('Received data:', data.toString("hex"));
resolve(data);
networkConnection.destroy();
}
});

networkConnection.on('error', (error) => {
reject(error);
printer.destroy();
networkConnection.destroy();
});

printer.on('timeout', () => {
networkConnection.on('timeout', () => {
reject(new Error('Socket timeout'));
printer.destroy();
networkConnection.destroy();
});
});
}
Expand Down
1 change: 1 addition & 0 deletions lib/types/epson-config.js
Expand Up @@ -11,6 +11,7 @@ module.exports = {
HW_INIT: Buffer.from([0x1b, 0x40]), // Clear data in buffer and reset modes
HW_SELECT: Buffer.from([0x1b, 0x3d, 0x01]), // Printer select
HW_RESET: Buffer.from([0x1b, 0x3f, 0x0a, 0x00]), // Reset printer hardware
TRANSMIT_PAPER_STATUS: Buffer.from([0x1d, 0x72, 0x01]), // Transmit printer paper status
BEEP: Buffer.from([0x1b, 0x1e]), // Sounds built-in buzzer (if equipped)
UPSIDE_DOWN_ON: Buffer.from([0x1b, 0x7b, 0x01]), // Upside down printing ON (rotated 180 degrees).
UPSIDE_DOWN_OFF: Buffer.from([0x1b, 0x7b, 0x00]), // Upside down printing OFF (default).
Expand Down
6 changes: 6 additions & 0 deletions lib/types/epson.js
Expand Up @@ -6,6 +6,12 @@ class Epson extends PrinterType {
this.config = require('./epson-config');
}

// ------------------------------ Get paper status ------------------------------
getStatus() {
// https://www.epson-biz.com/modules/ref_escpos/index.php?content_id=124#gs_lr
return this.config.TRANSMIT_PAPER_STATUS;
}

// ------------------------------ Append ------------------------------
append(appendBuffer) {
if (this.buffer) {
Expand Down
3 changes: 2 additions & 1 deletion node-thermal-printer.d.ts
Expand Up @@ -89,12 +89,13 @@ declare class ThermalPrinter {

/**
* Send printing buffer to printer
* @param Object Options (docname)
* @param Object Options (docname, waitForResponse)
* @returns Promise<String>
*/
execute(
options?: {
docname?: string;
waitForResponse?: boolean;
}
): Promise<String>;

Expand Down

0 comments on commit 0da1761

Please sign in to comment.