Skip to content

Commit

Permalink
Merge pull request #94 from yaacov/fix-is-open-checks
Browse files Browse the repository at this point in the history
Fix wrong check for port open
  • Loading branch information
yaacov authored Mar 31, 2017
2 parents adaa9b9 + 0809e1e commit fd1e44c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
6 changes: 5 additions & 1 deletion examples/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//var ModbusRTU = require("modbus-serial");
var ModbusRTU = require("../index");
var client = new ModbusRTU();
var timeoutRef = null;

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

Expand All @@ -12,6 +13,9 @@ function checkError(e) {
if(e.errno && networkErrors.includes(e.errno)) {
console.log("we have to reconnect");

// stop reading loop
clearTimeout(timeoutRef);

// close port
client.close();

Expand Down Expand Up @@ -51,7 +55,7 @@ function run() {
.catch(function(e) {
checkError(e);
console.log(e.message); })
.then(function() { setTimeout(run, 1000); });
.then(function() { timeoutRef = setTimeout(run, 1000); });
}

// connect and start logging
Expand Down
26 changes: 16 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,22 @@ ModbusRTU.prototype.open = function(callback) {
* or failure.
*/
ModbusRTU.prototype.close = function(callback) {
// close the serial port
this._port.close(callback);
this._port.removeAllListeners("data");
// close the serial port if exist
if (this._port) {
this._port.close(callback);
this._port.removeAllListeners("data");
}
};

/**
* Check if port is open
*/
ModbusRTU.prototype.isOpen = function() {
return this._port && this._port.isOpen();
if (this._port) {
return this._port.isOpen();
}

return false;
};

/**
Expand All @@ -358,7 +364,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 (this.isOpen() !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -411,7 +417,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 (this.isOpen() !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -452,7 +458,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 (this.isOpen() !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -497,7 +503,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 (this.isOpen() !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -538,7 +544,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 (this.isOpen() !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -594,7 +600,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 (this.isOpen() !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modbus-serial",
"version": "5.2.4",
"version": "5.2.6",
"description": "A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit fd1e44c

Please sign in to comment.