modbus-serial
Advanced tools
Comparing version 1.2.0 to 1.2.1
31
index.js
@@ -61,3 +61,3 @@ /** | ||
* Parse the data for a Modbus - | ||
* Read Input Registers (FC=04) | ||
* Read Input Registers (FC=04,03) | ||
* | ||
@@ -77,3 +77,3 @@ * @param {buffer} data the data buffer to parse. | ||
if (next) | ||
next(null, {"data": contents}); | ||
next(null, {"data": contents, "buffer": data.slice(3, 3 + length)}); | ||
} | ||
@@ -192,4 +192,6 @@ | ||
// Read Input Registers (FC=04) | ||
if (code == 4) { | ||
/* Read Input Registers (FC=04) | ||
* Read Holding Registers (FC=03) | ||
*/ | ||
if (code == 4 || code == 3) { | ||
_readFC4(data, next); | ||
@@ -208,3 +210,3 @@ } | ||
/** | ||
* Write a Modbus Read Input Registers (FC=04) to serial port. | ||
* Write a Modbus "Read Holding Registers" (FC=03) to serial port. | ||
* | ||
@@ -216,4 +218,17 @@ * @param {number} address the slave unit address. | ||
*/ | ||
ModbusRTU.prototype.writeFC4 = function (address, dataAddress, length, next) { | ||
var code = 4; | ||
ModbusRTU.prototype.writeFC3 = function (address, dataAddress, length, next) { | ||
this.writeFC4(address, dataAddress, length, next, 3); | ||
} | ||
/** | ||
* Write a Modbus "Read Input Registers" (FC=04) to serial port. | ||
* | ||
* @param {number} address the slave unit address. | ||
* @param {number} dataAddress the Data Address of the first register. | ||
* @param {number} length the total number of registers requested. | ||
* @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; | ||
@@ -242,3 +257,3 @@ // set state variables | ||
/** | ||
* Write a Modbus Preset Multiple Registers (FC=16) to serial port. | ||
* Write a Modbus "Preset Multiple Registers" (FC=16) to serial port. | ||
* | ||
@@ -245,0 +260,0 @@ * @param {number} address the slave unit address. |
{ | ||
"name": "modbus-serial", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,3 +35,3 @@ # modbus-serial | ||
This class implements FC4 "Read Input Registers" and | ||
This class implements FC3 "Read Holding Registers", FC4 "Read Input Registers" and | ||
FC16 "Preset Multiple Registers" of modbus-RTU. | ||
@@ -112,3 +112,25 @@ | ||
``` | ||
---- | ||
###### Read raw buffer | ||
``` javascript | ||
var SerialPort = require("serialport").SerialPort; | ||
var serialPort = new SerialPort("/dev/ttyUSB0", {baudrate: 9600}); | ||
var ModbusRTU = require("modbus-serial"); | ||
var modbusRTU = new ModbusRTU(serialPort); | ||
modbusRTU.open(); | ||
// read 2 16bit-registers to get one 32 bit number | ||
setTimeout(function() { | ||
modbusRTU.writeFC4(1, 5, 2, function(err, data) { | ||
console.log(data.buffer.readUInt32BE()); | ||
}); | ||
}, 2000); | ||
// close communication | ||
setTimeout(function() { | ||
serialPort.close(); | ||
}, 3000); | ||
``` | ||
#### Methods | ||
@@ -123,2 +145,25 @@ ---- | ||
---- | ||
##### .writeFC3 (unit, address, length, callback) | ||
Writes "Read Holding Registers" (FC=03) request to serial port. | ||
###### unit | ||
The slave unit address. | ||
###### address | ||
The Data Address of the first register. | ||
###### length | ||
The total number of registers requested. | ||
###### callback (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
``` | ||
error - null on success, error string o/w | ||
data - an object with two fildes: | ||
data.data: array of unsinged 16 bit registers. | ||
data.buffer: raw baffer of bytes returned by slave. | ||
``` | ||
---- | ||
##### .writeFC4 (unit, address, length, callback) | ||
@@ -139,2 +184,8 @@ Writes "Read Input Registers" (FC=04) request to serial port. | ||
that looks like: function (error, data) { ... } | ||
``` | ||
error - null on success, error string o/w | ||
data - an object with two fildes: | ||
data.data: array of unsinged 16 bit registers. | ||
data.buffer: raw baffer of bytes returned by slave. | ||
``` | ||
@@ -141,0 +192,0 @@ ---- |
@@ -21,3 +21,23 @@ 'use strict'; | ||
describe('#writeFC4() - read registers.', function () { | ||
describe('#writeFC3() - read holding registers.', function () { | ||
it('should read 3 registers [0xa12b, 0xffff, 0xb21a] without errors', function (done) { | ||
modbusRTU.writeFC3(1, 8, 3, function(err, data) { | ||
expect(err).to.be.a('null'); | ||
expect(data).to.have.property('data').with.length(3); | ||
expect(data.data.toString()).to.equal([0xa12b, 0xffff, 0xb21a].toString()); | ||
done() | ||
}); | ||
}); | ||
it('should read raw buffer "a12bffffb21a" without errors', function (done) { | ||
modbusRTU.writeFC3(1, 8, 3, function(err, data) { | ||
expect(err).to.be.a('null'); | ||
expect(data).to.have.property('buffer'); | ||
expect(data.buffer.toString('hex')).to.equal("a12bffffb21a"); | ||
done() | ||
}); | ||
}); | ||
}); | ||
describe('#writeFC4() - read input registers.', function () { | ||
it('should read 3 registers [8, 9, 10] without errors', function (done) { | ||
@@ -91,3 +111,3 @@ modbusRTU.writeFC4(1, 8, 3, function(err, data) { | ||
describe('#writeFC4() - read registers after write.', function () { | ||
describe('#writeFC4() - read input registers after write.', function () { | ||
it('should read 3 registers [42, 128, 5] without errors', function (done) { | ||
@@ -94,0 +114,0 @@ modbusRTU.writeFC4(1, 8, 3, function(err, data) { |
@@ -14,3 +14,4 @@ 'use strict'; | ||
// simulate 14 registers | ||
this._registers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; | ||
this._registers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
this._holding_registers = [0,0,0,0,0,0,0,0, 0xa12b, 0xffff, 0xb21a ]; | ||
events.call(this); | ||
@@ -84,2 +85,22 @@ } | ||
// function code 3 | ||
if (functionCode == 3) { | ||
var address = buf.readUInt16BE(2); | ||
var length = buf.readUInt16BE(4); | ||
// if length is bad, ignore message | ||
if (buf.length != 8) { | ||
return; | ||
} | ||
// build answer | ||
buffer = new Buffer(3 + length * 2 + 2); | ||
buffer.writeUInt8(length * 2, 2); | ||
// read registers | ||
for (var i = 0; i < length; i++) { | ||
buffer.writeUInt16BE(this._holding_registers[address + i], 3 + i * 2); | ||
} | ||
} | ||
// function code 4 | ||
@@ -86,0 +107,0 @@ if (functionCode == 4) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29704
12
668
205