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 3.5.2 to 3.6.2

utils/buffer_bit.js

72

apis/connection.js

@@ -31,4 +31,4 @@ 'use strict';

* @param {string} path the path to the Serial Port - required.
* @param {object} options - the serial port options - optional.
* @param {function} next the function to call next.
* @param {Object} options - the serial port options - optional.
* @param {Function} next the function to call next.
*/

@@ -44,10 +44,7 @@ cl.connectRTU = function (path, options, next) {

var SerialPort = require("serialport").SerialPort;
var serialPort = new SerialPort(path, options, false);
this._port = new SerialPort(path, options, false);
// re-set the serial port to use
this._port = serialPort;
// open and call next
this.open(next);
}
};

@@ -58,8 +55,6 @@ /**

* @param {string} ip the ip of the TCP Port - required.
* @param {object} options - the serial port options - optional.
* @param {function} next the function to call next.
* @param {Object} options - the serial port options - optional.
* @param {Function} next the function to call next.
*/
cl.connectTCP = function (ip, options, next) {
var port;
// check if we have options

@@ -73,10 +68,7 @@ if (typeof(next) == 'undefined' && typeof(options) == 'function') {

var TcpPort = require('../ports/tcpport');
var tcpPort = new TcpPort(ip, options);
this._port = new TcpPort(ip, options);
// re-set the port to use
this._port = tcpPort;
// open and call next
this.open(next);
}
};

@@ -87,8 +79,6 @@ /**

* @param {string} ip the ip of the TelnetPort - required.
* @param {object} options - the serial port options - optional.
* @param {function} next the function to call next.
* @param {Object} options - the serial port options - optional.
* @param {Function} next the function to call next.
*/
cl.connectTelnet = function (ip, options, next) {
var port;
// check if we have options

@@ -102,10 +92,7 @@ if (typeof(next) == 'undefined' && typeof(options) == 'function') {

var TelnetPort = require('../ports/telnetport');
var telnetPort = new TelnetPort(ip, options);
this._port = new TelnetPort(ip, options);
// re-set the port to use
this._port = telnetPort;
// open and call next
this.open(next);
}
};

@@ -116,8 +103,6 @@ /**

* @param {string} ip the ip of the TelnetPort - required.
* @param {object} options - the serial port options - optional.
* @param {function} next the function to call next.
* @param {Object} options - the serial port options - optional.
* @param {Function} next the function to call next.
*/
cl.connectC701 = function (ip, options, next) {
var port;
// check if we have options

@@ -131,10 +116,7 @@ if (typeof(next) == 'undefined' && typeof(options) == 'function') {

var C701Port = require('../ports/c701port');
var c701Port = new C701Port(ip, options);
this._port = new C701Port(ip, options);
// re-set the port to use
this._port = c701Port;
// open and call next
this.open(next);
}
};

@@ -145,4 +127,4 @@ /**

* @param {string} path the path to the Serial Port - required.
* @param {object} options - the serial port options - optional.
* @param {function} next the function to call next.
* @param {Object} options - the serial port options - optional.
* @param {Function} next the function to call next.
*/

@@ -158,10 +140,7 @@ cl.connectRTUBuffered = function (path, options, next) {

var SerialPort = require('../ports/rtubufferedport');
var serialPort = new SerialPort(path, options);
this._port = new SerialPort(path, options);
// re-set the serial port to use
this._port = serialPort;
// open and call next
this.open(next);
}
};

@@ -172,4 +151,4 @@ /**

* @param {string} path the path to the Serial Port - required.
* @param {object} options - the serial port options - optional.
* @param {function} next the function to call next.
* @param {Object} options - the serial port options - optional.
* @param {Function} next the function to call next.
*/

@@ -185,12 +164,9 @@ cl.connectAsciiSerial = function (path, options, next) {

var SerialPortAscii = require('../ports/asciiport');
var serialPortAscii = new SerialPortAscii(path, options);
this._port = new SerialPortAscii(path, options);
// re-set the serial port to use
this._port = serialPortAscii;
// open and call next
this.open(next);
}
}
};
module.exports = addConnctionAPI;

