@@ -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 ;
0 commit comments