Skip to content

Commit

Permalink
Merge pull request #250 from yaacov/no-serialport
Browse files Browse the repository at this point in the history
force close server, catch server callback timeouts, do not force serialport import
  • Loading branch information
yaacov authored Jan 19, 2019
2 parents 7a586ef + 21ee520 commit 1c0c827
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion examples/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); })
Expand Down
10 changes: 5 additions & 5 deletions examples/write_complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); })
Expand Down Expand Up @@ -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); })
Expand Down
66 changes: 33 additions & 33 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -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
)
);
});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
}
29 changes: 24 additions & 5 deletions servers/servertcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions servers/servertcp_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Loading

0 comments on commit 1c0c827

Please sign in to comment.