@@ -21,3 +21,3 @@ 'use strict';

*
* @param {function} f the function to convert
* @param {Function} f the function to convert
* @return a function that calls function "f" and return a promise.

@@ -53,6 +53,6 @@ */

}
}
};
return converted;
}
};

@@ -69,4 +69,4 @@ /**

// set/get unitID
cl.setID = function(id) {this._unitID = id;}
cl.getID = function() {return this._unitID;}
cl.setID = function(id) {this._unitID = id;};
cl.getID = function() {return this._unitID;};

@@ -81,4 +81,4 @@ // convert functions to return promises

cl.writeRegisters = convert(cl.writeFC16);
}
};
module.exports = addPromiseAPI;

@@ -20,3 +20,4 @@ 'use strict';

*/
require('./apis/buffer_bit')();
require('./utils/buffer_bit')();
var crc16 = require('./utils/crc16');

@@ -33,40 +34,7 @@ /**

/**
* Calculate buffer CRC16 and add it to the
* end of the buffer.
*
* @param {buffer} buf the data buffer.
* @param {number} length the length of the buffer without CRC.
*
* @return {number} the calculated CRC16
*/
function _CRC16(buf, length) {
var crc = 0xFFFF;
var tmp;
// calculate crc16
for (var i = 0; i < length; i++) {
crc = crc ^ buf[i];
for (var j = 0; j < 8; j++) {
tmp = crc & 0x0001;
crc = crc >> 1;
if (tmp) {
crc = crc ^ 0xA001;
}
}
}
// add to end of buffer
buf.writeUInt16LE(crc, length);
// return the crc
return crc;
}
/**
* Parse the data for a Modbus -
* Read Coils (FC=02, 01)
*
* @param {buffer} data the data buffer to parse.
* @param {function} next the function to call next.
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/

@@ -94,4 +62,4 @@ function _readFC2(data, next) {

*
* @param {buffer} data the data buffer to parse.
* @param {function} next the function to call next.
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/

@@ -115,4 +83,4 @@ function _readFC4(data, next) {

*
* @param {buffer} data the data buffer to parse.
* @param {function} next the function to call next.
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/

@@ -131,4 +99,4 @@ function _readFC5(data, next) {

*
* @param {buffer} data the data buffer to parse.
* @param {function} next the function to call next.
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/

@@ -147,4 +115,4 @@ function _readFC6(data, next) {

*
* @param {buffer} data the data buffer to parse.
* @param {function} next the function to call next.
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/

@@ -180,3 +148,3 @@ function _readFC16(data, next) {

*
* @param {function} callback the function to call next on open success
* @param {Function} callback the function to call next on open success
* of failure.

@@ -227,5 +195,3 @@ */

var crcIn = data.readUInt16LE(data.length - 2);
var crc = _CRC16(data, data.length - 2);
if (crcIn != crc) {
if (crcIn != crc16(data.slice(0, -2))) {
error = "CRC error";

@@ -322,7 +288,7 @@ if (next)

* @param {number} length the total number of coils requested.
* @param {function} next the function to call next.
* @param {Function} next the function to call next.
*/
ModbusRTU.prototype.writeFC1 = function (address, dataAddress, length, next) {
this.writeFC2(address, dataAddress, length, next, 1);
}
};

@@ -335,7 +301,7 @@ /**

* @param {number} length the total number of digital inputs requested.
* @param {function} next the function to call next.
* @param {Function} next the function to call next.
*/
ModbusRTU.prototype.writeFC2 = function (address, dataAddress, length, next, code) {
// function code defaults to 2
if (!code) code = 2;
code = code || 2;

@@ -357,7 +323,7 @@ // set state variables

// add crc bytes to buffer
_CRC16(buf, codeLength);
buf.writeUInt16LE(crc16(buf.slice(0, -2)), codeLength);
// write buffer to serial port
this._port.write(buf);
}
};

@@ -370,7 +336,7 @@ /**

* @param {number} length the total number of registers requested.
* @param {function} next the function to call next.
* @param {Function} next the function to call next.
*/
ModbusRTU.prototype.writeFC3 = function (address, dataAddress, length, next) {
this.writeFC4(address, dataAddress, length, next, 3);
}
};

