ArduinoModbus
ArduinoModbus copied to clipboard
Add configuration for rx buffer size in Modbus RTU server
I'm using this library as a Modbus RTU server on a Arduino Nano board.
Since I have a lot of other libraries, RAM usage is very high and the application crashes due to stack overflow.
I noticed that inside ModbusRTUServerClass:Poll()
, a request buffer is allocated:
void ModbusRTUServerClass::poll()
{
uint8_t request[MODBUS_RTU_MAX_ADU_LENGTH];
int requestLength = modbus_receive(_mb, request);
if (requestLength > 0) {
modbus_reply(_mb, request, requestLength, &_mbMapping);
}
}
MODBUS_RTU_MAX_ADU_LENGTH is 256 bytes, which is more than 10% of the RAM available on the Arduino Nano.
Since all Modbus messages in my application are way smaller than that, I added a parameter to the class begin
method in order to choose the request buffer size.
Do you think that could be useful? I can open a PR if you think so.