A command-based messaging library for Arduino that enables structured communication between microcontrollers and PC applications over serial, Bluetooth, or TCP/IP.
Documentation · Examples · C# Client · PyCmdMessenger (Python)
Arduino Library Manager (recommended):
Search for "CmdMessenger" in Sketch → Include Library → Manage Libraries.
PlatformIO:
lib_deps = CmdMessenger#include <CmdMessenger.h>
enum { kSetLed, kStatus };
CmdMessenger cmdMessenger = CmdMessenger(Serial);
void onSetLed(void) {
bool ledState = cmdMessenger.readBoolArg();
digitalWrite(13, ledState ? HIGH : LOW);
cmdMessenger.sendCmd(kStatus, ledState);
}
void setup() {
Serial.begin(115200);
cmdMessenger.attach(kSetLed, onSetLed);
pinMode(13, OUTPUT);
}
void loop() {
cmdMessenger.feedinSerialData();
}Send 0,1; from the serial monitor to turn on the LED; it responds with 1,1;.
- Send and receive commands with zero or more typed arguments
- All primary data types — bool, int16, int32, float, double, char, string, binary
- Plain text or binary encoding — human-readable or efficient, your choice
- Callback-driven — attach handler functions to command IDs
- Separator-free arguments —
sendArg()family for custom packet formats - Configurable delimiters — field separator (
,) and command separator (;) are changeable - Multiple transports — Serial USB, Bluetooth, TCP/IP
- PC companion libraries — full C# (.NET) and Visual Basic implementations included
- Arduino AVR (Uno, Mega, Nano, Pro Micro, etc.)
- Arduino ARM (Due, Zero)
- ESP8266 and ESP32
- Teensy 3.x / 4.x
- Any board with a
Stream-compatible interface
Requires Arduino IDE 1.5+ or PlatformIO.
The examples/ folder contains sketches from basic to advanced, each with a matching C# PC counterpart:
| Example | Demonstrates |
|---|---|
| Receive | Receive commands with parameters |
| SendAndReceive | Bidirectional communication |
| SendAndReceiveArguments | Multiple typed arguments |
| SendAndReceiveBinaryArguments | Efficient binary encoding |
| SendWithoutSeparator | sendArg() family for custom formats |
| DataLogging | Charting with ZedGraph |
| ArduinoController | GUI control with queued commands |
| TemperatureControl | PID control with live charting |
| SimpleWatchdog | Auto-reconnect over serial |
| ConsoleShell | Interactive serial shell (no PC app) |
- Upload the
.inosketch to your board. - Open
extras/CSharp/CmdMessenger.slnin Visual Studio. - Set the matching project as startup, then run.
- Cannot connect — verify COM port and baud rate match. Use the ConsoleShell example with the Arduino Serial Monitor to test.
- Callbacks not firing — enable logging; see the SendAndReceiveArguments example.
- Sparkfun Pro Micro — set
DtrEnable = trueon the PC side. - Unit tests — run the
CommandMessengerTestsproject in the C# solution for systematic checks.
| Version | Author |
|---|---|
| Messenger (original) | Thomas Ouellet Fredericks |
| CmdMessenger v1 | Neil Dudman |
| CmdMessenger v2 | Dreamcat4 |
| CmdMessenger v3–v4 | Thijs Elenbaas & Valeriy Kucherenko |
MIT — Copyright © 2013–2026 Thijs Elenbaas.
Changelog
- [Arduino]
sendArg(),sendSciArg(),sendBinArg()— send arguments without field separator - [Arduino]
unescape()applied to string reads (fixes escaped delimiter handling) - [Arduino] Bounds-checked
readBinArg<T>()withArgOkvalidation - [Arduino] Configurable
CMDMESSENGER_MAXCALLBACKSwith compile-time zero-callback support - [Arduino] ESP8266/ESP32 type fixes
- [Arduino]
LastArgLengthtracking in argument parser - Fix:
startCommandinitialization - Fix:
comms->printused consistently (no directSerial.print) - Fix: LICENSE.md reformatted for GitHub detection
- [Arduino] Additional autoConnect sample
- [.Net/.Mono] Full threading redesign
- [.Net/.Mono] AutoConnect & watchdog functionality
- [.Net/.Mono] Tested Linux compatibility
- [.Net/.Mono] Visual Basic samples
- [.Net/.Mono] Native Bluetooth support (Windows only)
- [Arduino] Bugfix: ~1 in 1000 commands failed with multiple binary parameters
- [Arduino] Bugfix: binary send of non-number compile error
- [Arduino] Feature: send command without argument
- [Arduino] Feature: scientific notation for full float range
- [.Net/.Mono] Unit tests, performance improvements
- [Arduino] Console shell sample, minor performance improvement
- [Arduino] Bug-fix in receiving binary values
- [.Net/.Mono] 100× faster communication, single-core support
- [Arduino] Speed improvements for Teensy
- [All] Clean transport layer interface (Bluetooth, ZigBee, Web)
- [.Net/.Mono] Adaptive throttling, smart queuing
- Multi-argument commands, type arguments, binary data, escaping, acknowledgements
- Arduino IDE 022, configurable separators, Base-64 encoding, Serial Monitor debugging
