From 0da1761a1198fd7b31dc77d1468c53094795b056 Mon Sep 17 00:00:00 2001 From: Klemen Kastelic Date: Wed, 13 Sep 2023 10:41:28 +0200 Subject: [PATCH] - added experimental getStatus for epson printer, - added wait for response flag to network interface, - added example for get status with a network printer --- examples/status.js | 20 +++++++++++++++++++ lib/core.js | 5 +++++ lib/interfaces/network.js | 42 ++++++++++++++++++++++++--------------- lib/types/epson-config.js | 1 + lib/types/epson.js | 6 ++++++ node-thermal-printer.d.ts | 3 ++- 6 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 examples/status.js diff --git a/examples/status.js b/examples/status.js new file mode 100644 index 0000000..e8bcdc8 --- /dev/null +++ b/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(); diff --git a/lib/core.js b/lib/core.js index 21005c1..b9a6d58 100644 --- a/lib/core.js +++ b/lib/core.js @@ -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()); diff --git a/lib/interfaces/network.js b/lib/interfaces/network.js index 700c446..40e5de7 100644 --- a/lib/interfaces/network.js +++ b/lib/interfaces/network.js @@ -12,7 +12,7 @@ class Network extends Interface { async isPrinterConnected() { return new Promise((resolve) => { - const printer = Net.connect( + const networkConnection = Net.connect( { host: this.host, port: this.port, @@ -20,49 +20,59 @@ class Network extends Interface { }, () => { 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(); }); }); } diff --git a/lib/types/epson-config.js b/lib/types/epson-config.js index f7f7963..a7d6a2b 100644 --- a/lib/types/epson-config.js +++ b/lib/types/epson-config.js @@ -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). diff --git a/lib/types/epson.js b/lib/types/epson.js index c43cb0a..e4dae50 100644 --- a/lib/types/epson.js +++ b/lib/types/epson.js @@ -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) { diff --git a/node-thermal-printer.d.ts b/node-thermal-printer.d.ts index 4e76058..b882545 100755 --- a/node-thermal-printer.d.ts +++ b/node-thermal-printer.d.ts @@ -89,12 +89,13 @@ declare class ThermalPrinter { /** * Send printing buffer to printer - * @param Object Options (docname) + * @param Object Options (docname, waitForResponse) * @returns Promise */ execute( options?: { docname?: string; + waitForResponse?: boolean; } ): Promise;