Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

modbus-serial

Package Overview
Dependencies
Maintainers
1
Versions
123
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

modbus-serial - npm Package Compare versions

Comparing version 5.3.1 to 5.3.2

examples/buffertcp1.js

2

examples/buffertcp.js

@@ -9,3 +9,3 @@ /* eslint-disable no-console, spaced-comment */

// NPort Gateway 801D NetPort
client.connectTcpRTUBuffered("127.0.0.1", { port: 8502, removeCrc: false })
client.connectTcpRTUBuffered("127.0.0.1", { port: 8502 })
.then(setClient)

@@ -12,0 +12,0 @@ .then(function() {

@@ -17,1 +17,5 @@ /* eslint-disable no-console, no-unused-vars, spaced-comment */

var serverTCP = new ModbusRTU.ServerTCP(vector, { host: "0.0.0.0", port: 8502, debug: true, unitID: 1 });
serverTCP.on("socketError", function(err) {
console.error(err);
});

@@ -98,3 +98,2 @@ declare namespace ModbusRTU {

port?: number;
removeCrc?: boolean;
}

@@ -101,0 +100,0 @@

@@ -27,2 +27,5 @@ "use strict";

var BAD_ADDRESS_MESSAGE = "Bad Client Address";
var BAD_ADDRESS_ERRNO = "ECONNREFUSED";
var PortNotOpenError = function() {

@@ -35,2 +38,9 @@ Error.captureStackTrace(this, this.constructor);

var BadAddressError = function() {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = BAD_ADDRESS_MESSAGE;
this.errno = BAD_ADDRESS_ERRNO;
};
/**

@@ -383,2 +393,8 @@ * @fileoverview ModbusRTU module, exports the ModbusRTU class.

// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
// function code defaults to 2

@@ -437,2 +453,8 @@ code = code || 2;

// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
// function code defaults to 4

@@ -479,2 +501,8 @@ code = code || 4;

// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
var code = 5;

@@ -525,2 +553,8 @@

// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
var code = 6;

@@ -567,2 +601,8 @@

// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
var code = 15;

@@ -624,2 +664,8 @@ var i = 0;

// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
var code = 16;

@@ -626,0 +672,0 @@

{
"name": "modbus-serial",
"version": "5.3.1",
"version": "5.3.2",
"description": "A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -48,3 +48,2 @@ "use strict";

modbusSerialDebug({ action: "receive serial rtu buffered port", data: data, buffer: self._buffer });
modbusSerialDebug(JSON.stringify({ action: "receive serial rtu buffered port strings", data: data, buffer: self._buffer }));

@@ -57,2 +56,3 @@ // check data length

self._buffer = self._buffer.slice(-MAX_BUFFER_LENGTH);
bufferLength = MAX_BUFFER_LENGTH;
}

@@ -97,3 +97,2 @@

modbusSerialDebug({ action: "emit data serial rtu buffered port", buffer: buffer });
modbusSerialDebug(JSON.stringify({ action: "emit data serial rtu buffered port strings", buffer: buffer }));

@@ -181,9 +180,2 @@ this.emit("data", buffer);

});
modbusSerialDebug(JSON.stringify({
action: "send serial rtu buffered strings",
data: data,
unitid: this._id,
functionCode: this._cmd
}));
};

@@ -190,0 +182,0 @@

@@ -72,3 +72,3 @@ "use strict";

// debug
modbusSerialDebug({ action: "parsed tcp port", buffer: buffer });
modbusSerialDebug({ action: "parsed tcp port", buffer: buffer, transactionId: modbus._transactionIdRead });

@@ -153,8 +153,2 @@ // reset data

// set next transaction id
this._transactionIdWrite = (this._transactionIdWrite + 1) % MAX_TRANSACTIONS;
// send buffer to slave
this._client.write(buffer);
modbusSerialDebug({

@@ -169,10 +163,7 @@ action: "send tcp port",

modbusSerialDebug(JSON.stringify({
action: "send tcp port strings",
data: data,
buffer: buffer,
unitid: this._id,
functionCode: this._cmd,
transactionsId: this._transactionIdWrite
}));
// send buffer to slave
this._client.write(buffer);
// set next transaction id
this._transactionIdWrite = (this._transactionIdWrite + 1) % MAX_TRANSACTIONS;
};

@@ -179,0 +170,0 @@

@@ -11,6 +11,7 @@ "use strict";

/* TODO: const should be set once, maybe */
var EXCEPTION_LENGTH = 5;
var EXCEPTION_LENGTH = 3;
var MIN_DATA_LENGTH = 6;
var MIN_MBAP_LENGTH = 6;
var MAX_TRANSACTIONS = 64; // maximum transaction to wait for
var MAX_BUFFER_LENGTH = 256;
var CRC_LENGTH = 2;

@@ -29,18 +30,14 @@

var TcpRTUBufferedPort = function(ip, options) {
var self = this;
this.ip = ip;
this.openFlag = false;
this.callback = null;
this._transactionIdWrite = 1;
var modbus = this;
modbus.ip = ip;
modbus.openFlag = false;
modbus.callback = null;
modbus._transactionIdWrite = 1;
// options
if (typeof(options) === "undefined") options = {};
this.port = options.port || MODBUS_PORT;
this.removeCrc = options.removeCrc;
modbus.port = options.port || MODBUS_PORT;
// internal buffer
this._buffer = new Buffer(0);
this._id = 0;
this._cmd = 0;
this._length = 0;
modbus._buffer = new Buffer(0);

@@ -50,5 +47,5 @@ // handle callback - call a callback function only once, for the first event

var handleCallback = function(had_error) {
if (self.callback) {
self.callback(had_error);
self.callback = null;
if (modbus.callback) {
modbus.callback(had_error);
modbus.callback = null;
}

@@ -58,46 +55,46 @@ };

// create a socket
this._client = new net.Socket();
modbus._client = new net.Socket();
// register the port data event
this._client.on("data", function onData(data) {
var buffer;
// check data length
if (data.length < MIN_MBAP_LENGTH) return;
// cut 6 bytes of mbap
buffer = new Buffer(data.length - MIN_MBAP_LENGTH);
data.copy(buffer, 0, MIN_MBAP_LENGTH);
modbus._client.on("data", function onData(data) {
// add data to buffer
self._buffer = Buffer.concat([self._buffer, buffer]);
modbus._buffer = Buffer.concat([modbus._buffer, data]);
modbusSerialDebug({ action: "receive tcp rtu buffered port", data: data, buffer: buffer });
modbusSerialDebug(JSON.stringify({ action: "receive tcp rtu buffered port strings", data: data, buffer: buffer }));
modbusSerialDebug({ action: "receive tcp rtu buffered port", data: data, buffer: modbus._buffer });
// check if buffer include a complete modbus answer
var expectedLength = self._length;
var bufferLength = self._buffer.length + CRC_LENGTH;
var bufferLength = modbus._buffer.length;
modbusSerialDebug("on data expected length:" + expectedLength + " buffer length:" + bufferLength);
// check data length
if (bufferLength < MIN_MBAP_LENGTH) return;
// check buffer size for MAX_BUFFER_SIZE
if (bufferLength > MAX_BUFFER_LENGTH) {
modbus._buffer = modbus._buffer.slice(-MAX_BUFFER_LENGTH);
bufferLength = MAX_BUFFER_LENGTH;
}
// check data length
if (expectedLength < MIN_MBAP_LENGTH || bufferLength < EXCEPTION_LENGTH) return;
if (bufferLength < (MIN_MBAP_LENGTH + EXCEPTION_LENGTH)) return;
// loop and check length-sized buffer chunks
var maxOffset = bufferLength - EXCEPTION_LENGTH;
var maxOffset = bufferLength - MIN_MBAP_LENGTH;
for (var i = 0; i <= maxOffset; i++) {
var unitId = self._buffer[i];
var functionCode = self._buffer[i + 1];
modbus._transactionIdRead = modbus._buffer.readUInt16BE(i);
var protocolID = modbus._buffer.readUInt16BE(i + 2);
var msgLength = modbus._buffer.readUInt16BE(i + 4);
var cmd = modbus._buffer[i + 7];
if (unitId !== self._id) continue;
modbusSerialDebug(
{ protocolID: protocolID, msgLength: msgLength, bufferLength: bufferLength, cmd: cmd });
if (functionCode === self._cmd && i + expectedLength <= bufferLength) {
self._emitData(i, expectedLength);
if (protocolID === 0 &&
cmd !== 0 &&
msgLength >= EXCEPTION_LENGTH &&
(i + MIN_MBAP_LENGTH + msgLength) <= bufferLength) {
// add crc and emit
modbus._emitData((i + MIN_MBAP_LENGTH), msgLength);
return;
}
if (functionCode === (0x80 | self._cmd) && i + EXCEPTION_LENGTH <= bufferLength) {
self._emitData(i, EXCEPTION_LENGTH);
return;
}
}

@@ -107,3 +104,3 @@ });

this._client.on("connect", function() {
self.openFlag = true;
modbus.openFlag = true;
handleCallback();

@@ -113,3 +110,3 @@ });

this._client.on("close", function(had_error) {
self.openFlag = false;
modbus.openFlag = false;
handleCallback(had_error);

@@ -119,3 +116,3 @@ });

this._client.on("error", function(had_error) {
self.openFlag = false;
modbus.openFlag = false;
handleCallback(had_error);

@@ -136,9 +133,8 @@ });

TcpRTUBufferedPort.prototype._emitData = function(start, length) {
var modbus = this;
var data = modbus._buffer.slice(start, start + length);
var data = this._buffer.slice(start, start + length);
this._buffer = this._buffer.slice(start + length);
// cut the buffer
modbus._buffer = modbus._buffer.slice(start + length);
// update transaction id
this._transactionIdRead = data.readUInt16BE(0);
if (data.length > 0) {

@@ -152,11 +148,9 @@ var buffer = Buffer.alloc(data.length + CRC_LENGTH);

this.emit("data", buffer);
modbus.emit("data", buffer);
// debug
modbusSerialDebug({ action: "parsed tcp buffered port", buffer: buffer, transactionId: modbus._transactionIdRead });
} else {
modbusSerialDebug({ action: "emit data to short", data: data });
}
// reset internal vars
this._id = 0;
this._cmd = 0;
this._length = 0;
};

@@ -204,32 +198,2 @@

var length = 0;
// remember current unit and command
this._id = data[0];
this._cmd = data[1];
// calculate expected answer length
switch (this._cmd) {
case 1:
case 2:
length = data.readUInt16BE(4);
this._length = 3 + parseInt((length - 1) / 8 + 1) + 2;
break;
case 3:
case 4:
length = data.readUInt16BE(4);
this._length = 3 + 2 * length + 2;
break;
case 5:
case 6:
case 15:
case 16:
this._length = 6 + 2;
break;
default:
// raise and error ?
this._length = 0;
break;
}
// remove crc and add mbap

@@ -242,8 +206,2 @@ var buffer = new Buffer(data.length + MIN_MBAP_LENGTH - CRC_LENGTH);

// get next transaction id
this._transactionIdWrite = (this._transactionIdWrite + 1) % MAX_TRANSACTIONS;
// send buffer to slave
this._client.write(buffer);
modbusSerialDebug({

@@ -253,15 +211,10 @@ action: "send tcp rtu buffered port",

buffer: buffer,
unitid: this._id,
functionCode: this._cmd,
transactionsId: this._transactionIdWrite
});
modbusSerialDebug(JSON.stringify({
action: "send tcp rtu buffered port strings",
data: data,
buffer: buffer,
unitid: this._id,
functionCode: this._cmd,
transactionsId: this._transactionIdWrite
}));
// get next transaction id
this._transactionIdWrite = (this._transactionIdWrite + 1) % MAX_TRANSACTIONS;
// send buffer to slave
this._client.write(buffer);
};

@@ -268,0 +221,0 @@

@@ -165,4 +165,9 @@ # modbus-serial

var serverTCP = new ModbusRTU.ServerTCP(vector, { host: "0.0.0.0", port: 8502, debug: true, unitID: 1 });
serverTCP.on("socketError", function(err){
// Handle socket error if needed, can be ignored
console.error(err);
});
```
to get more see [Examples](https://github.com/yaacov/node-modbus-serial/wiki)

@@ -81,3 +81,3 @@ "use strict";

functionCode = parseInt(functionCode) | 0x80;
responseBuffer = new Buffer(3 + 2);
responseBuffer = Buffer.alloc(3 + 2);
responseBuffer.writeUInt8(errorCode, 2);

@@ -148,3 +148,3 @@

var dataBytes = parseInt((length - 1) / 8 + 1);
var responseBuffer = new Buffer(3 + dataBytes + 2);
var responseBuffer = Buffer.alloc(3 + dataBytes + 2);
responseBuffer.writeUInt8(dataBytes, 2);

@@ -180,3 +180,3 @@

// build answer
var responseBuffer = new Buffer(3 + length * 2 + 2);
var responseBuffer = Buffer.alloc(3 + length * 2 + 2);
responseBuffer.writeUInt8(length * 2, 2);

@@ -214,3 +214,3 @@

// build answer
var responseBuffer = new Buffer(3 + length * 2 + 2);
var responseBuffer = Buffer.alloc(3 + length * 2 + 2);
responseBuffer.writeUInt8(length * 2, 2);

@@ -245,3 +245,3 @@

// build answer
var responseBuffer = new Buffer(8);
var responseBuffer = Buffer.alloc(8);
responseBuffer.writeUInt16BE(address, 2);

@@ -275,3 +275,3 @@ responseBuffer.writeUInt16BE(state, 4);

// build answer
var responseBuffer = new Buffer(8);
var responseBuffer = Buffer.alloc(8);
responseBuffer.writeUInt16BE(address, 2);

@@ -306,3 +306,3 @@ responseBuffer.writeUInt16BE(value, 4);

// build answer
var responseBuffer = new Buffer(8);
var responseBuffer = Buffer.alloc(8);
responseBuffer.writeUInt16BE(address, 2);

@@ -341,3 +341,3 @@ responseBuffer.writeUInt16BE(length, 4);

// build answer
var responseBuffer = new Buffer(8);
var responseBuffer = Buffer.alloc(8);
responseBuffer.writeUInt16BE(address, 2);

@@ -385,3 +385,3 @@ responseBuffer.writeUInt16BE(length, 4);

// remove mbap and add crc16
var requestBuffer = new Buffer(data.length - 6 + 2);
var requestBuffer = Buffer.alloc(data.length - 6 + 2);
data.copy(requestBuffer, 0, 6);

@@ -408,3 +408,3 @@ var crc = crc16(requestBuffer.slice(0, -2));

// remove crc and add mbap
var outTcp = new Buffer(responseBuffer.length + 6 - 2);
var outTcp = Buffer.alloc(responseBuffer.length + 6 - 2);
outTcp.writeUInt16BE(transactionsId, 0);

@@ -422,2 +422,8 @@ outTcp.writeUInt16BE(0, 2);

});
sock.on("error", function(err) {
modbusSerialDebug(JSON.stringify({ action: "socket error", data: err }));
modbus.emit("socketError", err);
});
});

@@ -424,0 +430,0 @@ EventEmitter.call(this);

@@ -10,2 +10,11 @@ "use strict";

function send(buffer) {
port.open(function() {
setTimeout(function() {
port._client.receive(buffer);
port.close();
});
});
}
before(function() {

@@ -17,3 +26,3 @@ var mock = require("./../mocks/netMock");

var TcpRTUBufferedPort = require("./../../ports/tcprtubufferedport");
port = new TcpRTUBufferedPort("127.0.0.1", { port: 9999, removeCRC: true });
port = new TcpRTUBufferedPort("127.0.0.1", { port: 9999 });
});

@@ -25,6 +34,2 @@

afterEach(function() {
port.close();
});
describe("#isOpen", function() {

@@ -55,11 +60,6 @@ it("should not be open before #open", function() {

port.once("data", function(data) {
expect(data.toString("hex")).to.equal("01030802070207020702072637");
expect(data.toString("hex")).to.equal("1103667788994fa2");
done();
});
port.open(function() {
port.write(new Buffer("0103000500045408", "hex"));
setTimeout(function() {
port._client.receive(new Buffer("00010000000b0103080207020702070207", "hex"));
});
});
send(new Buffer("000100000006110366778899", "hex"));
});

@@ -69,15 +69,11 @@

port.once("data", function(data) {
expect(data.toString("hex")).to.equal("01030802070207020702072637");
expect(data.toString("hex")).to.equal("1103667788994fa2");
done();
});
port.open(function() {
port.write(new Buffer("0103000500045408", "hex"));
setTimeout(function() {
port._client.receive(new Buffer("00010000000b010308", "hex"));
port._client.receive(new Buffer("00010000000b0207020702070207", "hex"));
});
});
send(new Buffer("0002000000", "hex"));
send(new Buffer("0611036677", "hex"));
send(new Buffer("8899", "hex"));
});
it("should return a valid Modbus RTU exception", function(done) {
it("should return a valid Modbus TCP exception", function(done) {
port.once("data", function(data) {

@@ -87,12 +83,3 @@ expect(data.toString("hex")).to.equal("1183044136");

});
port.open(function() {
port.write(new Buffer("1103006B00037687", "hex"));
setTimeout(function() {
port._client.receive(new Buffer("00000000000611", "hex"));
port._client.receive(new Buffer("00000000000683", "hex"));
port._client.receive(new Buffer("00000000000604", "hex"));
port._client.receive(new Buffer("00000000000641", "hex"));
port._client.receive(new Buffer("00000000000636", "hex"));
});
});
send(new Buffer("000300000003118304", "hex"));
});

@@ -102,25 +89,6 @@

port.once("data", function(data) {
expect(data.toString("hex")).to.equal("110306ae415652434049ad");
expect(data.toString("hex")).to.equal("1103667788994fa2");
done();
});
port.open(function() {
port.write(new Buffer("1103006B00037687", "hex"));
setTimeout(function() {
port._client.receive(new Buffer("00000000000620", "hex")); // illegal char
port._client.receive(new Buffer("00000000000654", "hex")); // illegal char
port._client.receive(new Buffer("00000000000654", "hex")); // illegal char
port._client.receive(new Buffer("000000000006ff", "hex")); // illegal char
port._client.receive(new Buffer("00000000000611", "hex"));
port._client.receive(new Buffer("00000000000603", "hex"));
port._client.receive(new Buffer("00000000000606", "hex"));
port._client.receive(new Buffer("000000000006ae", "hex"));
port._client.receive(new Buffer("00000000000641", "hex"));
port._client.receive(new Buffer("00000000000656", "hex"));
port._client.receive(new Buffer("00000000000652", "hex"));
port._client.receive(new Buffer("00000000000643", "hex"));
port._client.receive(new Buffer("00000000000640", "hex"));
port._client.receive(new Buffer("00000000000649", "hex"));
port._client.receive(new Buffer("000000000006ad", "hex"));
});
});
send(new Buffer("3456000400000006110366778899", "hex"));
});

@@ -130,49 +98,7 @@

port.once("data", function(data) {
expect(data.toString("hex")).to.equal("110306ae415652434049ad");
expect(data.toString("hex")).to.equal("1103667788994fa2");
done();
});
port.open(function() {
port.write(new Buffer("1103006B00037687", "hex"));
setTimeout(function() {
port._client.receive(new Buffer("00000000000611", "hex"));
port._client.receive(new Buffer("00000000000603", "hex"));
port._client.receive(new Buffer("00000000000606", "hex"));
port._client.receive(new Buffer("000000000006ae", "hex"));
port._client.receive(new Buffer("00000000000641", "hex"));
port._client.receive(new Buffer("00000000000656", "hex"));
port._client.receive(new Buffer("00000000000652", "hex"));
port._client.receive(new Buffer("00000000000643", "hex"));
port._client.receive(new Buffer("00000000000640", "hex"));
port._client.receive(new Buffer("00000000000649", "hex"));
port._client.receive(new Buffer("000000000006ad", "hex"));
port._client.receive(new Buffer("00000000000620", "hex")); // illegal char
port._client.receive(new Buffer("00000000000654", "hex")); // illegal char
port._client.receive(new Buffer("00000000000654", "hex")); // illegal char
port._client.receive(new Buffer("000000000006ff", "hex")); // illegal char
});
});
send(new Buffer("0005000000061103667788993456", "hex"));
});
it("should return a valid Modbus TCP RTU message on illegal chars", function(done) {
port.once("data", function(data) {
expect(data.toString("hex")).to.equal("110306ae415652434049ad");
done();
});
port.open(function() {
port.write(new Buffer("1103006B00037687", "hex"));
setTimeout(function() {
port._client.receive(new Buffer("00000000000611", "hex"));
port._client.receive(new Buffer("00000000000603", "hex"));
port._client.receive(new Buffer("00000000000606", "hex"));
port._client.receive(new Buffer("000000000006ae", "hex"));
port._client.receive(new Buffer("00000000000641", "hex"));
port._client.receive(new Buffer("00000000000656", "hex"));
port._client.receive(new Buffer("00000000000652", "hex"));
port._client.receive(new Buffer("00000000000643", "hex"));
port._client.receive(new Buffer("00000000000640", "hex"));
port._client.receive(new Buffer("00000000000649", "hex"));
port._client.receive(new Buffer("000000000006ad", "hex"));
});
});
});
});

@@ -183,5 +109,5 @@

port.write(new Buffer("0103000500045408", "hex"));
expect(port._client._data.toString("hex")).to.equal("000700000006010300050004");
expect(port._client._data.toString("hex")).to.equal("000100000006010300050004");
});
});
});

@@ -10,3 +10,2 @@ "use strict";

var serverTCP; // eslint-disable-line no-unused-vars
var sock;

@@ -24,13 +23,5 @@ before(function() {

beforeEach(function() {
sock = new net.Socket();
});
afterEach(function() {
sock.end();
});
describe("function code handler", function() {
it("should receive a valid Modbus TCP message", function(done) {
const client = net.connect({ host: "0.0.0.0", port: 8512 }, () => {
const client = net.connect({ host: "0.0.0.0", port: 8512 }, function() {
// FC05 - force single coil, to on 0xff00

@@ -52,3 +43,3 @@ client.write(new Buffer("00010000000601050005ff00", "hex"));

it("should receive a valid Modbus TCP message", function(done) {
const client = net.connect({ host: "0.0.0.0", port: 8512 }, () => {
const client = net.connect({ host: "0.0.0.0", port: 8512 }, function() {
// FC07 - unhandled function

@@ -67,2 +58,19 @@ client.write(new Buffer("000100000006010700000000", "hex"));

});
describe("socket connection error", function() {
it("should receive an error event on socket closed by client", function(done) {
var client = net.connect({ host: "0.0.0.0", port: 8512 }, function() {
client.destroy();
serverTCP.emit("socketError");
});
serverTCP.on("socketError", function() {
// Error handled correctly
done();
});
});
// TODO: exceptions
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc