Socket
Socket
Sign inDemoInstall

node-modbus

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-modbus - npm Package Compare versions

Comparing version 2.0.8 to 2.1.0

6

node-modbus/handler/client/ReadCoils.js

@@ -18,3 +18,9 @@ var Stampit = require('stampit'),

this.log.debug("handeling read coils response.");
this.log.debug("on read coils got PDU: " + JSON.stringify(pdu) + ' request: ' + JSON.stringify(request));
if(pdu.length < 2) {
request.defer.reject();
return;
}
var fc = pdu.readUInt8(0),

@@ -21,0 +27,0 @@ byteCount = pdu.readUInt8(1),

@@ -18,3 +18,9 @@ var Stampit = require('stampit'),

this.log.debug("handeling read discrete inputs response.");
this.log.debug("on read discrete inputs got PDU: " + JSON.stringify(pdu) + ' request: ' + JSON.stringify(request));
if(pdu.length < 2) {
request.defer.reject();
return;
}
var fc = pdu.readUInt8(0),

@@ -21,0 +27,0 @@ byteCount = pdu.readUInt8(1),

@@ -19,2 +19,7 @@ var Stampit = require('stampit'),

if(pdu.length < 2) {
request.defer.reject();
return;
}
var fc = pdu.readUInt8(0),

@@ -21,0 +26,0 @@ byteCount = pdu.readUInt8(1);

@@ -18,3 +18,9 @@ var Stampit = require('stampit'),

this.log.debug("handling read input registers response.");
this.log.debug("on read input registers got PDU: " + JSON.stringify(pdu) + ' request: ' + JSON.stringify(request));
if(pdu.length < 2) {
request.defer.reject();
return;
}
var fc = pdu.readUInt8(0),

@@ -21,0 +27,0 @@ byteCount = pdu.readUInt8(1);

@@ -18,3 +18,9 @@ var stampit = require('stampit'),

this.log.debug("handling multiple coils response.");
this.log.debug("on write multiple coils got PDU: " + JSON.stringify(pdu) + ' request: ' + JSON.stringify(request));
if(pdu.length < 5) {
request.defer.reject();
return;
}
var fc = pdu.readUInt8(0),

@@ -21,0 +27,0 @@ startAddress = pdu.readUInt16BE(1),

@@ -18,3 +18,9 @@ var stampit = require('stampit'),

this.log.debug("handling multiple registers response.");
this.log.debug("on write multiple registers got PDU: " + JSON.stringify(pdu) + ' request: ' + JSON.stringify(request));
if(pdu.length < 5) {
request.defer.reject();
return;
}
var fc = pdu.readUInt8(0),

@@ -21,0 +27,0 @@ startAddress = pdu.readUInt16BE(1),

@@ -17,3 +17,9 @@ var Stampit = require('stampit'),

this.log.debug("handling write single coil response.");
this.log.debug("on write single coil got PDU: " + JSON.stringify(pdu) + ' request: ' + JSON.stringify(request));
if(pdu.length < 5) {
request.defer.reject();
return;
}
var fc = pdu.readUInt8(0),

@@ -20,0 +26,0 @@ outputAddress = pdu.readUInt16BE(1),

@@ -18,3 +18,9 @@ var Stampit = require('stampit'),

this.log.debug("handling write single register response.");
this.log.debug("on write single register got PDU: " + JSON.stringify(pdu) + ' request: ' + JSON.stringify(request));
if(pdu.length < 5) {
request.defer.reject();
return;
}
var fc = pdu.readUInt8(0),

@@ -21,0 +27,0 @@ registerAddress = pdu.readUInt16BE(1),

33

node-modbus/modbus-client-core.js

@@ -38,2 +38,6 @@

if (!this.endianType) {
this.endianType = 'LITTLE';
}
this.on('data', onData);

@@ -53,18 +57,20 @@ this.on('newState_ready', flush);

currentRequest = this.reqFifo.shift();
setRequestTimeout();
this.setState('waiting');
this.emit('send', currentRequest.pdu);
this.log.debug('Data flushed.');
}.bind(this);
var setRequestTimeout = function () {
currentRequest.timeout = setTimeout(function () {
currentRequest.defer.reject({ err: 'timeout' });
this.emit('trashCurrentRequest');
this.logError('Request timed out.');
this.logError('Request timed out after ' + this.timeout / 1000 + ' sec');
this.setState('error');
}.bind(this), this.timeout);
this.setState('waiting');
this.emit('send', currentRequest.pdu);
this.log.debug('Data flushed.');
}.bind(this);

