Skip to content

Commit

Permalink
Revert changes to 1.2.5 and retagged this as 1.2.6 so that no errors …
Browse files Browse the repository at this point in the history
…occure in 1.2.6.
  • Loading branch information
stefanpoeter committed Nov 26, 2016
1 parent 6f97bc4 commit d2c6bae
Show file tree
Hide file tree
Showing 26 changed files with 430 additions and 421 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
*.swp
*.idea
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsmodbus",
"version": "1.2.6",
"version": "1.2.5",
"description": "Implementation for the Serial/TCP Modbus protocol.",
"author": "Stefan Poeter <[email protected]>",
"main": "./src/modbus.js",
Expand All @@ -9,7 +9,8 @@
"url": "https://github.com/Cloud-Automation/node-modbus"
},
"dependencies": {
"bluebird": "^3.4.6",
"put": "0.0.6",
"q": "1.0.1",
"crc": "3.4.0",
"serialport": "^4.0.1",
"stampit": "^2.1.2",
Expand Down
17 changes: 8 additions & 9 deletions src/handler/client/ReadCoils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var Stampit = require('stampit'),
Promise = require('bluebird')
Q = require('q'),
Put = require('put');


module.exports = Stampit()
.init(function () {
Expand All @@ -12,10 +14,11 @@ module.exports = Stampit()

var onResponse = function (pdu, request) {

this.log.debug("handling read coils response.");
this.log.debug("handeling read coils response.");

var fc = pdu.readUInt8(0),
byteCount = pdu.readUInt8(1);
byteCount = pdu.readUInt8(1),
bitCount = byteCount * 8;

var resp = {
fc : fc,
Expand Down Expand Up @@ -47,12 +50,8 @@ module.exports = Stampit()
this.readCoils = function (start, quantity) {

var fc = 1,
defer = Promise.defer(),
pdu = Buffer.allocUnsafe(5)

pdu.writeUInt8(fc,0)
pdu.writeUInt16BE(start,1)
pdu.writeUInt16BE(quantity,3)
defer = Q.defer(),
pdu = Put().word8(fc).word16be(start).word16be(quantity).buffer();

this.queueRequest(fc, pdu, defer);

Expand Down
21 changes: 11 additions & 10 deletions src/handler/client/ReadDiscreteInputs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var Stampit = require('stampit'),
Promise = require('bluebird')
Q = require('q'),
Put = require('put');


module.exports = Stampit()
.init(function () {
Expand All @@ -12,7 +14,7 @@ module.exports = Stampit()

var onResponse = function (pdu, request) {

this.log.debug("handling read discrete inputs response.");
this.log.debug("handeling read discrete inputs response.");

var fc = pdu.readUInt8(0),
byteCount = pdu.readUInt8(1),
Expand All @@ -29,9 +31,9 @@ module.exports = Stampit()
return;
}

for (var i = 0; i < byteCount; i += 1) {
for (var i = 0; i < byteCount; i+=1) {
var h = 1, cur = pdu.readUInt8(2 + i);
for (var j = 0; j < 8; j += 1) {
for (var j = 0; j < 8; j+=1) {
resp.coils[cntr] = (cur & h) > 0 ;
h = h << 1;
cntr += 1;
Expand All @@ -45,17 +47,16 @@ module.exports = Stampit()
this.readDiscreteInputs = function (start, quantity) {

var fc = 2,
defer = Promise.defer(),
pdu = Buffer.allocUnsafe(5);

pdu.writeUInt8(fc);
pdu.writeUInt16BE(start, 1);
pdu.writeUInt16BE(quantity, 3);
defer = Q.defer(),
pdu = Put().word8be(2).word16be(start).word16be(quantity).buffer();

if (quantity > 2000) {

defer.reject();

return defer.promise;


}

this.queueRequest(fc, pdu, defer);
Expand Down
12 changes: 5 additions & 7 deletions src/handler/client/ReadHoldingRegisters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var Stampit = require('stampit'),
Promise = require('bluebird')
Q = require('q'),
Put = require('put');


module.exports = Stampit()
.init(function () {
Expand Down Expand Up @@ -45,12 +47,8 @@ module.exports = Stampit()
this.log.debug('Starting read holding registers request.');

var fc = 3,
defer = Promise.defer(),
pdu = Buffer.allocUnsafe(5)

pdu.writeUInt8(fc)
pdu.writeUInt16BE(start,1)
pdu.writeUInt16BE(quantity,3)
defer = Q.defer(),
pdu = Put().word8be(3).word16be(start).word16be(quantity).buffer();

this.queueRequest(fc, pdu, defer);

Expand Down
12 changes: 5 additions & 7 deletions src/handler/client/ReadInputRegisters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var Stampit = require('stampit'),
Promise = require('bluebird')
Q = require('q'),
Put = require('put');


module.exports = Stampit()
.init(function () {
Expand Down Expand Up @@ -43,12 +45,8 @@ module.exports = Stampit()
this.readInputRegisters = function (start, quantity) {

var fc = 4,
defer = Promise.defer(),
pdu = Buffer.allocUnsafe(5)

pdu.writeUInt8(fc)
pdu.writeUInt16BE(start,1)
pdu.writeUInt16BE(quantity,3)
defer = Q.defer(),
pdu = Put().word8be(4).word16be(start).word16be(quantity).buffer();

this.queueRequest(fc, pdu, defer);

Expand Down
38 changes: 17 additions & 21 deletions src/handler/client/WriteMultipleCoils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var stampit = require('stampit'),
Promise = require('bluebird')
Q = require('q'),
Put = require('put');


module.exports = stampit()
.init(function () {
Expand Down Expand Up @@ -36,20 +38,17 @@ module.exports = stampit()

this.writeMultipleCoils = function (startAddress, coils, N) {

var defer = Promise.defer();
var defer = Q.defer();
var fc = 15,
basePdu = Buffer.allocUnsafe(6)
var pdu

basePdu.writeUInt8(fc,0)
basePdu.writeUInt16BE(startAddress,1)
pdu = Put()
.word8(fc)
.word16be(startAddress);

if (coils instanceof Buffer) {

basePdu.writeUInt16BE(N, 3)
basePdu.writeUInt8(coils.length, 5)
pdu = Buffer.concat([basePdu, coils])

pdu.word16be(N)
.word8(coils.length)
.put(coils);
} else if (coils instanceof Array) {

if (coils.length > 1968) {
Expand All @@ -59,12 +58,10 @@ module.exports = stampit()

var byteCount = Math.ceil(coils.length / 8),
curByte = 0,
curByteIdx = 0,
cntr = 0
var payloadPdu = Buffer.allocUnsafe(byteCount)
cntr = 0;

basePdu.writeUInt16BE(coils.length, 3)
basePdu.writeUInt8(byteCount, 5)
pdu.word16be(coils.length)
.word8(byteCount);

for (var i = 0; i < coils.length; i += 1) {

Expand All @@ -73,15 +70,14 @@ module.exports = stampit()
cntr = (cntr + 1) % 8;

if (cntr === 0 || i === coils.length - 1 ) {
payloadPdu.writeUInt8(curByte, curByteIdx)
curByteIdx = curByteIdx + 1
curByte = 0
pdu.word8(curByte);
curByte = 0;
}
}

pdu = Buffer.concat([basePdu, payloadPdu])
}

pdu = pdu.buffer();

this.queueRequest(fc, pdu, defer);

return defer.promise;
Expand Down
33 changes: 15 additions & 18 deletions src/handler/client/WriteMultipleRegisters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var stampit = require('stampit'),
Promise = require('bluebird')
Q = require('q'),
Put = require('put');


module.exports = stampit()
.init(function () {
Expand Down Expand Up @@ -36,24 +38,21 @@ module.exports = stampit()

this.writeMultipleRegisters = function (startAddress, register) {

var defer = Promise.defer();
var defer = Q.defer();
var fc = 16,
basePdu = Buffer.allocUnsafe(6),
pdu

basePdu.writeUInt8(fc)
basePdu.writeUInt16BE(startAddress, 1)
pdu = Put()
.word8(fc)
.word16be(startAddress)

if(register instanceof Buffer) {

if(register.length/2 > 0x007b) {
defer.reject();
}

basePdu.writeUInt16BE(register.length/2,3)
basePdu.writeUInt8(register.length,5)

pdu = Buffer.concat([basePdu, register])
pdu.word16be(register.length/2)
.word8(register.length)
.put(register)
}
else if(register instanceof Array) {

Expand All @@ -65,20 +64,18 @@ module.exports = stampit()
var byteCount = Math.ceil(register.length * 2),
curByte = 0

var payloadPdu = Buffer.allocUnsafe(byteCount)

basePdu.writeUInt16BE(register.length, 3)
basePdu.writeUInt8(byteCount, 5)
pdu.word16be(register.length)
.word8(byteCount)

for (var i = 0; i < register.length; i += 1) {
payloadPdu.writeUInt16BE(register[i],2*i)
pdu.word16be(register[i]);
}

pdu = Buffer.concat([basePdu, payloadPdu])
} else {
defer.reject();
}

pdu = pdu.buffer();

this.queueRequest(fc, pdu, defer);

return defer.promise;
Expand Down
11 changes: 4 additions & 7 deletions src/handler/client/WriteSingleCoil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Stampit = require('stampit'),
Promise = require('bluebird')
Q = require('q'),
Put = require('put');

module.exports = Stampit()
.init(function () {
Expand Down Expand Up @@ -36,13 +37,9 @@ module.exports = Stampit()
this.writeSingleCoil = function (address, value) {

var fc = 5,
defer = Promise.defer(),
defer = Q.defer(),
payload = (value instanceof Buffer) ? (value.readUInt8(0) > 0) : value,
pdu = Buffer.allocUnsafe(5)

pdu.writeUInt8(fc,0)
pdu.writeUInt16BE(address,1)
pdu.writeUInt16BE(payload ? 0xff00:0x0000,3)
pdu = Put().word8be(5).word16be(address).word16be(payload?0xff00:0x0000).buffer();

this.queueRequest(fc, pdu, defer);

Expand Down
18 changes: 8 additions & 10 deletions src/handler/client/WriteSingleRegister.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var Stampit = require('stampit'),
Promise = require('bluebird')
Q = require('q'),
Put = require('put');


module.exports = Stampit()
.init(function () {
Expand All @@ -22,8 +24,8 @@ module.exports = Stampit()
fc : fc,
registerAddress : registerAddress,
registerValue : registerValue,
registerAddressRaw: pdu.slice(1,3),
registerValueRaw: pdu.slice(3,5)
registerAddressRaw: pdu.slice(1,2),
registerValueRaw: pdu.slice(3,2)
};

if (fc !== 6) {
Expand All @@ -38,13 +40,9 @@ module.exports = Stampit()
this.writeSingleRegister = function (address, value) {

var fc = 6,
defer = Promise.defer(),
payload = (value instanceof Buffer) ? value.readUInt16BE(0) : value,
pdu = Buffer.allocUnsafe(5)

pdu.writeUInt8(fc, 0)
pdu.writeUInt16BE(address, 1)
pdu.writeUInt16BE(payload, 3)
defer = Q.defer(),
payload = (value instanceof Buffer) ? value : Put().word16be(value).buffer(),
pdu = Put().word8be(6).word16be(address).put(payload).buffer();

this.queueRequest(fc, pdu, defer);

Expand Down
Loading

0 comments on commit d2c6bae

Please sign in to comment.