An enhanced serial terminal library for Arduino based on ErriezSerialTerminal, featuring command history, tab completion, larger buffers, and a modern interactive command-line experience.
This enhanced version transforms your Arduino serial terminal into a modern, user-friendly command interface:
| Feature | ErriezSerialTerminal | PeterSerialTerminal |
|---|---|---|
| Receive buffer size | 32 bytes | 256 bytes |
| Max command length | 8 characters | 12 characters |
| Command history | No | Yes (up to 20 entries) |
| Arrow key navigation | No | Yes (Up/Down) |
| Tab completion | No | Yes |
| History management API | No | Yes |
| Backspace handling | Basic | Enhanced (BS + DEL) |
| Line editing & cursor control | No | Yes |
| Backward compatible | - | 100% |
Navigate through previously entered commands using arrow keys:
- Up Arrow (↑): Recall previous command
- Down Arrow (↓): Move to next command in history
Up to 20 commands are stored (128 bytes each). Duplicate consecutive commands are automatically skipped.
Press Tab to auto-complete commands:
- Single match: The command is completed automatically
- Multiple matches: All matching commands are displayed
- No match: A bell character is sent to the terminal
- Improved backspace support (handles both
^HandDEL/ ASCII 127) - Automatic character echoing for terminal programs like PuTTY
- Line clearing and cursor control for seamless editing
- Post-command handler for custom prompts (e.g.,
>)
Programmatic control over command history:
term.showHistory(); // Display all stored commands
term.clearHistory(); // Clear the history buffer
term.addToHistory("command"); // Manually add a command to historyArduino:
- UNO, Nano, Micro
- Pro / Pro Mini
- Mega / Mega2560
- Leonardo
Other platforms:
- Arduino DUE
- ESP8266 / ESP32
- SAMD21
- STM32F1
- Download or clone this repository
- Copy the folder to your Arduino libraries directory
- Restart the Arduino IDE
#include <PeterSerialTerminal.h>
SerialTerminal term('\r', ' ');
void setup()
{
Serial.begin(115200);
term.setSerialEcho(true);
term.setDefaultHandler(unknownCommand);
term.setPostCommandHandler(printPrompt);
term.addCommand("help", cmdHelp);
term.addCommand("on", cmdLedOn);
term.addCommand("off", cmdLedOff);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println(F("Type 'help' for usage."));
printPrompt();
}
void loop()
{
term.readSerial();
}
void printPrompt()
{
Serial.print(F("> "));
}
void unknownCommand(const char *command)
{
Serial.print(F("Unknown command: "));
Serial.println(command);
}
void cmdHelp()
{
Serial.println(F("Commands: help, on, off"));
}
void cmdLedOn()
{
Serial.println(F("LED on"));
digitalWrite(LED_BUILTIN, HIGH);
}
void cmdLedOff()
{
Serial.println(F("LED off"));
digitalWrite(LED_BUILTIN, LOW);
}SerialTerminal term(newlineChar, delimiterChar);| Parameter | Default | Description |
|---|---|---|
newlineChar |
'\n' |
Newline character ('\r' or '\n') |
delimiterChar |
' ' |
Separator between command and arguments |
term.addCommand("cmd", callbackFunction); // Register a command
term.setDefaultHandler(unknownHandler); // Handler for unrecognized commands
term.setPostCommandHandler(promptFunction); // Called after every commandterm.readSerial(); // Process incoming serial data (call in loop())
term.setSerialEcho(true); // Enable character echoingchar *arg = term.getNext(); // Get next space-delimited argument
char *rest = term.getRemaining(); // Get all remaining charactersterm.clearBuffer(); // Clear the serial receive buffer
term.showHistory(); // Print command history to Serial
term.clearHistory(); // Clear all history entries
term.addToHistory("cmd"); // Add a command to history manuallyThe following macros in PeterSerialTerminal.h can be adjusted:
| Macro | Default | Description |
|---|---|---|
ST_RX_BUFFER_SIZE |
256 | Serial receive buffer size (bytes) |
ST_NUM_COMMAND_CHARS |
12 | Max characters per command name |
ST_MAX_HISTORY_ENTRIES |
20 | Max number of history entries |
ST_HISTORY_ENTRY_SIZE |
128 | Max length per history entry (bytes) |
| Example | Description |
|---|---|
| ErriezSerialTerminal | Basic usage with command registration and argument parsing |
| ErriezSerialTerminal_EchoAndCallback | Character echoing, post-command handler, and interactive prompt |
None.
MIT License. See LICENSE for details.
This project is based on ErriezSerialTerminal by Erriez. The original library provides an excellent, lightweight foundation for serial command parsing on Arduino. This enhanced version builds upon that solid foundation by adding command history, tab completion, extended buffers, and improved terminal editing — while maintaining full backward compatibility. Many thanks to the Erriez team for their outstanding work on the original library.
