(这是热敏收据打印机。)
我无法弄清楚如何解决我遇到的ECONNREFUSED错误。
我尝试将此代码放在一个独立的 print.js 文件中并调用:
node print.js
{ 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 }
我检查了设备上打开/可用的端口。事实证明,Epson的SDK文档不正确!为TCP /打印机请求打开的实际端口是515,而不是8008.好悲伤!
nc -z 192.168.77.22 1-6000
printer.init 未建立连接。 printer.isPrinterConnected 正在返回 false 。
我用正确的端口重新尝试了爱普生的epos SDK。我收到以下错误:ERROR_TIMEOUT
。
所以,我想我可能没有正确设置TimeOut值?
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");
}
});
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();
}
}
答案 0 :(得分:2)
当我扩大搜索范围时,我发现打印机正在侦听另一个端口上的命令!
nc -z 192.168.77.22 1-50000
...重新调整了以下结果:
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!
所以,我只是在代码的 init 部分将端口号更改为9100,瞧!
答案 1 :(得分:0)
我不知道为什么我的Epson TM-M30 POS打印机的端口已经从官方的#8008'到' 515'。
问题是 epos-2.7.0.js 已将连接端口声明为常量:
...
this.IFPORT_EPOSDEVICE = 8008;
this.IFPORT_EPOSDEVICE_S = 8043;
...
但真正的是515,所以你必须修改官方的js:
this.IFPORT_EPOSDEVICE = 515;
通过此更改,这是我的代码:
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();
});
});
}});