Skip to content

Commit

Permalink
Improve server callbacks (#416)
Browse files Browse the repository at this point in the history
* getMultipleHoldingRegisters - do not require value array if returning error

* test for setRegisterArray handler before setRegister handler when responding to _handleWriteMultipleRegisters; 4 arguments (not 6) for setRegisterArray with callback function

* test for setCoilArray before setCoil when responding to _handleForceMultipleCoils

* missed }

* fix linting issues

Co-authored-by: bruce-c-eng <[email protected]>
  • Loading branch information
bruceceng and bruce-c-eng authored Jun 27, 2021
1 parent 5db6624 commit ea7ea76
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"keyword-spacing": "off",
"line-comment-position": "off",
"linebreak-style": [
"error",
"off",
"unix"
],
"lines-around-comment": "off",
Expand Down
101 changes: 54 additions & 47 deletions servers/servertcp_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,18 @@ function _handleReadMultipleRegisters(requestBuffer, vector, unitID, callback) {
if (!err && values.length !== length) {
var error = new Error("Requested address length and response length do not match");
callback(error);
} else {
} else if (err) {
const cb = buildCb(i);
try {
cb(err); // no need to use value array if there is an error
}
catch (ex) {
cb(ex);
}
}
else {
for (var i = 0; i < length; i++) {
var cb = buildCb(i);
const cb = buildCb(i);
try {
cb(err, values[i]);
}
Expand Down Expand Up @@ -592,28 +601,8 @@ function _handleForceMultipleCoils(requestBuffer, vector, unitID, callback) {
msg: "Invalid length"
});

if (vector.setCoil) {
var state;

for (var i = 0; i < length; i++) {
var cb = buildCb(i);
state = requestBuffer.readBit(i, 7);

try {
if (vector.setCoil.length === 4) {
vector.setCoil(address + i, state !== false, unitID, cb);
}
else {
var promiseOrValue = vector.setCoil(address + i, state !== false, unitID);
_handlePromiseOrValue(promiseOrValue, cb);
}
}
catch(err) {
cb(err);
}
}
} else if (vector.setCoilArray) {
state = [];
if (vector.setCoilArray) {
var state = [];

for (i = 0; i < length; i++) {
cb = buildCb(i);
Expand All @@ -632,9 +621,28 @@ function _handleForceMultipleCoils(requestBuffer, vector, unitID, callback) {
catch(err) {
cb(err);
}
} else if (vector.setCoil) {
let state;

for (var i = 0; i < length; i++) {
var cb = buildCb(i);
state = requestBuffer.readBit(i, 7);

try {
if (vector.setCoil.length === 4) {
vector.setCoil(address + i, state !== false, unitID, cb);
}
else {
var promiseOrValue = vector.setCoil(address + i, state !== false, unitID);
_handlePromiseOrValue(promiseOrValue, cb);
}
}
catch(err) {
cb(err);
}
}
}
}

/**
* Function to handle FC16 request.
*
Expand Down Expand Up @@ -689,8 +697,28 @@ function _handleWriteMultipleRegisters(requestBuffer, vector, unitID, callback)
modbusErrorCode: 0x02, // Illegal address
msg: "Invalid length"
});
if (vector.setRegisterArray) {
value = [];

if (vector.setRegister) {
for (i = 0; i < length; i++) {
cb = buildCb(i);

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

try {
if (vector.setRegisterArray.length === 4) {
vector.setRegisterArray(address, value, unitID, cb);
}
else {
vector.setRegisterArray(address, value, unitID);
}
}
catch (err) {
cb(err);
}
} else if (vector.setRegister) {
var value;

for (var i = 0; i < length; i++) {
Expand All @@ -710,27 +738,6 @@ function _handleWriteMultipleRegisters(requestBuffer, vector, unitID, callback)
cb(err);
}
}
} else if (vector.setRegisterArray) {
value = [];

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

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

try {
if (vector.setRegisterArray.length === 6) {
vector.setRegisterArray(address, value, unitID, cb);
}
else {
vector.setRegisterArray(address, value, unitID);
}
}
catch (err) {
cb(err);
}
}
}

Expand Down

0 comments on commit ea7ea76

Please sign in to comment.