@@ -383,7 +349,7 @@ /**

* @param {number} length the total number of registers requested.
* @param {function} next the function to call next.
* @param {Function} next the function to call next.
*/
ModbusRTU.prototype.writeFC4 = function (address, dataAddress, length, next, code) {
// function code defaults to 4
if (!code) code = 4;
code = code || 4;

@@ -405,7 +371,7 @@ // set state variables

// add crc bytes to buffer
_CRC16(buf, codeLength);
buf.writeUInt16LE(crc16(buf.slice(0, -2)), codeLength);
// write buffer to serial port
this._port.write(buf);
}
};

@@ -418,3 +384,3 @@ /**

* @param {number} state the boolean state to write to the coil (true / false).
* @param {function} next the function to call next.
* @param {Function} next the function to call next.
*/

@@ -444,7 +410,7 @@ ModbusRTU.prototype.writeFC5 = function (address, dataAddress, state, next) {

// add crc bytes to buffer
_CRC16(buf, codeLength);
buf.writeUInt16LE(crc16(buf.slice(0, -2)), codeLength);
// write buffer to serial port
this._port.write(buf);
}
};

@@ -455,4 +421,5 @@ /**

* @param {number} address the slave unit address.
* @param {value} number the value to write to a specific register.
* @param {function} next the function to call next.
* @param {number} dataAddress the Data Address of the register.
* @param {number} value the value to write to the register.
* @param {Function} next the function to call next.
*/

@@ -478,7 +445,7 @@ ModbusRTU.prototype.writeFC6 = function (address, dataAddress, value, next) {

// add crc bytes to buffer
_CRC16(buf, codeLength);
buf.writeUInt16LE(crc16(buf.slice(0, -2)), codeLength);
// write buffer to serial port
this._port.write(buf);
}
};

@@ -490,4 +457,4 @@ /**

* @param {number} dataAddress the Data Address of the first coil.
* @param {array} array the array of boolean states to write to coils.
* @param {function} next the function to call next.
* @param {Array} array the array of boolean states to write to coils.
* @param {Function} next the function to call next.
*/

@@ -527,7 +494,7 @@ ModbusRTU.prototype.writeFC15 = function (address, dataAddress, array, next) {

// add crc bytes to buffer
_CRC16(buf, codeLength);
buf.writeUInt16LE(crc16(buf.slice(0, -2)), codeLength);
// write buffer to serial port
this._port.write(buf);
}
};

@@ -539,4 +506,4 @@ /**

* @param {number} dataAddress the Data Address of the first register.
* @param {array} array the array of values to write to registers.
* @param {function} next the function to call next.
* @param {Array} array the array of values to write to registers.
* @param {Function} next the function to call next.
*/

@@ -566,7 +533,7 @@ ModbusRTU.prototype.writeFC16 = function (address, dataAddress, array, next) {

// add crc bytes to buffer
_CRC16(buf, codeLength);
buf.writeUInt16LE(crc16(buf.slice(0, -2)), codeLength);
// write buffer to serial port
this._port.write(buf);
}
};

@@ -573,0 +540,0 @@ // add the connection shorthand API

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

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

@@ -6,33 +6,8 @@ 'use strict';

/**
* calculate crc16
*
* @param {buffer} buf the buffer to to crc on.
* @return {number} the calculated crc16
*/
function crc16(buf) {
var length = buf.length - 2;
var crc = 0xFFFF;
var tmp;
var crc16 = require('./../utils/crc16');
// calculate crc16
for (var i = 0; i < length; i++) {
crc = crc ^ buf[i];
for (var j = 0; j < 8; j++) {
tmp = crc & 0x0001;
crc = crc >> 1;
if (tmp) {
crc = crc ^ 0xA001;
}
}
}
return crc;
}
/**
* calculate lrc
*
* @param {buffer} buf the buffer to to lrc on.
* @param {Buffer} buf the buffer to to lrc on.
* @return {number} the calculated lrc

@@ -55,4 +30,4 @@ */

