From 4b22252a133bc079c0a817af74dd87f049da4115 Mon Sep 17 00:00:00 2001 From: Henri de Jong Date: Sun, 17 Jan 2021 11:59:15 +0100 Subject: [PATCH 1/2] Increase buffer size --- src/src/PluginStructs/P044_data_struct.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/src/PluginStructs/P044_data_struct.h b/src/src/PluginStructs/P044_data_struct.h index a008ec9eb5..f23dbdbc42 100644 --- a/src/src/PluginStructs/P044_data_struct.h +++ b/src/src/PluginStructs/P044_data_struct.h @@ -15,7 +15,7 @@ #define P044_CHECKSUM_LENGTH 4 #define P044_DATAGRAM_START_CHAR '/' #define P044_DATAGRAM_END_CHAR '!' -#define P044_DATAGRAM_MAX_SIZE 1024 +#define P044_DATAGRAM_MAX_SIZE 2048 struct P044_Task : public PluginTaskData_base { From a5100e7050548b0202019ddfc017256fcbc61edb Mon Sep 17 00:00:00 2001 From: TD-er Date: Sun, 17 Jan 2021 13:01:17 +0100 Subject: [PATCH 2/2] Use max seen P1 message length to allocate buffer --- src/src/PluginStructs/P044_data_struct.cpp | 14 +++++++++----- src/src/PluginStructs/P044_data_struct.h | 5 ++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/src/PluginStructs/P044_data_struct.cpp b/src/src/PluginStructs/P044_data_struct.cpp index 6f28c92c15..5179b7bd04 100644 --- a/src/src/PluginStructs/P044_data_struct.cpp +++ b/src/src/PluginStructs/P044_data_struct.cpp @@ -117,8 +117,12 @@ void P044_Task::checkBlinkLED() { } void P044_Task::clearBuffer() { + if (serial_buffer.length() > maxMessageSize) { + maxMessageSize = serial_buffer.length(); + } + serial_buffer = ""; - serial_buffer.reserve(P044_DATAGRAM_MAX_SIZE); + serial_buffer.reserve(maxMessageSize); } void P044_Task::addChar(char ch) { @@ -143,11 +147,11 @@ bool P044_Task::checkDatagram() const { const int checksumStartIndex = endChar + 1; - if (PLUGIN_044_DEBUG) { + #ifdef PLUGIN_044_DEBUG for (unsigned int cnt = 0; cnt < serial_buffer.length(); ++cnt) { serialPrint(serial_buffer.substring(cnt, 1)); } - } + #endif // calculate the CRC and check if it equals the hexadecimal one attached to the datagram unsigned int crc = CRC16(serial_buffer, checksumStartIndex); @@ -332,11 +336,11 @@ bool P044_Task::handleChar(char ch) { // input is not a datagram char addLog(LOG_LEVEL_DEBUG, F("P1 : Error: DATA corrupt, discarded input.")); - if (PLUGIN_044_DEBUG) { + #ifdef PLUGIN_044_DEBUG serialPrint(F("faulty char>")); serialPrint(String(ch)); serialPrintln("<"); - } + #endif state = ParserState::WAITING; // reset } diff --git a/src/src/PluginStructs/P044_data_struct.h b/src/src/PluginStructs/P044_data_struct.h index f23dbdbc42..82c4057c2b 100644 --- a/src/src/PluginStructs/P044_data_struct.h +++ b/src/src/PluginStructs/P044_data_struct.h @@ -7,9 +7,7 @@ #include -#ifndef PLUGIN_044_DEBUG - # define PLUGIN_044_DEBUG false // extra logging in serial out -#endif // ifndef PLUGIN_044_DEBUG +// #define PLUGIN_044_DEBUG // extra logging in serial out #define P044_STATUS_LED 12 #define P044_CHECKSUM_LENGTH 4 @@ -97,6 +95,7 @@ struct P044_Task : public PluginTaskData_base { boolean CRCcheck = false; ESPeasySerial *P1EasySerial = nullptr; unsigned long blinkLEDStartTime = 0; + size_t maxMessageSize = P044_DATAGRAM_MAX_SIZE / 4; }; #endif