A simple interface for serial communication.
See Example Public Methods Release NotesThe Arduino Serial object provides a great general purpose interface for sending and receiving serial commands. This library takes care of the details and allows you to quickly define and consume an event-based serial protocol.
This is an alpha release, which means this library is functional but likely to change. Please post feedback, questions, and issues to this GitHub issues page.
Note: this library will look for a \n newline character at the end of each command. You must configure the Arduino Serial Monitor to include a \n newline character (next to baud rate), or call setFlag() to change the default value.
This library communicates over the Arduino USB port set at 115200 baud:
#include <RBD_SerialManager.h> RBD::SerialManager serial_manager; void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { if(serial_manager.isCmd("on")) { serial_manager.println("IT'S ON!"); } if(serial_manager.isCmd("pwm")) { int value = serial_manager.getParam().toInt(); serial_manager.print("SET PWM "); serial_manager.println(value); } } }
RBD::SerialManager
Create a new Serial Manager instance.
RBD::SerialManager serial_manager; void setup() { ... }
Begin serial communication over the Arduino USB cable at 115200 baud. This method must be called in setup() in order to use this library.
void setup() { serial_manager.start(); }
Provide a char to change the identifier that denotes the end of each serial command. This library defaults to having a terminating flag of \n newline character. You must configure the Arduino Serial Monitor to include a newline (next to baud rate) for this library to work by default.
void setup() { serial_manager.setFlag(";"); // default flag \n newline }
Provide a char to change the identifier between parameters in serial commands. This library defaults to having a delimiter of , comma.
void setup() { // example serial command: pwm,123 serial_manager.setDelimiter(","); // default delimiter }
Returns true when a setFlag() has been identified in the serial communication input stream and the parsed data is available from getValue().
In order for this library to behave correctly; you must wrap all real-time methods in a conditional with this method (as shown below).
void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { // example serial command: hello world serial_manager.getValue(); // returns: hello world } }
Returns a String of data parsed in chunks between setFlag() from the serial communication input stream. This method must be used with onReceive() for this library to behave correctly (as shown below).
void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { // example serial command: hello world serial_manager.getValue(); // returns: hello world } }
Returns a String of data that is the first (or only) parameter parsed from getValue(). This enables a serial command structure that follows a cmd,param template. This method must be used with onReceive() for this library to behave correctly (as shown below).
void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { // example serial command: pwm,123 serial_manager.getCmd(); // returns: pwm } }
Returns a String of data that is the second parameter parsed from getValue(). This enables a serial command structure that follows a cmd,param template. This method must be used with onReceive() for this library to behave correctly (as shown below).
void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { // example serial command: pwm,123 serial_manager.getParam().toInt(); // returns: 123 } }
Provide a String to see if it matches the current getCmd(). This method must be used with onReceive() for this library to behave correctly (as shown below).
void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { // example serial command: on if(serial_manager.isCmd("on")) { // something.on(); } // example serial command: off if(serial_manager.isCmd("off")) { // something.off(); } } }
Provide a String to see if it matches the current getParam(). This method must be used with onReceive() for this library to behave correctly (as shown below).
void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { // example serial command: servo,up if(serial_manager.isCmd("servo")) { if(serial_manager.isParam("up")) { // servo.moveToDegrees(180); } } } }
Provide any value to print to serial output. This is identical to the documented Arduino Serial.print() function.
void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { // echo command to serial serial_manager.print(serial_manager.getValue()); } }
Provide any value to print to serial output, then a carriage return and newline character will be appended. This is identical to the documented Arduino Serial.println() function.
void setup() { serial_manager.start(); } void loop() { if(serial_manager.onReceive()) { // echo command to serial with new line serial_manager.println(serial_manager.getValue()); } }