项目作者: Dennis-van-Gils

项目描述 :
C++ library to allow listening to a serial port for commands on a Arduino(-like) device.
高级语言: C++
项目地址: git://github.com/Dennis-van-Gils/DvG_SerialCommand.git
创建时间: 2018-08-14T16:36:53Z
项目社区:https://github.com/Dennis-van-Gils/DvG_SerialCommand

开源协议:MIT License

下载


[DEPRECATED] Instead, use the improved library DvG_StreamCommand.

Serial command listener

This library allows listening to a serial port for incoming commands and act upon them. To keep the memory usage low, it uses a C-string (null-terminated character array) to store incoming characters received over the serial port, instead of using a memory hungry C++ string. Carriage return (‘\r’, ASCII 13) characters are ignored. Once a linefeed (‘\n’, ASCII 10) character is received, or whenever the incoming message length has exceeded the buffer of size BUFLEN_SERIALCOMMAND (defined in DvG_SerialCommand.h), we speak of a received ‘command’. It doesn’t matter if the command is ASCII or binary encoded.

available() should be called periodically to poll for incoming characters. It will return true when a new command is ready to be processed. Subsequently, the command string can be retrieved by calling getCmd().

Example usage on an Arduino:

  1. #include <Arduino.h>
  2. #include "DvG_SerialCommand.h"
  3. #define Ser Serial // Listen on this port
  4. DvG_SerialCommand sc(Ser);
  5. void setup() {
  6. Ser.begin(115200); // Open port
  7. }
  8. void loop() {
  9. char* strCmd; // Incoming serial command string
  10. if (sc.available()) {
  11. strCmd = sc.getCmd();
  12. // Your command string comparison routines and actions here
  13. if (strcmp(strCmd, "id?") == 0) {
  14. Ser.println("Arduino, Blinker");
  15. }
  16. }
  17. }