Skip to content

Commit b6cbcf9

Browse files
committed
Broadened use of improper response error
Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
1 parent 40753f4 commit b6cbcf9

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/SensorModbusMaster.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,8 @@ int16_t modbusMaster::getModbusData(byte slaveId, byte readCommand,
726726
F(" bytes of expected "), expectedReturnBytes, '\n');
727727

728728
delay(25);
729-
// If it's the last attempt, set the error code
730-
if (tries + 1 >= commandRetries) { lastError = WRONG_BYTE_COUNT; }
729+
// If it's the last attempt, set the error code for an improper response
730+
if (tries + 1 >= commandRetries) { lastError = IMPROPER_RESPONSE; }
731731
}
732732
tries++;
733733
}
@@ -854,6 +854,8 @@ bool modbusMaster::setRegisters(int16_t startRegister, int16_t numRegisters,
854854
value[1], F(" was expected"), '\n');
855855
}
856856
delay(25);
857+
// If it's the last attempt, set the error code for an improper response
858+
if (tries + 1 >= commandRetries) { lastError = IMPROPER_RESPONSE; }
857859
}
858860
tries++;
859861
}
@@ -924,6 +926,8 @@ bool modbusMaster::setCoil(int16_t coilAddress, bool value) {
924926
: F("does not "),
925927
F("match the command\n"));
926928
delay(25);
929+
// If it's the last attempt, set the error code for an improper response
930+
if (tries + 1 >= commandRetries) { lastError = IMPROPER_RESPONSE; }
927931
}
928932
tries++;
929933
}
@@ -1003,6 +1007,8 @@ bool modbusMaster::setCoils(int16_t startCoil, int16_t numCoils, byte* value) {
10031007
debugPrint(F("The slave said it set "), int16FromFrame(bigEndian, 4),
10041008
F(" of expected "), numCoils, F(" coils\n"));
10051009
delay(25);
1010+
// If it's the last attempt, set the error code for an improper response
1011+
if (tries + 1 >= commandRetries) { lastError = IMPROPER_RESPONSE; }
10061012
}
10071013
tries++;
10081014
}
@@ -1117,6 +1123,9 @@ void modbusMaster::printLastError(void) {
11171123
case GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND:
11181124
debugPrint("Gateway Target Device Failed to Respond!\n");
11191125
break;
1126+
case IMPROPER_RESPONSE:
1127+
debugPrint("Response is incorrect, malformed, or doesn't have the expected number of bytes!\n");
1128+
break;
11201129
case WRONG_SLAVE_ID:
11211130
debugPrint("Response is not from the correct modbus slave!\n");
11221131
break;

src/SensorModbusMaster.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ typedef enum modbusErrorCode {
102102
GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND =
103103
0x0B, ///< The gateway target device failed to respond
104104
// additional codes in this library
105-
WRONG_BYTE_COUNT = 0xC, ///< The response doesn't have the expected number of bytes
105+
IMPROPER_RESPONSE =
106+
0xC, ///< The response is incorrect, malformed, or doesn't have the
107+
///< expected number of bytes
106108
WRONG_SLAVE_ID = 0xD, ///< The response is not from the correct modbus slave
107109
BAD_CRC = 0xE, ///< The CRC check on the response failed
108110
NO_RESPONSE = 0xF, ///< No response was received at all

0 commit comments

Comments
 (0)