Skip to content

Commit

Permalink
Merge pull request #93 from yaacov/add-errno-to-port-no-open
Browse files Browse the repository at this point in the history
Add an errno to port not open error
  • Loading branch information
yaacov committed Mar 31, 2017
2 parents 1061b45 + 31979fc commit adaa9b9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
38 changes: 27 additions & 11 deletions examples/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ var client = new ModbusRTU();

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

// check error, and reconnect if needed
function checkError(e) {
if(e.errno && networkErrors.includes(e.errno)) {
console.log("we have to reconnect");

// close port
client.close();

// re open client
client = new ModbusRTU();
setTimeout(connect, 3000);
}
}

// open connection to a serial port
//client.connectRTU("/dev/ttyUSB0", {baudrate: 9600})
client.connectTCP("127.0.0.1", { port: 8502 })
.then(setClient)
.then(function() {
console.log("Connected"); })
.catch(function(e) {
if(e.errno) {
if(networkErrors.includes(e.errno)) {
console.log("we have to reconnect");
}
}
console.log(e.message); });
function connect() {
client.connectTCP("127.0.0.1", { port: 8502 })
.then(setClient)
.then(function() {
console.log("Connected"); })
.catch(function(e) {
checkError(e);
console.log(e.message); });
}

function setClient() {
// set the client's unit id
Expand All @@ -37,6 +49,10 @@ function run() {
.then(function(d) {
console.log("Receive:", d.data); })
.catch(function(e) {
checkError(e);
console.log(e.message); })
.then(function() { setTimeout(run, 1000); });
}

// connect and start logging
connect();
25 changes: 18 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ var crc16 = require("./utils/crc16");
var modbusSerialDebug = require("debug")("modbus-serial");

var PORT_NOT_OPEN_MESSAGE = "Port Not Open";
var PORT_NOT_OPEN_ERRNO = "ECONNREFUSED";

var PortNotOpenError = function() {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = PORT_NOT_OPEN_MESSAGE;
this.errno = PORT_NOT_OPEN_ERRNO;
};

/**
* @fileoverview ModbusRTU module, exports the ModbusRTU class.
* this class makes ModbusRTU calls fun and easy.
Expand Down Expand Up @@ -350,7 +359,7 @@ ModbusRTU.prototype.writeFC1 = function(address, dataAddress, length, next) {
ModbusRTU.prototype.writeFC2 = function(address, dataAddress, length, next, code) {
// check port is actually open before attempting write
if (this._port.isOpen() === false) {
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
if (next) next(new PortNotOpenError());
return;
}

Expand Down Expand Up @@ -403,7 +412,7 @@ ModbusRTU.prototype.writeFC3 = function(address, dataAddress, length, next) {
ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code) {
// check port is actually open before attempting write
if (this._port.isOpen() === false) {
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
if (next) next(new PortNotOpenError());
return;
}

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

Expand Down Expand Up @@ -489,7 +498,7 @@ ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) {
ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
// check port is actually open before attempting write
if (this._port.isOpen() === false) {
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
if (next) next(new PortNotOpenError());
return;
}

Expand Down Expand Up @@ -530,7 +539,7 @@ ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {
// check port is actually open before attempting write
if (this._port.isOpen() === false) {
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
if (next) next(new PortNotOpenError());
return;
}

Expand Down Expand Up @@ -586,7 +595,7 @@ ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {
ModbusRTU.prototype.writeFC16 = function(address, dataAddress, array, next) {
// check port is actually open before attempting write
if (this._port.isOpen() === false) {
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE));
if (next) next(new PortNotOpenError());
return;
}

Expand Down Expand Up @@ -629,7 +638,9 @@ require("./apis/promise")(ModbusRTU);
// exports
module.exports = ModbusRTU;
module.exports.TestPort = require("./ports/testport");
module.exports.RTUBufferedPort = require("./ports/rtubufferedport");
try {
module.exports.RTUBufferedPort = require("./ports/rtubufferedport");
} catch (err) {}
module.exports.TcpPort = require("./ports/tcpport");
module.exports.TcpRTUBufferedPort = require("./ports/tcprtubufferedport");
module.exports.TelnetPort = require("./ports/telnetport");
Expand Down
4 changes: 2 additions & 2 deletions ports/tcpport.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ var TcpPort = function(ip, options) {

this._client.on("close", function(had_error) {
modbus.openFlag = false;
modbusSerialDebug("TCP port: signal close" + had_error);
modbusSerialDebug("TCP port: signal close: " + had_error);
handleCallback(had_error);
});

this._client.on("error", function(had_error) {
modbus.openFlag = false;
modbusSerialDebug("TCP port: signal error" + had_error);
modbusSerialDebug("TCP port: signal error: " + had_error);
handleCallback(had_error);
});

Expand Down

0 comments on commit adaa9b9

Please sign in to comment.