Initial commit
This commit is contained in:
114
lib/jsmodbus/handler/client/WriteMultipleRegisters.js
Normal file
114
lib/jsmodbus/handler/client/WriteMultipleRegisters.js
Normal file
@@ -0,0 +1,114 @@
|
||||
'use strict';
|
||||
|
||||
const Stampit = require('stampit');
|
||||
const Put = require('put');
|
||||
|
||||
|
||||
module.exports = Stampit()
|
||||
.init(function () {
|
||||
const FC = 16;
|
||||
|
||||
let init = () => this.addResponseHandler(FC, onResponse);
|
||||
|
||||
let onResponse = (unitId, pdu, cb) => {
|
||||
const fc = pdu.readUInt8(0);
|
||||
|
||||
if (fc !== FC) {
|
||||
cb(`WriteMultipleRegisters: Invalid FC ${fc}`);
|
||||
} else {
|
||||
const startAddress = pdu.readUInt16BE(1);
|
||||
const quantity = pdu.readUInt16BE(3);
|
||||
|
||||
let resp = {
|
||||
unitId: unitId,
|
||||
fc: fc,
|
||||
startAddress: startAddress,
|
||||
quantity: quantity
|
||||
};
|
||||
cb(null, resp);
|
||||
}
|
||||
};
|
||||
|
||||
this.writeMultipleRegisters = (unitId, startAddress, data) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let pdu = Put().word8(FC).word16be(startAddress);
|
||||
if (data instanceof Buffer) {
|
||||
if (data.length / 2 > 0x007b) {
|
||||
reject('Length is too big');
|
||||
return;
|
||||
}
|
||||
|
||||
pdu.word16be(register.length / 2).word8(data.length).put(data);
|
||||
} else if (data instanceof Array) {
|
||||
if (data.length > 0x007b) {
|
||||
reject('Length is too big');
|
||||
return;
|
||||
}
|
||||
|
||||
let byteCount = Math.ceil(data.length * 2);
|
||||
pdu.word16be(data.length).word8(byteCount);
|
||||
|
||||
for (let i = 0; i < data.length; i += 1) {
|
||||
pdu.word16be(data[i]);
|
||||
}
|
||||
} else {
|
||||
reject('Invalid data');
|
||||
return;
|
||||
}
|
||||
|
||||
this.queueRequest(unitId, FC, pdu.buffer(), (err, resp) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(resp);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
this.writeMultipleRegisters = function (startAddress, register) {
|
||||
var defer = Q.defer();
|
||||
var fc = 16,
|
||||
pdu = Put()
|
||||
.word8(fc)
|
||||
.word16be(startAddress);
|
||||
|
||||
if(register instanceof Buffer) {
|
||||
|
||||
if(register.length/2 > 0x007b) {
|
||||
defer.reject();
|
||||
}
|
||||
|
||||
pdu.word16be(register.length/2)
|
||||
.word8(register.length)
|
||||
.put(register)
|
||||
}
|
||||
else if(register instanceof Array) {
|
||||
|
||||
if (register.length > 0x007b) {
|
||||
defer.reject();
|
||||
return;
|
||||
}
|
||||
|
||||
var byteCount = Math.ceil(register.length * 2),
|
||||
curByte = 0;
|
||||
|
||||
pdu.word16be(register.length)
|
||||
.word8(byteCount);
|
||||
|
||||
for (var i = 0; i < register.length; i += 1) {
|
||||
pdu.word16be(register[i]);
|
||||
}
|
||||
} else {
|
||||
defer.reject();
|
||||
}
|
||||
|
||||
pdu = pdu.buffer();
|
||||
|
||||
this.queueRequest(fc, pdu, defer);
|
||||
|
||||
return defer.promise;
|
||||
};
|
||||
|
||||
init();
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user