Skip to content

Commit adaa9b9

Browse files
authored
Merge pull request #93 from yaacov/add-errno-to-port-no-open
Add an errno to port not open error
2 parents 1061b45 + 31979fc commit adaa9b9

3 files changed

Lines changed: 47 additions & 20 deletions

File tree

examples/logger.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,31 @@ var client = new ModbusRTU();
77

88
var networkErrors = ["ESOCKETTIMEDOUT", "ETIMEDOUT", "ECONNRESET", "ECONNREFUSED"];
99

10+
// check error, and reconnect if needed
11+
function checkError(e) {
12+
if(e.errno && networkErrors.includes(e.errno)) {
13+
console.log("we have to reconnect");
14+
15+
// close port
16+
client.close();
17+
18+
// re open client
19+
client = new ModbusRTU();
20+
setTimeout(connect, 3000);
21+
}
22+
}
23+
1024
// open connection to a serial port
1125
//client.connectRTU("/dev/ttyUSB0", {baudrate: 9600})
12-
client.connectTCP("127.0.0.1", { port: 8502 })
13-
.then(setClient)
14-
.then(function() {
15-
console.log("Connected"); })
16-
.catch(function(e) {
17-
if(e.errno) {
18-
if(networkErrors.includes(e.errno)) {
19-
console.log("we have to reconnect");
20-
}
21-
}
22-
console.log(e.message); });
26+
function connect() {
27+
client.connectTCP("127.0.0.1", { port: 8502 })
28+
.then(setClient)
29+
.then(function() {
30+
console.log("Connected"); })
31+
.catch(function(e) {
32+
checkError(e);
33+
console.log(e.message); });
34+
}
2335

2436
function setClient() {
2537
// set the client's unit id
@@ -37,6 +49,10 @@ function run() {
3749
.then(function(d) {
3850
console.log("Receive:", d.data); })
3951
.catch(function(e) {
52+
checkError(e);
4053
console.log(e.message); })
4154
.then(function() { setTimeout(run, 1000); });
4255
}
56+
57+
// connect and start logging
58+
connect();

index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ var crc16 = require("./utils/crc16");
2222
var modbusSerialDebug = require("debug")("modbus-serial");
2323

2424
var PORT_NOT_OPEN_MESSAGE = "Port Not Open";
25+
var PORT_NOT_OPEN_ERRNO = "ECONNREFUSED";
26+
27+
var PortNotOpenError = function() {
28+
Error.captureStackTrace(this, this.constructor);
29+
this.name = this.constructor.name;
30+
this.message = PORT_NOT_OPEN_MESSAGE;
31+
this.errno = PORT_NOT_OPEN_ERRNO;
32+
};
33+
2534
/**
2635
* @fileoverview ModbusRTU module, exports the ModbusRTU class.
2736
* this class makes ModbusRTU calls fun and easy.
@@ -350,7 +359,7 @@ ModbusRTU.prototype.writeFC1 = function(address, dataAddress, length, next) {
350359
ModbusRTU.prototype.writeFC2 = function(address, dataAddress, length, next, code) {
351360
// check port is actually open before attempting write
352361
if (this._port.isOpen() === false) {
353-
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
362+
if (next) next(new PortNotOpenError());
354363
return;
355364
}
356365

@@ -403,7 +412,7 @@ ModbusRTU.prototype.writeFC3 = function(address, dataAddress, length, next) {
403412
ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code) {
404413
// check port is actually open before attempting write
405414
if (this._port.isOpen() === false) {
406-
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
415+
if (next) next(new PortNotOpenError());
407416
return;
408417
}
409418

@@ -444,7 +453,7 @@ ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code
444453
ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) {
445454
// check port is actually open before attempting write
446455
if (this._port.isOpen() === false) {
447-
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
456+
if (next) next(new PortNotOpenError());
448457
return;
449458
}
450459

@@ -489,7 +498,7 @@ ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) {
489498
ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
490499
// check port is actually open before attempting write
491500
if (this._port.isOpen() === false) {
492-
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
501+
if (next) next(new PortNotOpenError());
493502
return;
494503
}
495504

@@ -530,7 +539,7 @@ ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
530539
ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {
531540
// check port is actually open before attempting write
532541
if (this._port.isOpen() === false) {
533-
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
542+
if (next) next(new PortNotOpenError());
534543
return;
535544
}
536545

@@ -586,7 +595,7 @@ ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {
586595
ModbusRTU.prototype.writeFC16 = function(address, dataAddress, array, next) {
587596
// check port is actually open before attempting write
588597
if (this._port.isOpen() === false) {
589-
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
598+
if (next) next(new PortNotOpenError());
590599
return;
591600
}
592601

@@ -629,7 +638,9 @@ require("./apis/promise")(ModbusRTU);
629638
// exports
630639
module.exports = ModbusRTU;
631640
module.exports.TestPort = require("./ports/testport");
632-
module.exports.RTUBufferedPort = require("./ports/rtubufferedport");
641+
try {
642+
module.exports.RTUBufferedPort = require("./ports/rtubufferedport");
643+
} catch (err) {}
633644
module.exports.TcpPort = require("./ports/tcpport");
634645
module.exports.TcpRTUBufferedPort = require("./ports/tcprtubufferedport");
635646
module.exports.TelnetPort = require("./ports/telnetport");

ports/tcpport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ var TcpPort = function(ip, options) {
6969

7070
this._client.on("close", function(had_error) {
7171
modbus.openFlag = false;
72-
modbusSerialDebug("TCP port: signal close" + had_error);
72+
modbusSerialDebug("TCP port: signal close: " + had_error);
7373
handleCallback(had_error);
7474
});
7575

7676
this._client.on("error", function(had_error) {
7777
modbus.openFlag = false;
78-
modbusSerialDebug("TCP port: signal error" + had_error);
78+
modbusSerialDebug("TCP port: signal error: " + had_error);
7979
handleCallback(had_error);
8080
});
8181

0 commit comments

Comments
 (0)