Skip to content

Size the SoftwareSerial ISR edge buffer independently of the byte buffer - #1264

Open
Allram wants to merge 1 commit into
UtilitechAS:mainfrom
Allram:pr/swserial-isr-buffer
Open

Allram wants to merge 1 commit into
UtilitechAS:mainfrom
Allram:pr/swserial-isr-buffer

Conversation

@Allram

@Allram Allram commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Problem

EspSoftwareSerial keeps two receive buffers: the byte buffer and an ISR buffer with one 32-bit entry per bit transition. PassiveMeterCommunicator passes the same capacity for both. A byte at 8E1 produces up to 11 transitions, so a 256 entry ISR buffer holds roughly 25 bytes, about 0.1 s of data at 2400 baud. Any loop() stall longer than that (serving the web UI, an MQTT publish burst, a LittleFS write) overflows the ISR buffer and the reader reports "Serial buffer full" although the byte buffer is almost empty. The library's own documentation suggests bufCapacity * (2 + data bits + parity).

Fix

Scale the ISR buffer by SWSERIAL_ISR_BUFFER_FACTOR (default 4: about 0.4 s of slack, 1 kB extra with the default 64 byte buffer). The library default of 11 would be exact but costs 11 kB with a 256 byte buffer, which ESP8266 cannot afford. The factor can be overridden with -D SWSERIAL_ISR_BUFFER_FACTOR=n.

Tested

ESP8266 reading an Aidon 6525 (2400 baud 8E1) on GPIO5 via software serial over a weak WiFi link. Before: loading the web UI reliably produced "Serial buffer overflow" in the debug log. With the factor raised, the same test produced none.

EspSoftwareSerial keeps two receive buffers: the byte buffer and an ISR
buffer with one 32-bit entry per bit transition. We pass the same capacity
for both, but a byte at 8E1 produces up to 11 transitions, so a 256 entry
ISR buffer holds roughly 25 bytes, about 0.1 s of data at 2400 baud. Any
loop() stall longer than that (serving the web UI, an MQTT publish burst,
a LittleFS write) overflows the ISR buffer and the reader reports
"Serial buffer full" even though the byte buffer is almost empty.

Scale the ISR buffer by SWSERIAL_ISR_BUFFER_FACTOR (default 4: ~0.4 s of
slack, 1 kB extra with the default 64 byte buffer). The library default of
11 would be correct but costs 11 kB with a 256 byte buffer, which ESP8266
cannot afford. The factor can be overridden at build time.

Verified on an ESP8266 reading an Aidon 6525 over GPIO5 (software serial):
with the factor at 8, loading the web UI over a weak WiFi link no longer
produces serial buffer overflows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant