Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

AntiNoiseSerial

AntiNoiseSerial is a lightweight Arduino library for cleaning text received through a Stream such as Serial, Serial1, Serial2, or a compatible software serial implementation.

The library removes non-printable bytes, keeps a bounded FIFO-style text buffer, and provides simple pattern matching with MySQL LIKE-style wildcards.

Features

  • Accepts printable ASCII characters (32126), carriage return, and newline.
  • Discards other bytes commonly seen as corrupted or unwanted serial data.
  • Optional spacing prevents valid words on either side of a noise burst from being joined together.
  • Bounded buffer with configurable maximum size.
  • Works with any Arduino class derived from Stream.
  • Pattern matching with % and _ wildcards.
  • No external dependencies beyond the Arduino core.

Supported targets

The library is intended for boards that provide the Arduino Stream and String APIs, including Arduino boards and Arduino-core projects for ESP32 and STM32. It can be used above UART-to-RS-232 or UART-to-RS-485 hardware, but it does not configure the electrical interface itself.

Installation

Arduino IDE — ZIP installation

  1. Download this repository as a ZIP file.
  2. Open Arduino IDE.
  3. Select Sketch → Include Library → Add .ZIP Library.
  4. Select the downloaded ZIP file.
  5. Add #include <AntiNoiseSerial.h> to the sketch.

Manual installation

Copy the inner AntiNoiseSerial directory into the Arduino libraries folder, then restart Arduino IDE.

PlatformIO

Add the Git repository to platformio.ini:

lib_deps =
  https://github.com/DprasK/AntiNoiseSerial.git

Quick start

#include <AntiNoiseSerial.h>

// Stream, maximum buffer size, insert a space after a noise burst.
AntiNoiseSerial input(Serial, 128, true);

void setup() {
  Serial.begin(9600);
}

void loop() {
  input.update();

  String message = input.getBuffer();
  if (message.indexOf('\n') == -1) {
    return;
  }

  if (input.like("%SET_TEMP:%")) {
    Serial.println("Temperature command received");
  } else if (input.like("%ID__%")) {
    Serial.println("Two-character ID received");
  }

  input.clear();
}

A complete example is available in AntiNoiseSerial/examples/sample/sample.ino.

Constructor

AntiNoiseSerial(Stream& serialPort,
                size_t maxBufSize = 128,
                bool useSpacing = false);
Parameter Description
serialPort Any Arduino-compatible Stream instance.
maxBufSize Maximum number of characters retained in the buffer.
useSpacing Inserts one space between valid text segments separated by discarded bytes.

API

Method Description
void update() Reads all currently available bytes and updates the clean buffer. Call it repeatedly from loop().
String getBuffer() Returns a copy of the current clean buffer.
void clear() Clears the internal buffer before receiving the next message.
bool like(String pattern) Compares the entire buffer using % and _ wildcards.

Pattern matching

like() follows two common SQL wildcard rules:

Wildcard Meaning Example
% Zero or more characters %ON% matches text containing ON.
_ Exactly one character ID__ matches ID01 or IDAB.

Matching is case-sensitive. Add % at the start and end of a pattern when the value may occur anywhere inside a longer buffer.

input.like("READY");       // Exact buffer match
input.like("%READY%");    // Contains READY
input.like("SENSOR_?");   // '?' is literal; it is not a wildcard
input.like("SENSOR__");   // One arbitrary character after SENSOR_

Spacing mode

With spacing disabled, discarded bytes are simply removed:

Input bytes:  HELLO [noise] WORLD
Buffer:       HELLOWORLD

With spacing enabled, a single separator is inserted:

Input bytes:  HELLO [noise] WORLD
Buffer:       HELLO WORLD

Consecutive noise bytes do not create repeated spaces.

Buffer behavior

When the buffer reaches maxBufSize, each new valid character removes the oldest character before being appended. Choose a size large enough for the longest expected message, including line endings.

Important limitations

  • The library filters text by byte value; it cannot determine whether a printable character was corrupted into another printable character.
  • It is not a replacement for CRC/checksum validation, acknowledgements, retries, shielding, grounding, termination, or proper serial-level hardware.
  • Do not use it for binary protocols because valid non-printable bytes will be discarded.
  • Validate commands separately before controlling machinery or other safety-critical equipment.

Repository structure

AntiNoiseSerial/
├── library.properties
├── examples/sample/sample.ino
└── src/
    ├── AntiNoiseSerial.cpp
    └── AntiNoiseSerial.h

Contributing

Issues and pull requests are welcome. When reporting a serial problem, include the board, Arduino core version, baud rate, serial interface, and a sample of the received bytes.

About

Anti Noise Serial for Arduino/esp32/stm32 or rs232

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages