3

I am trying to print to an Epson TM-T88IV printer from javascript.

(This is a thermal receipt printer.)

Status:

  • The printer is connected to the network via ethernet.
  • I can successfully ping the printer.
  • I can access the printer web configuration page.
  • I have reset the printer.
  • I can print to the printer using an Epson's Mac OS print driver.

Development Environment:

  • I am running Mac OS X 10.10
  • I reinstalled Node and NPM using Homebrew.
  • Ran: brew update...brew doctor...brew symlink...

Issue:

I cannot figure out how to resolve the ECONNREFUSED error that I am encountering.

Already Tried:

  • Ran it on Tonicdev.com to no avail for clues. Link to Tonic Notebook.
  • I have searched and read every ECONNREFUSED question on Stackoverflow.
  • I have tried placing this code in a standalone print.js file and calling:

    node print.js

Error:

{ Error: connect ECONNREFUSED 192.168.77.22:8008
    at Object.exports._errnoException (util.js:1012:11)
    at exports._exceptionWithHostPort (util.js:1035:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '192.168.77.22',
  port: 8008 }

Update

I checked which ports were open/available on the device. It turns out that Epson's SDK documentation was incorrect! The actual port that is open for TCP/Printer requests is 515, not 8008. Good grief!

nc -z 192.168.77.22 1-6000

The printer.init isn't establishing a connection. The printer.isPrinterConnected is returning false.

I re-tried Epson's epos SDK with the correct port. I got the following error: ERROR_TIMEOUT.

So, I think I might not be setting a TimeOut value correctly?

Node-Thermal-Printer (Code)

var printer = require("node-thermal-printer");
printer.init({
    type: 'epson',
    interface: '/dev/usb/lp0',
    ip: "192.168.77.22",
    port: '515'
});

printer.isPrinterConnected(function(isConnected){
    console.log(isConnected);
});

process.on('uncaughtException', function (err) {
        console.log(err);
}); 


printer.alignCenter();
printer.println("Hello world");
printer.cut();
printer.execute(function(err){
    if (err) {
        console.error("Print failed", err);
    } else {
     console.log("Print done");
    }
});

Epson SDK (Code)

var ePosDev = new epson.ePOSDevice();

function connect() {
  var ipAddress = '192.168.77.22';
  var port = '515';
  ePosDev.connect(ipAddress, port, callback_connect);
}

function callback_connect(resultConnect){
    alert('Callback Called');
    var deviceId = 'local_printer';
    var options = {'crypto' : false, 'buffer' : false};
    alert(resultConnect);

    if ((resultConnect == 'OK') || (resultConnect == 'SSL_CONNECT_OK')) {
        alert('Connected!');
        ePosDev.createDevice(deviceID,ePosDev.DEVICE_TYPE_PRINTER,options,callback_createDevice);       
    }
    else {
        alert('Not Connected!');
    }
}

var printer = null;

function callback_createDevice(deviceObj, errorCode){
  if (deviceObj === null) {
//Displays an error message if the system fails to retrieve the Printer object
return; }
  printer = deviceObj;
//Registers the print complete event
  printer.onreceive = function(response){
    if (response.success) {
//Displays the successful print message
}
else {
//Displays error messages
} };
}

function createData(){
  printer.addTextAlign(printer.ALIGN_CENTER);
  printer.addText("Hello World\n");
}

function send(){
  if (ePosDev.isConnected) {
    printer.send();
  }
}
Kevin Kurpe
  • 61
  • 1
  • 6

2 Answers2

3

When I widened my search, I discovered that the printer was listening for commands on another port!

nc -z 192.168.77.22 1-50000

...retuned the following results:

Connection to 192.168.77.22 port 80 [tcp/http] succeeded!
Connection to 192.168.77.22 port 515 [tcp/printer] succeeded!
Connection to 192.168.77.22 port 9100 [tcp/hp-pdl-datastr] succeeded!

So, I simply changed the port number to 9100 in the init portion of the code and voila!

Kevin Kurpe
  • 61
  • 1
  • 6
0

I don't know why the port in my Epson TM-M30 POS printer has changed from official '8008' to '515'.

The problem is that epos-2.7.0.js has declared the connection ports as a constant:

    ...
    this.IFPORT_EPOSDEVICE = 8008;
    this.IFPORT_EPOSDEVICE_S = 8043;
    ...

But the real one is 515, so you have to modify the official js:

    this.IFPORT_EPOSDEVICE = 515;

With this change, here is my code:

var ePosDev = new epson.ePOSDevice();
var ipAddress = '192.168.150.162'; 
var port = '515';
var printer = null;
ePosDev.connect(ipAddress, port, function callback_connect(resultConnect){
  var deviceId = 'local_printer';
  var options = {'crypto' : false, 'buffer' : false};
  if ((resultConnect == 'OK') || (resultConnect == 'SSL_CONNECT_OK')) {
    //Retrieves the Printer object
    ePosDev.createDevice(
      deviceId, 
      ePosDev.DEVICE_TYPE_PRINTER, 
      options, 
      function callback_createDevice(deviceObj, errorCode){
        if (deviceObj === null) {
          return; 
        }
        printer = deviceObj;
        printer.addTextAlign(printer.ALIGN_CENTER); 
        printer.addText('My text..\n');
        printer.send();
        if (ePosDev.isConnected) {
          printer.send(); 
        }
        ePosDev.deleteDevice(printer, function callback_deleteDevice(errorCode){
          ePosDev.disconnect();
        });
      });
  }});
pablo.nunez
  • 124
  • 1
  • 5