Skip to content

Commit f881a8b

Browse files
authored
fix ESP32 compiler errors (#84)
- add SERIAL_BUFFER_SIZE define, which is not available in ESP32 - fix typo in "min" macro call. (ESP32 only) - remove blank spaces.
1 parent 69bd759 commit f881a8b

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/ModbusSlave.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void Modbus::setUnitAddress(uint8_t unitAddress)
157157
void Modbus::enable()
158158
{
159159
_enabled = true;
160-
}
160+
}
161161

162162
/**
163163
* Disable communication.
@@ -169,7 +169,7 @@ void Modbus::disable()
169169

170170
/**
171171
* Gets the enable status.
172-
*
172+
*
173173
* @return The enable state.
174174
*/
175175
bool Modbus::readEnabled()
@@ -313,7 +313,7 @@ uint8_t Modbus::readUnitAddress()
313313
}
314314

315315
/**
316-
* Returns a boolean value indicating if the request currently being processed
316+
* Returns a boolean value indicating if the request currently being processed
317317
* is a broadcast message and therefore does not need a response.
318318
*
319319
* @return True if the current request message is a broadcase message; otherwise false.
@@ -388,7 +388,7 @@ uint16_t Modbus::readRegisterFromBuffer(int offset)
388388
/**
389389
* Writes the exception status to the buffer.
390390
*
391-
* @param offset
391+
* @param offset
392392
* @param status Exception status flag (true / false)
393393
*/
394394
uint8_t Modbus::writeExceptionStatusToBuffer(int offset, bool status)
@@ -573,7 +573,8 @@ bool Modbus::readRequest()
573573
}
574574

575575
// 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);
577578
// Read the data from the serial stream into the buffer.
578579
length = _serialStream.readBytes(_requestBuffer + _requestBufferLength, MODBUS_MAX_BUFFER - _requestBufferLength);
579580

@@ -620,7 +621,7 @@ bool Modbus::readRequest()
620621
*/
621622
bool Modbus::relevantAddress(uint8_t unitAddress)
622623
{
623-
// Every device should listen to broadcast messages,
624+
// Every device should listen to broadcast messages,
624625
// keep the check it local, since we provide the unitAddress
625626
if (unitAddress == MODBUS_BROADCAST_ADDRESS)
626627
{
@@ -646,7 +647,7 @@ bool Modbus::relevantAddress(uint8_t unitAddress)
646647
*/
647648
bool Modbus::validateRequest()
648649
{
649-
650+
650651
// Check that the message was addressed to us
651652
if (!Modbus::relevantAddress(_requestBuffer[MODBUS_ADDRESS_INDEX]))
652653
{
@@ -655,7 +656,7 @@ bool Modbus::validateRequest()
655656
// The minimum buffer size (1 x Address, 1 x Function, n x Data, 2 x CRC).
656657
uint16_t expected_requestBufferSize = MODBUS_FRAME_SIZE;
657658
bool report_illegal_function=false;
658-
659+
659660
// Check the validity of the data based on the function code.
660661
switch (_requestBuffer[MODBUS_FUNCTION_CODE_INDEX])
661662
{
@@ -714,17 +715,17 @@ bool Modbus::validateRequest()
714715
{
715716
return false;
716717
}
717-
718+
718719
// report_illegal_function after the CRC check, cheaper
719720
if (report_illegal_function)
720721
{
721722
Modbus::reportException(STATUS_ILLEGAL_FUNCTION);
722723
return false;
723724
}
724-
725+
725726
// Set the length to be read from the request to the calculated expected length.
726727
_requestBufferLength = expected_requestBufferSize;
727-
728+
728729
return true;
729730
}
730731

src/ModbusSlave.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#define MODBUS_DEFAULT_UNIT_ADDRESS 1
2525
#define MODBUS_CONTROL_PIN_NONE -1
2626

27+
#if defined (ESP32) || defined (ESP8266)
28+
#define SERIAL_BUFFER_SIZE 128
29+
#endif
30+
2731
/**
2832
* Modbus function codes
2933
*/
@@ -141,7 +145,7 @@ class Modbus
141145

142146
Stream &_serialStream;
143147

144-
#if defined(SERIAL_TX_BUFFER_SIZE)
148+
#if defined(SERIAL_TX_BUFFER_SIZE) && !defined (ESP32) && !defined (ESP8266)
145149
int _serialTransmissionBufferLength = SERIAL_TX_BUFFER_SIZE;
146150
#else
147151
int _serialTransmissionBufferLength = SERIAL_BUFFER_SIZE;

0 commit comments

Comments
 (0)