@@ -96,3 +102,7 @@

// pdu describes an error
if(pdu.length < 2) {
currentRequest.defer.reject('PDU length ' + pdu.length + ' invalid');
return true;
}
var exceptionCode = pdu.readUInt8(1),

@@ -120,3 +130,3 @@ message = ExceptionMessage[exceptionCode];

this.log.debug('received data');
this.log.debug('received data PDU: ' + JSON.stringify(pdu));

@@ -128,3 +138,2 @@ if (!currentRequest) {

clearTimeout(currentRequest.timeout);

@@ -131,0 +140,0 @@

var stampit = require('stampit'),
crc = require('crc'),
Put = require('put'),

@@ -37,2 +38,6 @@ ModbusCore = require('./modbus-client-core.js');

if (!this.connectionType) {
this.connectionType = 'RTU';
}
// TODO: flowControl - ['xon', 'xoff', 'xany', 'rtscts']

@@ -42,10 +47,25 @@

serialport = new SerialPort(this.portName, {
baudRate: this.baudRate,
parity: this.parity,
dataBits: this.dataBits,
stopBits: this.stopBits
});
if(this.connectionType === 'ASCII') {
serialport = new SerialPort(this.portName, {
baudRate: this.baudRate,
parity: this.parity,
dataBits: this.dataBits,
stopBits: this.stopBits,
parser: (this.endianType === 'BIG') ? SerialPort.parsers.byteDelimiter([13,10]) : SerialPort.parsers.byteDelimiter([10,13])
});
} else {
serialport = new SerialPort(this.portName, {
baudRate: this.baudRate,
parity: this.parity,
dataBits: this.dataBits,
stopBits: this.stopBits
});
}
this.log.debug('serialport settings: ' + JSON.stringify(serialport.options));
serialport.on('open', onOpen);
serialport.on('disconnect', onDisconnect);
serialport.on('close', onClose);

@@ -55,2 +75,3 @@ serialport.on('data', onData);

this.log.debug('set on send method');
this.on('send', onSend);

@@ -67,2 +88,11 @@

var onDisconnect = function (err) {
this.setState('disconnected');
if(err) {
this.emit('error', err);
}
}.bind(this);
var onClose = function () {

@@ -78,3 +108,5 @@

this.emit('data', pdu);
if( crc.crc16modbus(pdu) === 0 ) { /* PDU is valid if CRC across whole PDU equals 0, else ignore and do nothing */
this.emit('data', pdu.slice(1));
}

@@ -91,2 +123,4 @@ }.bind(this);

this.log.debug("PDU data" + JSON.stringify(pdu));
var pkt = Put()

@@ -96,21 +130,24 @@ .word8(this.unitId)

buf = pkt.buffer();
crc = 0;
var pktWithCRC;
for (var i = 0; i < buf.length; i += 1) {
crc = (buf.readUInt8(i) + crc) % 0xFFFF;
if(this.endianType === 'BIG') {
pktWithCRC = pkt.word16be(crc.crc16modbus(buf)).buffer();
}
pkt = pkt.word16be(crc).buffer();
else {
pktWithCRC = pkt.word16le(crc.crc16modbus(buf)).buffer();
}
for (var j = 0; j < pkt.length; j += 1) {
console.log(pkt.readUInt8(j).toString(16));
}
serialport.write(pkt, function (err) {
this.log.debug("PDU in " + this.endianType +
"-ENDIAN with CRC via " + this.connectionType + ": " + JSON.stringify(pktWithCRC));
serialport.write(pktWithCRC, function (err) {
if (err) {
this.log.error("PDU with CRC error on write to serial. " + JSON.stringify(err));
this.emit('error', err);
return;
}
else {
this.log.debug("PDU with CRC written to serial");
}

@@ -127,4 +164,3 @@ }.bind(this));

init();
});
{
"name": "node-modbus",
"version": "2.0.8",
"version": "2.1.0",
"description": "Sum of implementations for the Serial/TCP Modbus protocol.",

@@ -12,2 +12,3 @@ "author": "Klaus Landsdorf <klaus.landsdorf@bianco-royal.de>",

"dependencies": {
"crc": "^3.4.0",
"put": "0.0.6",

@@ -14,0 +15,0 @@ "q": "1.0.1",

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