diff --git a/.eslintrc.json b/.eslintrc.json index 59d8e1e..bd27ab6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -58,6 +58,7 @@ "func-name-matching": "error", "func-names": "off", "func-style": "off", + "for-direction": "error", "generator-star-spacing": "error", "global-require": "off", "guard-for-in": "error", @@ -141,7 +142,7 @@ "no-mixed-operators": "off", "no-mixed-requires": "error", "no-multi-assign": "error", - "no-multi-spaces": "error", + "no-multi-spaces": "off", "no-multi-str": "off", "no-multiple-empty-lines": "off", "no-native-reassign": "error", diff --git a/examples/debug.js b/examples/debug.js index 52ad133..938716a 100755 --- a/examples/debug.js +++ b/examples/debug.js @@ -34,7 +34,7 @@ function setClient() { } function run() { - // read the 4 registers starting at address 5 + // read the 4 registers starting at address 5 client.readHoldingRegisters(5, 4) .then(function(d) { console.log("Receive:", d.data); }) diff --git a/examples/write_complete.js b/examples/write_complete.js index bcc2579..5679ccb 100755 --- a/examples/write_complete.js +++ b/examples/write_complete.js @@ -15,17 +15,17 @@ client.connectTCP("127.0.0.1", { port: 8502 }) console.log(e.message); }); function setClient() { - // set the client's unit id - // set a timout for requests default is null (no timeout) + // set the client's unit id + // set a timout for requests default is null (no timeout) client.setID(1); client.setTimeout(2000); - // run program + // run program run(); } function run() { - // write to coil + // write to coil client.writeCoils(1, [true, false, true, false, true, true, false, true]) .then(function(d) { console.log("Write to coils", d); }) @@ -65,7 +65,7 @@ function writeDiscreteCoils() { } function writeRegisters() { - // write 5 registers statrting at input registers + // write 5 registers statrting at input registers client.writeRegisters(1, [100, 90, 80, -200 + 65535, -100 + 65535]) .then(function(d) { console.log("Write 100, 90, 80, -200, -100 to input registers", d); }) diff --git a/gulpfile.js b/gulpfile.js index b7790ad..7a66239 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,64 +1,64 @@ -'use strict'; +"use strict"; -var gulp = require('gulp'); -var pump = require('pump'); -const jsdoc = require('gulp-jsdoc3'); -const clean = require('gulp-clean'); +var gulp = require("gulp"); +var pump = require("pump"); +const jsdoc = require("gulp-jsdoc3"); +const clean = require("gulp-clean"); -gulp.task('default', function() { +gulp.task("default", function() { // place code for your default task here }); -gulp.task('docs', ['doc', 'docExamples']); -gulp.task('build', ['apis', 'ports', 'servers', 'utils']); -gulp.task('publish', ['build', 'docs']); +gulp.task("docs", ["doc", "docExamples"]); +gulp.task("build", ["apis", "ports", "servers", "utils"]); +gulp.task("publish", ["build", "docs"]); -gulp.task('clean', function() { - return gulp.src(['modbus-serial', 'docs/gen']) - .pipe(clean({force: true})) +gulp.task("clean", function() { + return gulp.src(["modbus-serial", "docs/gen"]) + .pipe(clean({ force: true })); }); -gulp.task('doc', function(cb) { - gulp.src(['README.md', 'apis/**/*.js', 'ports/**/*.js', 'servers/**/*.js', 'utils/**/*.js'], {read: false}) - .pipe(jsdoc(cb)) +gulp.task("doc", function(cb) { + gulp.src(["README.md", "apis/**/*.js", "ports/**/*.js", "servers/**/*.js", "utils/**/*.js"], { read: false }) + .pipe(jsdoc(cb)); }); -gulp.task('docExamples', function() { - return gulp.src('examples/**/*').pipe(gulp.dest('docs/gen/examples')) +gulp.task("docExamples", function() { + return gulp.src("examples/**/*").pipe(gulp.dest("docs/gen/examples")); }); -gulp.task('apis', function(cb) { +gulp.task("apis", function(cb) { pump([ - gulp.src('apis/**/*.js'), - gulp.dest('modbus-serial/apis') + gulp.src("apis/**/*.js"), + gulp.dest("modbus-serial/apis") ], cb - ) + ); }); -gulp.task('ports', function(cb) { +gulp.task("ports", function(cb) { pump([ - gulp.src('ports/**/*.js'), - gulp.dest('modbus-serial/ports') + gulp.src("ports/**/*.js"), + gulp.dest("modbus-serial/ports") ], cb - ) + ); }); -gulp.task('servers', function(cb) { +gulp.task("servers", function(cb) { pump([ - gulp.src('servers/**/*.js'), - gulp.dest('modbus-serial/servers') + gulp.src("servers/**/*.js"), + gulp.dest("modbus-serial/servers") ], cb - ) + ); }); -gulp.task('utils', function(cb) { +gulp.task("utils", function(cb) { pump([ - gulp.src('utils/**/*.js'), - gulp.dest('modbus-serial/utils') + gulp.src("utils/**/*.js"), + gulp.dest("modbus-serial/utils") ], cb - ) + ); }); diff --git a/index.js b/index.js index 6324c8c..35aedb1 100644 --- a/index.js +++ b/index.js @@ -647,7 +647,7 @@ ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) { var dataBytes = Math.ceil(array.length / 8); var codeLength = 7 + dataBytes; - var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes + var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes buf.writeUInt8(address, 0); buf.writeUInt8(code, 1); diff --git a/package.json b/package.json index 4ea0416..e95063f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "modbus-serial", - "version": "7.4.2", + "version": "7.4.3-no-serial-port", "description": "A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.", "main": "index.js", "scripts": { @@ -25,19 +25,19 @@ }, "homepage": "https://github.com/yaacov/node-modbus-serial#readme", "devDependencies": { - "chai": "^3.5.0", - "eslint": "^3.16.1", - "gulp": "^3.9.1", - "gulp-clean": "^0.3.2", - "gulp-jsdoc3": "^1.0.1", - "mocha": "^3.2.0", - "mocha-eslint": "^3.0.1", - "mockery": "^2.0.0", - "pump": "^1.0.2", - "sinon": "^1.17.7" + "chai": "^4.2.0", + "eslint": "^5.12.1", + "gulp": "^4.0.0", + "gulp-clean": "^0.4.0", + "gulp-jsdoc3": "^2.0.0", + "mocha": "^5.2.0", + "mocha-eslint": "^5.0.0", + "mockery": "^2.1.0", + "pump": "^3.0.0", + "serialport": "^7.1.3", + "sinon": "^7.2.2" }, "dependencies": { - "debug": "^3.0.1", - "serialport": "^6.1.1" + "debug": "^4.1.1" } } diff --git a/servers/servertcp.js b/servers/servertcp.js index 99bc16c..fa885d6 100644 --- a/servers/servertcp.js +++ b/servers/servertcp.js @@ -23,7 +23,8 @@ var modbusSerialDebug = require("debug")("modbus-serial"); var HOST = "127.0.0.1"; var UNIT_ID = 255; // listen to all adresses var MODBUS_PORT = 502; - // Not really its official length, but we parse UnitID as part of PDU + +// Not really its official length, but we parse UnitID as part of PDU const MBAP_LEN = 6; /* Get Handlers @@ -211,12 +212,24 @@ var ServerTCP = function(vector, options) { // create a server unit id var serverUnitID = options.unitID || UNIT_ID; + // remember open sockets + modbus.socks = new Map(); + modbus._server.on("connection", function(sock) { + modbus.socks.set(sock, 0); + modbusSerialDebug({ action: "connected", address: sock.address(), remoteAddress: sock.remoteAddress, - remotePort: sock.remotePort + localPort: sock.localPort + }); + + sock.once("close", function() { + modbusSerialDebug({ + action: "closed" + }); + modbus.socks.delete(sock); }); sock.on("data", function(data) { @@ -296,10 +309,16 @@ util.inherits(ServerTCP, EventEmitter); * @param callback */ ServerTCP.prototype.close = function(callback) { + const modbus = this; + // close the net port if exist - if (this._server) { - this._server.removeAllListeners("data"); - this._server.close(callback); + if (modbus._server) { + modbus._server.removeAllListeners("data"); + modbus._server.close(callback); + + modbus.socks.forEach(function(e, sock) { + sock.destroy(); + }); modbusSerialDebug({ action: "close server" }); } else { diff --git a/servers/servertcp_handler.js b/servers/servertcp_handler.js index 6512b4a..7d7fc68 100644 --- a/servers/servertcp_handler.js +++ b/servers/servertcp_handler.js @@ -44,16 +44,14 @@ function _errorRequestBufferLength(requestBuffer) { */ function _handlePromiseOrValue(promiseOrValue, cb) { if (promiseOrValue && promiseOrValue.then && typeof promiseOrValue.then === "function") { - promiseOrValue.then(function(value) { - cb(null, value); - }); - if (promiseOrValue.catch && typeof promiseOrValue.catch === "function") { - promiseOrValue.catch(function(err) { + promiseOrValue + .then(function(value) { + cb(null, value); + }) + .catch(function(err) { cb(err); }); - } - } - else { + } else { cb(null, promiseOrValue); } } diff --git a/test/servers/servertcp.test.js b/test/servers/servertcp.test.js index 172e2ca..29b1d71 100644 --- a/test/servers/servertcp.test.js +++ b/test/servers/servertcp.test.js @@ -17,20 +17,20 @@ describe("Modbus TCP Server (no serverID)", function() { if (addr === 62) throw new Error(); - console.log("Holding register: ", addr); + console.log("\tHolding register: ", addr); return addr + 8000; }, getCoil: function(addr) { - console.log("Holding register: ", addr); + console.log("\tHolding register: ", addr); return (addr % 2) === 0; }, setRegister: function(addr, value) { - console.log("set register", addr, value); + console.log("\tset register", addr, value); return; }, setCoil: function(addr, value) { - console.log("set coil", addr, value); + console.log("\tset coil", addr, value); return; } }; @@ -51,6 +51,8 @@ describe("Modbus TCP Server (no serverID)", function() { client.once("data", function(data) { // FC05 - valid responce expect(data.toString("hex")).to.equal("00010000000601050005ff00"); + + client.end(); done(); }); }); @@ -68,6 +70,8 @@ describe("Modbus TCP Server (no serverID)", function() { client.once("data", function(data) { // A valid error message, code 0x01 - Illegal fanction expect(data.toString("hex")).to.equal("000100000003018701"); + + client.end(); done(); }); }); @@ -81,6 +85,8 @@ describe("Modbus TCP Server (no serverID)", function() { client.once("data", function(data) { // A valid error message, code 0x04 - Slave failure expect(data.toString("hex")).to.equal("000100000003018304"); + + client.end(); done(); }); }); @@ -98,6 +104,7 @@ describe("Modbus TCP Server (no serverID)", function() { serverTCP.on("socketError", function() { // Error handled correctly + client.end(); done(); }); }); @@ -115,6 +122,8 @@ describe("Modbus TCP Server (no serverID)", function() { client.once("data", function(data) { // A valid error message, code 0x04 - Slave failure expect(data.toString("hex")).to.equal("000100000003018104"); + + client.end(); done(); }); }); @@ -128,6 +137,8 @@ describe("Modbus TCP Server (no serverID)", function() { client.once("data", function(data) { // A valid error message, code 0x04 - Slave failure expect(data.toString("hex")).to.equal("000100000003018304"); + + client.end(); done(); }); }); @@ -141,6 +152,8 @@ describe("Modbus TCP Server (no serverID)", function() { client.once("data", function(data) { // A valid error message, code 0x04 - Slave failure expect(data.toString("hex")).to.equal("000100000003018404"); + + client.end(); done(); }); }); @@ -155,7 +168,7 @@ describe("Modbus TCP Server (serverID = requestID)", function() { before(function() { var vector = { setCoil: function(addr, value) { - console.log("set coil", addr, value); + console.log("\tset coil", addr, value); return; } }; @@ -176,6 +189,8 @@ describe("Modbus TCP Server (serverID = requestID)", function() { client.once("data", function(data) { // FC05 - valid responce expect(data.toString("hex")).to.equal("00010000000604050005ff00"); + + client.end(); done(); }); }); @@ -188,7 +203,7 @@ describe("Modbus TCP Server (serverID != requestID)", function() { before(function() { var vector = { setCoil: function(addr, value) { - console.log("set coil", addr, value); + console.log("\tset coil", addr, value); return; } }; @@ -215,6 +230,8 @@ describe("Modbus TCP Server (serverID != requestID)", function() { // FC05 - we expect no data for wrong unitID expect(data.toString("hex")).to.equal("NO DATA"); + + client.end(); done(); }); }); diff --git a/test/servers/servertcpCallback.test.js b/test/servers/servertcpCallback.test.js index 30c187d..6bf1592 100644 --- a/test/servers/servertcpCallback.test.js +++ b/test/servers/servertcpCallback.test.js @@ -37,7 +37,7 @@ describe("Modbus TCP Server Callback", function() { }, 50); }, setCoil: function(addr, value, unit, callback) { - setTimeout(function() {console.log("set coil", addr, value); + setTimeout(function() {console.log("\tset coil", addr, value); callback(null); }, 50); } diff --git a/test/servers/servertcpPromise.test.js b/test/servers/servertcpPromise.test.js index 0627e42..bfb2740 100644 --- a/test/servers/servertcpPromise.test.js +++ b/test/servers/servertcpPromise.test.js @@ -37,7 +37,7 @@ describe("Modbus TCP Server Promise", function() { setRegister: function(addr, value) { return new Promise(function(resolve) { setTimeout(function() { - console.log("set register", addr, value); + console.log("\tset register", addr, value); resolve(); }, 50); }); @@ -45,7 +45,7 @@ describe("Modbus TCP Server Promise", function() { setCoil: function(addr, value) { return new Promise(function(resolve) { setTimeout(function() { - console.log("set coil", addr, value); + console.log("\tset coil", addr, value); resolve(); }, 50); });