@@ -736,8 +736,8 @@ bool modbusMaster::getModbusData(byte readCommand, int16_t startAddress,
736736 int16_t respSize = sendCommand (command, 8 );
737737 success = (respSize == returnFrameSize &&
738738 responseBuffer[2 ] == expectedReturnBytes);
739- // if we got a modbusErrorCode, stop trying
740- if (lastError != NO_ERROR && lastError != NO_RESPONSE ) {
739+ // if we got a valid modbusErrorCode, stop trying
740+ if (static_cast < int8_t >( lastError) > 0 && static_cast < int8_t >( lastError) < 0x0C ) {
741741 tries = commandRetries; // exit the loop
742742 } else if (!success && lastError == NO_ERROR) { // print error info
743743 debugPrint (F (" Failed to get requested data on try " ), tries + 1 , ' \n ' );
@@ -841,8 +841,8 @@ bool modbusMaster::setRegisters(int16_t startRegister, int16_t numRegisters,
841841 success = respSize == 8 && int16FromFrame (bigEndian, 2 ) == startRegister &&
842842 responseBuffer[4 ] == value[0 ] && responseBuffer[5 ] == value[1 ];
843843 }
844- // if we got a modbusErrorCode, stop trying
845- if (lastError != NO_ERROR && lastError != NO_RESPONSE ) {
844+ // if we got a valid modbusErrorCode, stop trying
845+ if (static_cast < int8_t >( lastError) > 0 && static_cast < int8_t >( lastError) < 0x0C ) {
846846 tries = commandRetries; // exit the loop
847847 } else if (!success && lastError == NO_ERROR) { // print error info
848848 debugPrint (F (" Failed to set register[s] on try " ), tries + 1 , ' \n ' );
@@ -913,8 +913,8 @@ bool modbusMaster::setCoil(int16_t coilAddress, bool value) {
913913 if (respSize == 8 && strncmp ((char *)responseBuffer, (char *)command, 8 ) == 0 ) {
914914 success = true ;
915915 }
916- // if we got a modbusErrorCode, stop trying
917- if (lastError != NO_ERROR && lastError != NO_RESPONSE ) {
916+ // if we got a valid modbusErrorCode, stop trying
917+ if (static_cast < int8_t >( lastError) > 0 && static_cast < int8_t >( lastError) < 0x0C ) {
918918 tries = commandRetries; // exit the loop
919919 } else if (!success && lastError == NO_ERROR) { // print error info
920920 debugPrint (F (" Failed to set a single coil on try " ), tries + 1 , ' \n ' );
@@ -985,8 +985,8 @@ bool modbusMaster::setCoils(int16_t startCoil, int16_t numCoils, byte value[]) {
985985 // (hi/lo)}
986986 success = (respSize == 8 && int16FromFrame (bigEndian, 2 ) == startCoil &&
987987 int16FromFrame (bigEndian, 4 ) == numCoils);
988- // if we got a modbusErrorCode, stop trying
989- if (lastError != NO_ERROR && lastError != NO_RESPONSE ) {
988+ // if we got a valid modbusErrorCode, stop trying
989+ if (static_cast < int8_t >( lastError) > 0 && static_cast < int8_t >( lastError) < 0x0C ) {
990990 tries = commandRetries; // exit the loop
991991 } else if (!success && lastError == NO_ERROR) { // print error info
992992 debugPrint (F (" Failed to set multiple coils on try " ), tries + 1 , ' \n ' );
0 commit comments