*
* @param {buffer} buf the data buffer to encode.
* @return {buffer} the ascii encoded buffer
* @param {Buffer} buf the data buffer to encode.
* @return {Buffer} the ascii encoded buffer
*/

@@ -83,4 +58,4 @@ function asciiEncodeRequestBuffer(buf) {

*
* @param {buffer} bufAscii the ascii data buffer to decode.
* @return {buffer} the decoded buffer, or null if decode error
* @param {Buffer} bufAscii the ascii data buffer to decode.
* @return {Buffer} the decoded buffer, or null if decode error
*/

@@ -100,3 +75,3 @@ function asciiDecodeResponseBuffer(bufAscii) {

if( calculateLrc(bufDecoded) != lrcIn ) {
// retun null if lrc error
// return null if lrc error
return null;

@@ -115,3 +90,4 @@ }

*
* @param {buffer} buf the buffer to check.
* @param {AsciiPort} modbus
* @param {Buffer} buf the buffer to check.
* @return {boolean} if the buffer can be an answer

@@ -135,3 +111,3 @@ */

// options
if (typeof(options) == 'undefined') options = {};
options = options || {};

@@ -191,3 +167,3 @@ // internal buffer

events.call(this);
}
};
util.inherits(AsciiPort, events);

@@ -200,3 +176,3 @@

this._client.open(callback);
}
};

@@ -208,3 +184,4 @@ /**

this._client.close(callback);
}
};
/**

@@ -253,4 +230,4 @@ * Send data to a modbus slave via telnet server

this._client.write(_encodedData);
}
};
module.exports = AsciiPort;

@@ -6,36 +6,12 @@ 'use strict';

var crc16 = require('./../utils/crc16');
var C701_PORT = 0x7002;
/**
* calculate crc16
*
* @param {buffer} buf the buffer to to crc on.
* @return {number} the calculated crc16
*/
function crc16(buf) {
var length = buf.length - 2;
var crc = 0xFFFF;
var tmp;
// calculate crc16
for (var i = 0; i < length; i++) {
crc = crc ^ buf[i];
for (var j = 0; j < 8; j++) {
tmp = crc & 0x0001;
crc = crc >> 1;
if (tmp) {
crc = crc ^ 0xA001;
}
}
}
return crc;
}
/**
* check if a buffer chunk can be a modbus answer
* or modbus exception
*
* @param {buffer} buf the buffer to check.
* @param {UdpPort} modbus
* @param {Buffer} buf the buffer to check.
* @return {boolean} if the buffer can be an answer

@@ -98,3 +74,2 @@ */

modbus.emit('data', buffer);
return;
}

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

events.call(this);
}
};
util.inherits(UdpPort, events);

@@ -114,3 +89,3 @@

callback(null);
}
};

@@ -124,3 +99,3 @@ /**

callback(null);
}
};

@@ -179,4 +154,4 @@ /**

this._client.send(buffer, 0, buffer.length, this.port, this.ip);
}
};
module.exports = UdpPort;

@@ -6,29 +6,4 @@ 'use strict';

/**
* calculate crc16
*
* @param {buffer} buf the buffer to to crc on.
* @return {number} the calculated crc16
*/
function crc16(buf) {
var length = buf.length - 2;
var crc = 0xFFFF;
var tmp;
var crc16 = require('./../utils/crc16');
// calculate crc16
for (var i = 0; i < length; i++) {
crc = crc ^ buf[i];
for (var j = 0; j < 8; j++) {
tmp = crc & 0x0001;
crc = crc >> 1;
if (tmp) {
crc = crc ^ 0xA001;
}
}
}
return crc;
}
/**

@@ -38,3 +13,4 @@ * check if a buffer chunk can be a modbus answer

*
* @param {buffer} buf the buffer to check.
* @param {RTUBufferedPort} modbus
* @param {Buffer} buf the buffer to check.
* @return {boolean} if the buffer can be an answer

@@ -110,3 +86,3 @@ */

events.call(this);
}
};
util.inherits(RTUBufferedPort, events);

@@ -119,3 +95,3 @@

this._client.open(callback);
}
};

