Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: uncaught error in read device identification #557

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 54 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ function _readFC43(data, modbus, next) {

let startAt = 8;
const result = {};
for (let i = 0; i < numOfObjects; i++) {
// The modbus specification states that numOfObjects is the number of
// objects in the response, but the example on page 45 shows the total
// number over all responses. Therefore be careful about reading more data than available
for (let i = 0; i < numOfObjects && startAt < data.length; i++) {
const objectId = parseInt(data.readUInt8(startAt));
const objectLength = parseInt(data.readUInt8(startAt + 1));
const startOfData = startAt + 2;
Expand Down Expand Up @@ -496,51 +499,56 @@ function _onReceive(data) {

/* parse incoming data
*/

switch (code) {
case 1:
case 2:
// Read Coil Status (FC=01)
// Read Input Status (FC=02)
_readFC2(data, next);
break;
case 3:
case 4:
// Read Input Registers (FC=04)
// Read Holding Registers (FC=03)
if (modbus._enron && !(transaction.nextDataAddress >= modbus._enronTables.shortRange[0] && transaction.nextDataAddress <= modbus._enronTables.shortRange[1])) {
_readFC3or4Enron(data, next);
} else {
_readFC3or4(data, next);
}
break;
case 5:
// Force Single Coil
_readFC5(data, next);
break;
case 6:
// Preset Single Register
if (modbus._enron && !(transaction.nextDataAddress >= modbus._enronTables.shortRange[0] && transaction.nextDataAddress <= modbus._enronTables.shortRange[1])) {
_readFC6Enron(data, next);
} else {
_readFC6(data, next);
}
break;
case 15:
case 16:
// Force Multiple Coils
// Preset Multiple Registers
_readFC16(data, next);
break;
case 17:
_readFC17(data, next);
break;
case 20:
_readFC20(data, transaction.next);
break;
case 43:
// read device identification
_readFC43(data, modbus, next);
try {
switch (code) {
case 1:
case 2:
// Read Coil Status (FC=01)
// Read Input Status (FC=02)
_readFC2(data, next);
break;
case 3:
case 4:
// Read Input Registers (FC=04)
// Read Holding Registers (FC=03)
if (modbus._enron && !(transaction.nextDataAddress >= modbus._enronTables.shortRange[0] && transaction.nextDataAddress <= modbus._enronTables.shortRange[1])) {
_readFC3or4Enron(data, next);
} else {
_readFC3or4(data, next);
}
break;
case 5:
// Force Single Coil
_readFC5(data, next);
break;
case 6:
// Preset Single Register
if (modbus._enron && !(transaction.nextDataAddress >= modbus._enronTables.shortRange[0] && transaction.nextDataAddress <= modbus._enronTables.shortRange[1])) {
_readFC6Enron(data, next);
} else {
_readFC6(data, next);
}
break;
case 15:
case 16:
// Force Multiple Coils
// Preset Multiple Registers
_readFC16(data, next);
break;
case 17:
_readFC17(data, next);
break;
case 20:
_readFC20(data, transaction.next);
break;
case 43:
// read device identification
_readFC43(data, modbus, next);
}
} catch (e) {
if (transaction.next) {
next(e);
}
}
}

Expand Down
Loading