Skip to content

Commit

Permalink
WriteMultipleRegisters added callback and promises workflow (#478)
Browse files Browse the repository at this point in the history
* WriteMultipleRegisters added callback and promises workflow

* Update servertcp_handler.js

fixed missing semicolon

* improved workflow in _handleWriteMultipleRegisters

* fixed redefinning variable cb

* removed space after function
  • Loading branch information
HighWolf authored Oct 24, 2022
1 parent df17977 commit 23ea77b
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions servers/servertcp_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,27 +670,22 @@ function _handleWriteMultipleRegisters(requestBuffer, vector, unitID, callback)

// write registers
let callbackInvoked = false;
let cbCount = 0;
const buildCb = function(/* i - not used at the moment */) {
return function(err) {
if (err) {
if (!callbackInvoked) {
callbackInvoked = true;
callback(err);
}

return;
const cb = function(err) {
if (err) {
if (!callbackInvoked) {
callbackInvoked = true;
callback(err);
}

cbCount = cbCount + 1;
return;
}

if (cbCount === length && !callbackInvoked) {
modbusSerialDebug({ action: "FC16 response", responseBuffer: responseBuffer });
if (!callbackInvoked) {
modbusSerialDebug({ action: "FC16 response", responseBuffer: responseBuffer });

callbackInvoked = true;
callback(null, responseBuffer);
}
};
callbackInvoked = true;
callback(null, responseBuffer);
}
};

if (length === 0)
Expand All @@ -701,19 +696,17 @@ function _handleWriteMultipleRegisters(requestBuffer, vector, unitID, callback)
if (vector.setRegisterArray) {
value = [];

for (i = 0; i < length; i++) {
cb = buildCb(i);

value.push(requestBuffer.readUInt16BE(7 + i * 2));
_handlePromiseOrValue(value, cb);
}

try {
for (i = 0; i < length; i++) {
value.push(requestBuffer.readUInt16BE(7 + i * 2));
}

if (vector.setRegisterArray.length === 4) {
vector.setRegisterArray(address, value, unitID, cb);
}
else {
vector.setRegisterArray(address, value, unitID);
var promiseOrValue = vector.setRegisterArray(address, value, unitID);
_handlePromiseOrValue(promiseOrValue, cb);
}
}
catch (err) {
Expand All @@ -723,10 +716,9 @@ function _handleWriteMultipleRegisters(requestBuffer, vector, unitID, callback)
var value;

for (var i = 0; i < length; i++) {
var cb = buildCb(i);
value = requestBuffer.readUInt16BE(7 + i * 2);

try {
value = requestBuffer.readUInt16BE(7 + i * 2);

if (vector.setRegister.length === 4) {
vector.setRegister(address + i, value, unitID, cb);
}
Expand Down

0 comments on commit 23ea77b

Please sign in to comment.