@@ -127,3 +103,4 @@ /**

this._client.close(callback);
}
};
/**

@@ -169,4 +146,4 @@ * Send data to a modbus slave via telnet server

this._client.write(data);
}
};
module.exports = RTUBufferedPort;

@@ -6,33 +6,7 @@ 'use strict';

var crc16 = require('./../utils/crc16');
var MODBUS_PORT = 502; // modbus port
/**
* calculate crc16
*
* @param {buffer} buf the buffer to to crc on.
*
* @return {number} the calculated crc16
*/
function crc16(buf) {
var length = buf.length - 2;
var crc = 0xFFFF;
var tmp;
// calculate crc16
for (var i = 0; i < length; i++) {
crc = crc ^ buf[i];
for (var j = 0; j < 8; j++) {
tmp = crc & 0x0001;
crc = crc >> 1;
if (tmp) {
crc = crc ^ 0xA001;
}
}
}
return crc;
}
/**
* Simulate a modbus-RTU port using modbus-TCP connection

@@ -68,3 +42,3 @@ */

events.call(this);
}
};
util.inherits(TcpPort, events);

@@ -77,3 +51,3 @@

this._client.connect(this.port, this.ip, callback);
}
};

@@ -87,3 +61,3 @@ /**

callback(null);
}
};

@@ -103,4 +77,4 @@ /**

this._client.write(buffer);
}
};
module.exports = TcpPort;

@@ -6,36 +6,12 @@ 'use strict';

var crc16 = require('./../utils/crc16');
var TELNET_PORT = 2217;
/**
* calculate crc16
*
* @param {buffer} buf the buffer to to crc on.
* @return {number} the calculated crc16
*/
function crc16(buf) {
var length = buf.length - 2;
var crc = 0xFFFF;
var tmp;
// calculate crc16
for (var i = 0; i < length; i++) {
crc = crc ^ buf[i];
for (var j = 0; j < 8; j++) {
tmp = crc & 0x0001;
crc = crc >> 1;
if (tmp) {
crc = crc ^ 0xA001;
}
}
}
return crc;
}
/**
* check if a buffer chunk can be a modbus answer
* or modbus exception
*
* @param {buffer} buf the buffer to check.
* @param {TelnetPort} modbus
* @param {Buffer} buf the buffer to check.
* @return {boolean} if the buffer can be an answer

@@ -113,3 +89,3 @@ */

events.call(this);
}
};
util.inherits(TelnetPort, events);

@@ -122,3 +98,3 @@

this._client.connect(this.port, this.ip, callback);
}
};

@@ -132,3 +108,3 @@ /**

callback(null);
}
};

@@ -175,4 +151,4 @@ /**

this._client.write(data);
}
};
module.exports = TelnetPort;

@@ -7,3 +7,4 @@ 'use strict';

*/
require('../apis/buffer_bit')();
require('../utils/buffer_bit')();
var crc16 = require('./../utils/crc16');

@@ -28,34 +29,6 @@ /**

events.call(this);
}
};
util.inherits(TestPort, events);
/**
* calculate crc16
*
* @param {buffer} buf the buffer to to crc on.
*
* @return {number} the calculated crc16
*/
function crc16(buf) {
var length = buf.length - 2;
var crc = 0xFFFF;
var tmp;
// calculate crc16
for (var i = 0; i < length; i++) {
crc = crc ^ buf[i];
for (var j = 0; j < 8; j++) {
tmp = crc & 0x0001;
crc = crc >> 1;
if (tmp) {
crc = crc ^ 0xA001;
}
}
}
return crc;
}
/**
* Simulate successful port open

@@ -66,3 +39,3 @@ */

callback(null);
}
};

@@ -75,3 +48,3 @@ /**

callback(null);
}
};

@@ -94,3 +67,3 @@ /**

// if crc is bad, ignore message
if (crc != crc16(buf)) {
if (crc != crc16(buf.slice(0, -2))) {
return;

@@ -267,3 +240,3 @@ }

// add crc
crc = crc16(buffer);
crc = crc16(buffer.slice(0, -2));
buffer.writeUInt16LE(crc, buffer.length - 2);

@@ -278,4 +251,4 @@

}
}
};
module.exports = TestPort;
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