Easy Serial Com is a library for Arduino made to quickly get started with serial communication, and without getting the main code dirty by a bunch of incoming data parsing algorithm.
Works with both HardwareSerial and SoftwareSerial objects.
-
Clone the project (or download and extract).
-
Copy EasySerialCom.h and EasySerialCom.cpp to the directory where your main.cpp is located.
-
Done!
Perform the following steps in your main.cpp file:
-
Add an include:
#include <EasySerialCom.h>
-
Create a function with these exact params (name anything you want):
void onCommandReceived(Stream& serialStream, char* command, EasySerialCom::Error error) { // see working example... }
-
OPTIONAL - This step is recommended as it will allow you to switch the variable type between HardwareSerial and SoftwareSerial without having to change anything else in the whole code. Create a variable referencing to the HardwareSerial or SoftwareSerial object (for eg: Arduino Uno uses RX pin 0 and TX pin 1 for it's only HardwareSerial which can be directly referred to by Serial):
For HardwareSerial:
// replace Serial with whichever HardwareSerial you want to use from your board. HardwareSerial& mySerial = Serial;
For SoftwareSerial:
// replace 8,9 with whichever pins you want use from your board for SoftwareSerial. SoftwareSerial mySerial(8, 9);
-
Create an EasySerialCom object:
EasySerialCom easySerialCom(mySerial, onCommandReceived, maxDataLength);
- replace maxDataLength with the length of the longest command which you will receive! (64 or 128 is usually sufficient)
-
Due to a specific limitation of some microcontrollers (including Arduino Uno), and to keep the library more versatile, we will have to set the baud rate in main.cpp itself:
void setup() { mySerial.begin(9600); }
-
In loop() function, add:
void loop() { easySerialCom.loop(); }
-
That's it, now you will receive any data sent enclosed within the start and end tags in your
onCommandReceived
function!
A working example is provided in main.cpp.
- Default start and end tags are '<' and '>' respectively.
- Do not use the characters in between data which have been set as the start and end tags for that EasySerialCom object! If needed, change the start and end tags accordingly.
- The command param in onCommandReceived function will not include the start and end tags.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.