From f76d04b61382011fff93d81d1723535e8c454ed8 Mon Sep 17 00:00:00 2001 From: simowilso Date: Wed, 29 Jul 2026 01:05:18 +0100 Subject: [PATCH] [SERIAL] Make SERIAL gateway frame format (data/parity/stop bits) configurable (#2342) Serial.begin() was hardcoded to SERIAL_8N1 across all four UART init paths (UART0/1/2/3), with no way to override it via a build flag -- unlike SERIALBaud, which already is. Devices using non-8N1 framing (e.g. even-parity RS232 sources) currently can't be read correctly at all, not just unreliably: a parity mismatch misaligns the frame, not just adds occasional noise. Adds SERIAL_CONFIG following the existing #ifndef/#define override pattern used throughout this codebase, defaulting to SERIAL_8N1 so existing environments are unaffected byte-for-byte. Verified: default (SERIAL_8N1) and an override (SERIAL_CONFIG=SERIAL_8E1) both build successfully on ESP32. Co-authored-by: simowilso Co-authored-by: Claude Sonnet 5 --- main/config_SERIAL.h | 4 ++++ main/gatewaySERIAL.cpp | 14 +++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/main/config_SERIAL.h b/main/config_SERIAL.h index 65b1bc9..00cf5d1 100644 --- a/main/config_SERIAL.h +++ b/main/config_SERIAL.h @@ -81,6 +81,10 @@ extern bool XtoSERIAL(const char* topicOri, JsonObject& SERIALdata); # define SERIALBaud 9600 // The serial connection Baud #endif +#ifndef SERIAL_CONFIG +# define SERIAL_CONFIG SERIAL_8N1 // The serial frame format (data/parity/stop bits), e.g. SERIAL_8E1 for 8 data bits/even parity/1 stop bit +#endif + /*-------------------PIN DEFINITIONS----------------------*/ #ifndef SERIAL_UART // set hardware serial UART to be used for device communication or diff --git a/main/gatewaySERIAL.cpp b/main/gatewaySERIAL.cpp index 5f43111..ea6cc88 100644 --- a/main/gatewaySERIAL.cpp +++ b/main/gatewaySERIAL.cpp @@ -78,9 +78,9 @@ void setupSERIAL() { # if SERIAL_UART == 0 // init UART0 Serial.end(); // stop if already initialized # ifdef ESP32 - Serial.begin(SERIALBaud, SERIAL_8N1, SERIAL_RX_GPIO, SERIAL_TX_GPIO); + Serial.begin(SERIALBaud, SERIAL_CONFIG, SERIAL_RX_GPIO, SERIAL_TX_GPIO); # else - Serial.begin(SERIALBaud, SERIAL_8N1); + Serial.begin(SERIALBaud, SERIAL_CONFIG); # endif # if defined(ESP8266) && defined(SERIAL_UART0_SWAP) Serial.swap(); // swap UART0 ports from (GPIO1,GPIO3) to (GPIO15,GPIO13) @@ -91,9 +91,9 @@ void setupSERIAL() { # elif SERIAL_UART == 1 // init UART1 Serial1.end(); // stop if already initialized # ifdef ESP32 - Serial1.begin(SERIALBaud, SERIAL_8N1, SERIAL_RX_GPIO, SERIAL_TX_GPIO); + Serial1.begin(SERIALBaud, SERIAL_CONFIG, SERIAL_RX_GPIO, SERIAL_TX_GPIO); # else - Serial1.begin(SERIALBaud, SERIAL_8N1); + Serial1.begin(SERIALBaud, SERIAL_CONFIG); # endif SERIALStream = &Serial1; THEENGS_LOG_NOTICE(F("SERIAL HW UART1" CR)); @@ -101,16 +101,16 @@ void setupSERIAL() { # elif SERIAL_UART == 2 // init UART2 Serial2.end(); // stop if already initialized # ifdef ESP32 - Serial2.begin(SERIALBaud, SERIAL_8N1, SERIAL_RX_GPIO, SERIAL_TX_GPIO); + Serial2.begin(SERIALBaud, SERIAL_CONFIG, SERIAL_RX_GPIO, SERIAL_TX_GPIO); # else - Serial2.begin(SERIALBaud, SERIAL_8N1); + Serial2.begin(SERIALBaud, SERIAL_CONFIG); # endif SERIALStream = &Serial2; THEENGS_LOG_NOTICE(F("SERIAL HW UART2" CR)); # elif SERIAL_UART == 3 // init UART3 Serial3.end(); // stop if already initialized - Serial3.begin(SERIALBaud, SERIAL_8N1); + Serial3.begin(SERIALBaud, SERIAL_CONFIG); SERIALStream = &Serial3; THEENGS_LOG_NOTICE(F("SERIAL HW UART3" CR)); # endif