Socket
Socket
Sign inDemoInstall

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 7.7.4 to 7.8.0-no-serial-port

examples/polling_UDP.js

27

apis/connection.js

@@ -203,2 +203,29 @@ "use strict";

/**
* Connect to a communication port, using modbus-udp.
*
* @param {string} ip the ip of the UDP Port - required.
* @param {Object} options - the serial port options - optional.
* @param {Function} next the function to call next.
*/
cl.connectUDP = function(ip, options, next) {
// check if we have options
if (typeof next === "undefined" && typeof options === "function") {
next = options;
options = {};
}
// check if we have options
if (typeof options === "undefined") {
options = {};
}
// create the UdpPort
var UdpPort = require("../ports/udpport");
this._port = new UdpPort(ip, options);
// open and call next
return open(this, next);
};
/**
* Connect to a communication port, using Bufferd Serial port.

@@ -205,0 +232,0 @@ *

1

apis/promise.js

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

cl.writeRegisters = _convert(cl.writeFC16);
cl.readDeviceIdentification = _convert(cl.writeFC43);
};

@@ -86,0 +87,0 @@

@@ -161,3 +161,43 @@ "use strict";

/**
* Parse the data for a Modbus -
* Read Device Identification (FC=43)
*
* @param {Buffer} data the data buffer to parse.
* @param {Modbus} modbus the client in case we need to read more device information
* @param {Function} next the function to call next.
*/
function _readFC43(data, modbus, next) {
var address = parseInt(data.readUInt8(0));
var readDeviceIdCode = parseInt(data.readUInt8(3));
var conformityLevel = parseInt(data.readUInt8(4));
var moreFollows = parseInt(data.readUInt8(5));
var nextObjectId = parseInt(data.readUInt8(6));
var numOfObjects = parseInt(data.readUInt8(7));
var startAt = 8;
var result = {};
for (var i = 0; i < numOfObjects; i++) {
var objectId = parseInt(data.readUInt8(startAt));
var objectLength = parseInt(data.readUInt8(startAt + 1));
const startOfData = startAt + 2;
result[objectId] = data.toString("ascii", startOfData, startOfData + objectLength);
startAt = startOfData + objectLength;
}
// is it saying to follow and did you previously get data
// if you did not previously get data go ahead and halt to prevent an infinite loop
if (moreFollows && numOfObjects) {
const cb = function(err, data) {
data.data = Object.assign(data.data, result);
return next(err, data);
};
modbus.writeFC43(address, readDeviceIdCode, nextObjectId, cb);
} else if (next) {
next(null, { data: result, conformityLevel });
}
}
/**
* Wrapper method for writing to a port with timeout. <code><b>[this]</b></code> has the context of ModbusRTU

@@ -270,3 +310,3 @@ * @param {Buffer} buffer The data to send

*/
if (data.length < 5) {
if (!transaction.lengthUnknown && data.length < 5) {
error = "Data length error, expected " +

@@ -311,3 +351,3 @@ transaction.nextLength + " got " + data.length;

*/
if (data.length !== transaction.nextLength) {
if (!transaction.lengthUnknown && data.length !== transaction.nextLength) {
error = "Data length error, expected " +

@@ -362,2 +402,5 @@ transaction.nextLength + " got " + data.length;

break;
case 43:
// read device identification
_readFC43(data, modbus, transaction.next);
}

@@ -767,2 +810,39 @@ });

/**
* Write a Modbus "Read Device Identification" (FC=43) to serial port.
*
* @param {number} address the slave unit address.
* @param {number} deviceIdCode the read device access code.
* @param {number} objectId the array of values to write to registers.
* @param {Function} next the function to call next.
*/
ModbusRTU.prototype.writeFC43 = function(address, deviceIdCode, objectId, next) {
// check port is actually open before attempting write
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
var code = 0x2B; // 43
// set state variables
this._transactions[this._port._transactionIdWrite] = {
nextAddress: address,
nextCode: code,
lengthUnknown: true,
next: next
};
var codeLength = 5;
var buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes
buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
buf.writeUInt8(0x0E, 2); // 16 MEI Type
buf.writeUInt8(deviceIdCode, 3);
buf.writeUInt8(objectId, 4);
// add crc bytes to buffer
buf.writeUInt16LE(crc16(buf.slice(0, -2)), codeLength);
// write buffer to serial port
_writeBufferToPort.call(this, buf, this._port._transactionIdWrite);
};
// add the connection shorthand API

@@ -769,0 +849,0 @@ require("./apis/connection")(ModbusRTU);

@@ -22,2 +22,4 @@ export class ModbusRTU {

connectTCP(ip: string, options: TcpPortOptions): Promise<void>;
connectUDP(ip: string, options: UdpPortOptions, next: Function): void;
connectUDP(ip: string, options: UdpPortOptions): Promise<void>;
connectTcpRTUBuffered(ip: string, options: TcpRTUPortOptions, next: Function): void;

@@ -107,2 +109,8 @@ connectTcpRTUBuffered(ip: string, options: TcpRTUPortOptions): Promise<void>;

export interface UdpPortOptions {
port?: number;
localAddress?: string;
family?: number;
}
export interface TcpRTUPortOptions {

@@ -109,0 +117,0 @@ port?: number;

5

package.json
{
"name": "modbus-serial",
"version": "7.7.4",
"version": "7.8.0-no-serial-port",
"description": "A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.",

@@ -40,5 +40,4 @@ "main": "index.js",

"dependencies": {
"debug": "^4.1.1",
"serialport": "^8.0.6"
"debug": "^4.1.1"
}
}

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

var MIN_DATA_LENGTH = 8;
var MIN_DATA_LENGTH = 7;

@@ -92,3 +92,2 @@ /**

var crc = data[data.length - 2] + data[data.length - 1] * 0x100;
// if crc is bad, ignore message

@@ -245,2 +244,16 @@ if (crc !== crc16(data.slice(0, -2))) {

if (functionCode === 43) {
const productCode = "MyProductCode1234";
buffer = Buffer.alloc(12 + productCode.length);
buffer.writeUInt8(16, 2); // MEI Type
buffer.writeUInt8(data.readInt8(3), 3); // read device ID code
buffer.writeUInt8(0x01, 4); // conformity level
buffer.writeUInt8(0, 5); // number of follows left
buffer.writeUInt8(0, 6); // next object ID
buffer.writeUInt8(1, 7); // number of objects
buffer.writeUInt8(data.readInt8(4), 8);
buffer.writeUInt8(productCode.length, 9);
buffer.write(productCode, 10, productCode.length, "ascii");
}
// send data back

@@ -247,0 +260,0 @@ if (buffer) {

@@ -68,3 +68,3 @@ # modbus-serial

* FC16 "Preset Multiple Registers"
* FC43/14 "Read Device Identification" (server only)
* FC43/14 "Read Device Identification"

@@ -210,2 +210,21 @@ ###### Client Serial:

----
###### Logger UDP
``` javascript
// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
// open connection to a udp line
client.connectUDP("127.0.0.1", { port: 8502 });
client.setID(1);
// read the values of 10 registers starting at address 0
// on device number 1. and log the values to the console.
setInterval(function() {
client.readHoldingRegisters(0, 10, function(err, data) {
console.log(data.data);
});
}, 1000);
```
----
###### ModbusTCP Server

@@ -212,0 +231,0 @@ ``` javascript

@@ -18,2 +18,4 @@ "use strict";

var value = 1;
var deviceIdCode = 1;
var objectId = 2;

@@ -28,2 +30,3 @@ modbusRTU.open();

expect(modbusRTU.writeRegisters(address, [value])).to.be.instanceOf(Promise);
expect(modbusRTU.readDeviceIdentification(deviceIdCode, objectId)).to.be.instanceOf(Promise);
});

@@ -30,0 +33,0 @@ });

@@ -36,2 +36,7 @@ "use strict";

Socket.prototype.bind = function() {
this.emit("listening");
};
// Obsolete? It doesn't reflect the dgram interface
Socket.prototype.listen = function() {

@@ -42,3 +47,3 @@ this.emit("listening");

Socket.prototype.receive = function(buffer) {
this.emit("message", buffer);
this.emit("message", buffer, { address: this._address, port: this._port, size: this._data.length });
};

@@ -45,0 +50,0 @@

@@ -338,2 +338,13 @@ "use strict";

describe("#writeFC43() - read device identification", function() {
it("should return a device identificationm without errors", function(done) {
modbusRTU.writeFC43(1, 4, 1, function(err, data) {
expect(err).to.be.a("null");
expect(data.conformityLevel).to.equal(1);
expect(data.data["1"]).to.equal("MyProductCode1234");
done();
});
});
});
describe("Timeout", function() {

@@ -340,0 +351,0 @@ var timeout = 1000;

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