diff --git a/examples/logger.js b/examples/logger.js index 4e595a6..1a24d88 100644 --- a/examples/logger.js +++ b/examples/logger.js @@ -29,7 +29,7 @@ function connect() { clearTimeout(timeoutConnectRef); // if client already open, just run - if (client.isOpen()) { + if (client.isOpen) { run(); } diff --git a/examples/logger_complete.js b/examples/logger_complete.js index 765c372..3fe9b58 100755 --- a/examples/logger_complete.js +++ b/examples/logger_complete.js @@ -32,7 +32,7 @@ function connect() { clearTimeout(timeoutConnectRef); // if client already open, just run - if (client.isOpen()) { + if (client.isOpen) { run(); } diff --git a/index.js b/index.js index 50a49e9..3e59346 100644 --- a/index.js +++ b/index.js @@ -334,6 +334,20 @@ ModbusRTU.prototype.open = function(callback) { callback(error); } }); + + /** + * Check if port is open + */ + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + if (this._port) { + return this._port.isOpen; + } + + return false; + } + }); }; /** @@ -350,17 +364,6 @@ ModbusRTU.prototype.close = function(callback) { } }; -/** - * Check if port is open - */ -ModbusRTU.prototype.isOpen = function() { - if (this._port) { - return this._port.isOpen(); - } - - return false; -}; - /** * Write a Modbus "Read Coil Status" (FC=01) to serial port. * @@ -383,7 +386,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.isOpen() !== true) { + if (this.isOpen !== true) { if (next) next(new PortNotOpenError()); return; } @@ -442,7 +445,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.isOpen() !== true) { + if (this.isOpen !== true) { if (next) next(new PortNotOpenError()); return; } @@ -489,7 +492,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.isOpen() !== true) { + if (this.isOpen !== true) { if (next) next(new PortNotOpenError()); return; } @@ -540,7 +543,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.isOpen() !== true) { + if (this.isOpen !== true) { if (next) next(new PortNotOpenError()); return; } @@ -587,7 +590,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.isOpen() !== true) { + if (this.isOpen !== true) { if (next) next(new PortNotOpenError()); return; } @@ -649,7 +652,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.isOpen() !== true) { + if (this.isOpen !== true) { if (next) next(new PortNotOpenError()); return; } diff --git a/ports/asciiport.js b/ports/asciiport.js index 2be1a18..4921938 100644 --- a/ports/asciiport.js +++ b/ports/asciiport.js @@ -170,6 +170,18 @@ var AsciiPort = function(path, options) { }); EventEmitter.call(this); + + /** + * Check if port is open. + * + * @returns {boolean} + */ + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + return this._client.isOpen; + } + }); }; util.inherits(AsciiPort, EventEmitter); @@ -191,15 +203,6 @@ AsciiPort.prototype.close = function(callback) { this._client.close(callback); }; -/** - * Check if port is open. - * - * @returns {boolean} - */ -AsciiPort.prototype.isOpen = function() { - return this._client.isOpen(); -}; - /** * Send data to a modbus slave. * diff --git a/ports/c701port.js b/ports/c701port.js index 1424a73..8bc92cd 100644 --- a/ports/c701port.js +++ b/ports/c701port.js @@ -105,6 +105,18 @@ var UdpPort = function(ip, options) { }); EventEmitter.call(this); + + /** + * Check if port is open. + * + * @returns {boolean} + */ + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + return this.openFlag; + } + }); }; util.inherits(UdpPort, EventEmitter); @@ -129,15 +141,6 @@ UdpPort.prototype.close = function(callback) { callback(null); }; -/** - * Check if port is open. - * - * @returns {boolean} - */ -UdpPort.prototype.isOpen = function() { - return this.openFlag; -}; - /** * Send data to a modbus-tcp slave. * diff --git a/ports/rtubufferedport.js b/ports/rtubufferedport.js index fdf2fac..b718fc2 100644 --- a/ports/rtubufferedport.js +++ b/ports/rtubufferedport.js @@ -78,6 +78,18 @@ var RTUBufferedPort = function(path, options) { }); EventEmitter.call(this); + + /** + * Check if port is open. + * + * @returns {boolean} + */ + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + return this._client.isOpen; + } + }); }; util.inherits(RTUBufferedPort, EventEmitter); @@ -115,15 +127,6 @@ RTUBufferedPort.prototype.close = function(callback) { this._client.close(callback); }; -/** - * Check if port is open. - * - * @returns {boolean} - */ -RTUBufferedPort.prototype.isOpen = function() { - return this._client.isOpen(); -}; - /** * Send data to a modbus slave. * diff --git a/ports/tcpport.js b/ports/tcpport.js index 38a433a..44aed25 100644 --- a/ports/tcpport.js +++ b/ports/tcpport.js @@ -95,6 +95,18 @@ var TcpPort = function(ip, options) { }); EventEmitter.call(this); + + /** + * Check if port is open. + * + * @returns {boolean} + */ + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + return this.openFlag; + } + }); }; util.inherits(TcpPort, EventEmitter); @@ -118,15 +130,6 @@ TcpPort.prototype.close = function(callback) { this._client.end(); }; -/** - * Check if port is open. - * - * @returns {boolean} - */ -TcpPort.prototype.isOpen = function() { - return this.openFlag; -}; - /** * Send data to a modbus-tcp slave. * diff --git a/ports/tcprtubufferedport.js b/ports/tcprtubufferedport.js index 5e25349..bea8360 100644 --- a/ports/tcprtubufferedport.js +++ b/ports/tcprtubufferedport.js @@ -112,6 +112,18 @@ var TcpRTUBufferedPort = function(ip, options) { }); EventEmitter.call(this); + + /** + * Check if port is open. + * + * @returns {boolean} + */ + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + return this.openFlag; + } + }); }; util.inherits(TcpRTUBufferedPort, EventEmitter); @@ -166,15 +178,6 @@ TcpRTUBufferedPort.prototype.close = function(callback) { this._client.end(); }; -/** - * Check if port is open. - * - * @returns {boolean} - */ -TcpRTUBufferedPort.prototype.isOpen = function() { - return this.openFlag; -}; - /** * Send data to a modbus slave via telnet server. * diff --git a/ports/telnetport.js b/ports/telnetport.js index a241109..469cfbc 100644 --- a/ports/telnetport.js +++ b/ports/telnetport.js @@ -100,6 +100,18 @@ var TelnetPort = function(ip, options) { }); EventEmitter.call(this); + + /** + * Check if port is open. + * + * @returns {boolean} + */ + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + return this.openFlag; + } + }); }; util.inherits(TelnetPort, EventEmitter); @@ -140,15 +152,6 @@ TelnetPort.prototype.close = function(callback) { this._client.end(); }; -/** - * Check if port is open. - * - * @returns {boolean} - */ -TelnetPort.prototype.isOpen = function() { - return this.openFlag; -}; - /** * Send data to a modbus slave via telnet server. * diff --git a/ports/testport.js b/ports/testport.js index 0dd4bc1..48993a4 100644 --- a/ports/testport.js +++ b/ports/testport.js @@ -32,6 +32,18 @@ var TestPort = function() { this._coils = 0x0000; // TODO 0xa12b, 1010 0001 0010 1011 EventEmitter.call(this); + + /** + * Check if port is open. + * + * @returns {boolean} + */ + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + return true; + } + }); }; util.inherits(TestPort, EventEmitter); @@ -55,15 +67,6 @@ TestPort.prototype.close = function(callback) { callback(null); }; -/** - * Check if port is open. - * - * @returns {boolean} - */ -TestPort.prototype.isOpen = function() { - return true; -}; - /** * Simulate successful/failure port requests and replays. * diff --git a/test/mocks/SerialPortMock.js b/test/mocks/SerialPortMock.js index 9550cb9..9e64f01 100644 --- a/test/mocks/SerialPortMock.js +++ b/test/mocks/SerialPortMock.js @@ -13,6 +13,13 @@ var SerialPortMock = function(path, options, callback) { if (callback) { callback(null); } + + Object.defineProperty(this, "isOpen", { + enumerable: true, + get: function() { + return this._openFlag; + } + }); }; util.inherits(SerialPortMock, EventEmitter); @@ -24,10 +31,6 @@ SerialPortMock.prototype.open = function(callback) { this.emit("open"); }; -SerialPortMock.prototype.isOpen = function() { - return this._openFlag; -}; - SerialPortMock.prototype.write = function(buffer, callback) { this._data = buffer; if (callback) { diff --git a/test/ports/asciiport.test.js b/test/ports/asciiport.test.js index 6785d87..19c184b 100644 --- a/test/ports/asciiport.test.js +++ b/test/ports/asciiport.test.js @@ -31,12 +31,12 @@ describe("Modbus Ascii port", function() { describe("#isOpen", function() { it("should not be open before #open", function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; }); it("should be open after #open", function(done) { port.open(function() { - expect(port.isOpen()).to.be.true; + expect(port.isOpen).to.be.true; done(); }); }); @@ -44,7 +44,7 @@ describe("Modbus Ascii port", function() { it("should not be open after #close", function(done) { port.open(function() { port.close(function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; done(); }); }); diff --git a/test/ports/c701port.test.js b/test/ports/c701port.test.js index 75901b2..5a345aa 100644 --- a/test/ports/c701port.test.js +++ b/test/ports/c701port.test.js @@ -26,13 +26,13 @@ describe("Modbus UDP port", function() { describe("#isOpen", function() { it("should not be open before #open", function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; }); it("should be open after onListening", function(done) { port._client.listen(); setTimeout(function() { - expect(port.isOpen()).to.be.true; + expect(port.isOpen).to.be.true; done(); }); }); @@ -41,7 +41,7 @@ describe("Modbus UDP port", function() { port._client.listen(); setTimeout(function() { port.close(function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; done(); }); }); diff --git a/test/ports/rtubufferedport.test.js b/test/ports/rtubufferedport.test.js index 563f117..acd6197 100644 --- a/test/ports/rtubufferedport.test.js +++ b/test/ports/rtubufferedport.test.js @@ -31,12 +31,12 @@ describe("Modbus RTU buffered port", function() { describe("#isOpen", function() { it("should not be open before #open", function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; }); it("should be open after #open", function(done) { port.open(function() { - expect(port.isOpen()).to.be.true; + expect(port.isOpen).to.be.true; done(); }); }); @@ -44,7 +44,7 @@ describe("Modbus RTU buffered port", function() { it("should not be open after #close", function(done) { port.open(function() { port.close(function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; done(); }); }); diff --git a/test/ports/tcpport.test.js b/test/ports/tcpport.test.js index 406da18..56d24a8 100644 --- a/test/ports/tcpport.test.js +++ b/test/ports/tcpport.test.js @@ -26,12 +26,12 @@ describe("Modbus TCP port", function() { describe("#isOpen", function() { it("should not be open before #open", function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; }); it("should be open after #open", function(done) { port.open(function() { - expect(port.isOpen()).to.be.true; + expect(port.isOpen).to.be.true; done(); }); }); @@ -40,7 +40,7 @@ describe("Modbus TCP port", function() { port.open(function() { port.close(function() { setTimeout(function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; done(); }); }); diff --git a/test/ports/tcpportrtubuffered.test.js b/test/ports/tcpportrtubuffered.test.js index 31c5420..b5485ee 100644 --- a/test/ports/tcpportrtubuffered.test.js +++ b/test/ports/tcpportrtubuffered.test.js @@ -31,12 +31,12 @@ describe("Modbus TCP RTU buffered port", function() { describe("#isOpen", function() { it("should not be open before #open", function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; }); it("should be open after #open", function(done) { port.open(function() { - expect(port.isOpen()).to.be.true; + expect(port.isOpen).to.be.true; done(); }); }); @@ -44,7 +44,7 @@ describe("Modbus TCP RTU buffered port", function() { it("should not be open after #close", function(done) { port.open(function() { port.close(function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; done(); }); }); diff --git a/test/ports/telnetport.test.js b/test/ports/telnetport.test.js index 64c743d..1af0bab 100644 --- a/test/ports/telnetport.test.js +++ b/test/ports/telnetport.test.js @@ -31,12 +31,12 @@ describe("Modbus Telnet port", function() { describe("#isOpen", function() { it("should not be open before #open", function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; }); it("should be open after #open", function(done) { port.open(function() { - expect(port.isOpen()).to.be.true; + expect(port.isOpen).to.be.true; done(); }); }); @@ -44,7 +44,7 @@ describe("Modbus Telnet port", function() { it("should not be open after #close", function(done) { port.open(function() { port.close(function() { - expect(port.isOpen()).to.be.false; + expect(port.isOpen).to.be.false; done(); }); });