Code Monkey home page Code Monkey logo

erriezserialterminal's Introduction

Serial Terminal library for Arduino

This is a universal Serial Terminal library for Arduino to parse ASCII commands and arguments.

Serial Terminal

Hardware

Any Arduino hardware with a serial port, such as:

Arduino:

  • UNO
  • Nano
  • Micro
  • Pro or Pro Mini
  • Mega or Mega2560
  • Leonardo

Other targets:

  • DUE
  • ESP8266
  • ESP32
  • SAMD21
  • STM32F1

Examples

Arduino IDE | Examples | Erriez Serial Terminal |

Documentation

Usage

Initialization

Create a Serial Terminal object. This can be initialized with optional newline and delimiter characters.

Default newline character: '\n' Default delimiter character: Space

#include <ErriezSerialTerminal.h>

// Newline character '\r' or '\n'
char newlineChar = '\n'; 
// Separator character between commands and arguments
char delimiterChar = ' ';

// Create serial terminal object
SerialTerminal term(newlineChar, delimiterChar);


void setup()
{
    // Initialize serial port
    Serial.begin(115200);
    
    // Initialize the built-in LED
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
}

Register new commands

Commands must be registered at startup with a corresponding callback handler . This registers the command only, excluding arguments.

The callback handler will be called when the command has been received including the newline character.

An example of registering multiple commands:

void setup()
{
    ...

    // Add command callback handlers
    term.addCommand("?", cmdHelp);
    term.addCommand("help", cmdHelp);
    term.addCommand("on", cmdLedOn);
    term.addCommand("off", cmdLedOff);
}

void cmdHelp()
{
    // Print usage
    Serial.println(F("Serial terminal usage:"));
    Serial.println(F("  help or ?          Print this usage"));
    Serial.println(F("  on                 Turn LED on"));
    Serial.println(F("  off                Turn LED off"));
}

void cmdLedOn()
{
    // Turn LED on
    Serial.println(F("LED on"));
    digitalWrite(LED_BUILTIN, HIGH);
}

void cmdLedOff()
{
    // Turn LED off
    Serial.println(F("LED off"));
    digitalWrite(LED_BUILTIN, LOW);
}

Set default handler

Optional: The default handler will be called when the command is not recognized.

void setup()
{   
    ...

    // Set default handler for unknown commands
    term.setDefaultHandler(unknownCommand);
}

void unknownCommand(const char *command)
{
    // Print unknown command
    Serial.print(F("Unknown command: "));
    Serial.println(command);
}

Read from serial port

Read from the serial port in the main loop:

void loop()
{
    // Read from serial port and handle command callbacks
    term.readSerial();
}

Get next argument

Get pointer to next argument in serial receive buffer:

char *arg;

// Get next argument
arg = term.getNext();
if (arg != NULL) {
    Serial.print(F("Argument: "));
    Serial.println(arg);
} else {
    Serial.println(F("No argument"));
}

Get remaining characters

Get pointer to remaining characters in serial receive buffer:

char *arg;

// Get remaining characters
arg = term.getRemaining();
if (arg != NULL) {
    Serial.print(F("Remaining: "));
    Serial.println(arg);
}

Clear buffer

Optional: The serial receive buffer can be cleared with the following call:

term.clearBuffer();

Enable/Disable Character Echoing

Optional: Allow for any entered charecters to be printed back to the Serial interface. This is useful for terminal programs like PuTTY. Supports both backspace characters, ^H and ^127.

term.setSerialEcho(true); //Enable Character Echoing

Set Post Command Handler

Optional: Add a function to be called AFTER a command has been handled.

void setup()
{   
    ...

    // Set handler to be run AFTER a command has been handled.
    term.setPostCommandHandler(postCommandHandler);
}

void setPostCommandHandler()
{
    // Print '> ' for a primitive user UI
    Serial.print(F("> "));
}

Library configuration

SerialTerminal.h contains the following configuration macro's:

  • ST_RX_BUFFER_SIZE : The default serial receive buffer size is 32 Bytes. This includes the command and arguments, excluding the '\0' character.
  • ST_NUM_COMMAND_CHARS: The default number of command characters is 8 Bytes, excluding the '\0' character.

Library dependencies

  • None.

Library installation

Please refer to the Wiki page.

Other Arduino Libraries and Sketches from Erriez

erriezserialterminal's People

Contributors

anderswb avatar calw20 avatar erriez avatar per1234 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

erriezserialterminal's Issues

Add case insensitive commands

It would be nice if the command string wasn't case sensitive.

Something along the lines of adding it as an OPTIONAL parameter to addCommand().

This could be implemented by using strupr() or strlwr() in addCommand() when adding to the _commandList . Then use the same (lower or upper) function in the readSerial() function. Specifically here:

if (strncmp(command, _commandList[i].command, ST_NUM_COMMAND_CHARS) == 0) {

The conversion would only be carried out if configured to do so.

Using Serial1 or Serial2, or multiple serial ports.

I'm running on an ESP32 and I need a console to be on two serial port, both Serial and Serial1.
Would this be possible?
The trick here is that Serial is the normal serial that the boot loader etc uses, this port is used by a user to connect to the board and read/write settings. Serial1 is connected to a Nextion LCD that will write commands and poll for data. Having the same console interface on both serial port makes it all a lot easier.

Serial2 is used for modbus communication.

Error detecting Command string same start

I have discovered a little errorin the comparison of the command string.
It's like it takes the first that matches part of the string.

I have the following commands but the code always runs it's like i entered the first command "Setting_Ind_Bom"
so both "Setting_Ind_Port" and "Setting_Ind_Vejslojfe" get detected as "Setting_Ind_Bom"

term.addCommand("Setting_Ind_Bom", cmd_Setting_Ind_Bom);
term.addCommand("Setting_Ind_Port", cmd_Setting_Ind_Port);
term.addCommand("Setting_Ind_Vejslojfe", cmd_Setting_Ind_Vejslojfe);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.