项目作者: Erriez

项目描述 :
Serial Terminal library for Arduino
高级语言: C++
项目地址: git://github.com/Erriez/ErriezSerialTerminal.git
创建时间: 2018-07-22T14:23:18Z
项目社区:https://github.com/Erriez/ErriezSerialTerminal

开源协议:MIT License

下载


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

  1. #include <ErriezSerialTerminal.h>
  2. // Newline character '\r' or '\n'
  3. char newlineChar = '\n';
  4. // Separator character between commands and arguments
  5. char delimiterChar = ' ';
  6. // Create serial terminal object
  7. SerialTerminal term(newlineChar, delimiterChar);
  8. void setup()
  9. {
  10. // Initialize serial port
  11. Serial.begin(115200);
  12. // Initialize the built-in LED
  13. pinMode(LED_BUILTIN, OUTPUT);
  14. digitalWrite(LED_BUILTIN, LOW);
  15. }

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:

  1. void setup()
  2. {
  3. ...
  4. // Add command callback handlers
  5. term.addCommand("?", cmdHelp);
  6. term.addCommand("help", cmdHelp);
  7. term.addCommand("on", cmdLedOn);
  8. term.addCommand("off", cmdLedOff);
  9. }
  10. void cmdHelp()
  11. {
  12. // Print usage
  13. Serial.println(F("Serial terminal usage:"));
  14. Serial.println(F(" help or ? Print this usage"));
  15. Serial.println(F(" on Turn LED on"));
  16. Serial.println(F(" off Turn LED off"));
  17. }
  18. void cmdLedOn()
  19. {
  20. // Turn LED on
  21. Serial.println(F("LED on"));
  22. digitalWrite(LED_BUILTIN, HIGH);
  23. }
  24. void cmdLedOff()
  25. {
  26. // Turn LED off
  27. Serial.println(F("LED off"));
  28. digitalWrite(LED_BUILTIN, LOW);
  29. }

Set default handler

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

  1. void setup()
  2. {
  3. ...
  4. // Set default handler for unknown commands
  5. term.setDefaultHandler(unknownCommand);
  6. }
  7. void unknownCommand(const char *command)
  8. {
  9. // Print unknown command
  10. Serial.print(F("Unknown command: "));
  11. Serial.println(command);
  12. }

Read from serial port

Read from the serial port in the main loop:

  1. void loop()
  2. {
  3. // Read from serial port and handle command callbacks
  4. term.readSerial();
  5. }

Get next argument

Get pointer to next argument in serial receive buffer:

  1. char *arg;
  2. // Get next argument
  3. arg = term.getNext();
  4. if (arg != NULL) {
  5. Serial.print(F("Argument: "));
  6. Serial.println(arg);
  7. } else {
  8. Serial.println(F("No argument"));
  9. }

Get remaining characters

Get pointer to remaining characters in serial receive buffer:

  1. char *arg;
  2. // Get remaining characters
  3. arg = term.getRemaining();
  4. if (arg != NULL) {
  5. Serial.print(F("Remaining: "));
  6. Serial.println(arg);
  7. }

Clear buffer

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

  1. 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.

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

Set Post Command Handler

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

  1. void setup()
  2. {
  3. ...
  4. // Set handler to be run AFTER a command has been handled.
  5. term.setPostCommandHandler(postCommandHandler);
  6. }
  7. void setPostCommandHandler()
  8. {
  9. // Print '> ' for a primitive user UI
  10. Serial.print(F("> "));
  11. }

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