@@ -157,7 +157,7 @@ void Modbus::setUnitAddress(uint8_t unitAddress)
157
157
void Modbus::enable ()
158
158
{
159
159
_enabled = true ;
160
- }
160
+ }
161
161
162
162
/* *
163
163
* Disable communication.
@@ -169,7 +169,7 @@ void Modbus::disable()
169
169
170
170
/* *
171
171
* Gets the enable status.
172
- *
172
+ *
173
173
* @return The enable state.
174
174
*/
175
175
bool Modbus::readEnabled ()
@@ -313,7 +313,7 @@ uint8_t Modbus::readUnitAddress()
313
313
}
314
314
315
315
/* *
316
- * Returns a boolean value indicating if the request currently being processed
316
+ * Returns a boolean value indicating if the request currently being processed
317
317
* is a broadcast message and therefore does not need a response.
318
318
*
319
319
* @return True if the current request message is a broadcase message; otherwise false.
@@ -388,7 +388,7 @@ uint16_t Modbus::readRegisterFromBuffer(int offset)
388
388
/* *
389
389
* Writes the exception status to the buffer.
390
390
*
391
- * @param offset
391
+ * @param offset
392
392
* @param status Exception status flag (true / false)
393
393
*/
394
394
uint8_t Modbus::writeExceptionStatusToBuffer (int offset, bool status)
@@ -573,7 +573,8 @@ bool Modbus::readRequest()
573
573
}
574
574
575
575
// Check if there is enough room for the incoming bytes in the buffer.
576
- length = min (length, MODBUS_MAX_BUFFER - _requestBufferLength);
576
+ uint16_t m_length = MODBUS_MAX_BUFFER - _requestBufferLength;
577
+ length = min (length, m_length);
577
578
// Read the data from the serial stream into the buffer.
578
579
length = _serialStream.readBytes (_requestBuffer + _requestBufferLength, MODBUS_MAX_BUFFER - _requestBufferLength);
579
580
@@ -620,7 +621,7 @@ bool Modbus::readRequest()
620
621
*/
621
622
bool Modbus::relevantAddress (uint8_t unitAddress)
622
623
{
623
- // Every device should listen to broadcast messages,
624
+ // Every device should listen to broadcast messages,
624
625
// keep the check it local, since we provide the unitAddress
625
626
if (unitAddress == MODBUS_BROADCAST_ADDRESS)
626
627
{
@@ -646,7 +647,7 @@ bool Modbus::relevantAddress(uint8_t unitAddress)
646
647
*/
647
648
bool Modbus::validateRequest ()
648
649
{
649
-
650
+
650
651
// Check that the message was addressed to us
651
652
if (!Modbus::relevantAddress (_requestBuffer[MODBUS_ADDRESS_INDEX]))
652
653
{
@@ -655,7 +656,7 @@ bool Modbus::validateRequest()
655
656
// The minimum buffer size (1 x Address, 1 x Function, n x Data, 2 x CRC).
656
657
uint16_t expected_requestBufferSize = MODBUS_FRAME_SIZE;
657
658
bool report_illegal_function=false ;
658
-
659
+
659
660
// Check the validity of the data based on the function code.
660
661
switch (_requestBuffer[MODBUS_FUNCTION_CODE_INDEX])
661
662
{
@@ -714,17 +715,17 @@ bool Modbus::validateRequest()
714
715
{
715
716
return false ;
716
717
}
717
-
718
+
718
719
// report_illegal_function after the CRC check, cheaper
719
720
if (report_illegal_function)
720
721
{
721
722
Modbus::reportException (STATUS_ILLEGAL_FUNCTION);
722
723
return false ;
723
724
}
724
-
725
+
725
726
// Set the length to be read from the request to the calculated expected length.
726
727
_requestBufferLength = expected_requestBufferSize;
727
-
728
+
728
729
return true ;
729
730
}
730
731
0 commit comments