Skip to content

Commit

Permalink
move init response buffer to validateRequest() function (#93)
Browse files Browse the repository at this point in the history
Init response Buffer only if we have a possible Request
  • Loading branch information
device111 authored Apr 16, 2022
1 parent f051380 commit 052ef5f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/ModbusSlave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,6 @@ uint8_t Modbus::poll()
return 0;
}

// Prepare the output buffer.
memset(_responseBuffer, 0, MODBUS_MAX_BUFFER);
_responseBuffer[MODBUS_ADDRESS_INDEX] = _requestBuffer[MODBUS_ADDRESS_INDEX];
_responseBuffer[MODBUS_FUNCTION_CODE_INDEX] = _requestBuffer[MODBUS_FUNCTION_CODE_INDEX];
_responseBufferLength = MODBUS_FRAME_SIZE;

// If communication is not enabled, skip processing
if (!_enabled)
{
Expand Down Expand Up @@ -716,6 +710,12 @@ bool Modbus::validateRequest()
return false;
}

// Prepare the output buffer.
memset(_responseBuffer, 0, MODBUS_MAX_BUFFER);
_responseBuffer[MODBUS_ADDRESS_INDEX] = _requestBuffer[MODBUS_ADDRESS_INDEX];
_responseBuffer[MODBUS_FUNCTION_CODE_INDEX] = _requestBuffer[MODBUS_FUNCTION_CODE_INDEX];
_responseBufferLength = MODBUS_FRAME_SIZE;

// report_illegal_function after the CRC check, cheaper
if (report_illegal_function)
{
Expand Down Expand Up @@ -1036,7 +1036,7 @@ uint16_t Modbus::reportException(uint8_t exceptionCode)
*
* @return The calculated CRC as an unsigned 16 bit integer.
*/

#ifndef CRC_LTABLE_CALC

uint16_t Modbus::calculateCRC(uint8_t *buffer, int length)
Expand Down

0 comments on commit 052ef5f

Please sign in to comment.