The EasyCom library allows us to easily read a series of integers sent through the serial port. It is an easy way to communicate with our project, especially in simple developments or in early stages. In more advanced developments, we generally prefer the direct sending of bytes with the ComCenter library.
An EasyComma object is initialized by indicating its capacity, which is the maximum number of integers that the object can store. To receive data, we use the Receive() method, which we will call frequently in the control loop.
During reception, EasyComma checks if there are data to be received. If so, it processes them, and stores the received values in the internal array of integers. By default, the separator between integers is the comma (’,’) and the separator between groups of integers is the line feed (‘\n’), although we can change them to any other character.
When the reception of a package is finished, the callback action that we pass to the Receive() function is executed. This allows the main loop to be freed from functions.
If we receive more integers than the capacity with which we have created EasyComma, the array is rewritten from the beginning, similar to a circular buffer. In this way, the data stored in EasyComma are always the last N received values.
The EasyComma class has the Count() method that returns the number of values received in the last package of integers, and LastIndex() that returns the last position in which it has been written, used when we have overflow of the internal array.
For more information, check the entry Array separated by commas via serial port in Arduino.
User Manual
Constructor
EasyComma(size_t capacity);
Using the library
// Access the received elements
int& operator[](const size_t index);
// Separator between received integers
char Separator = ',';
// Separator between received groups
char EndSeparator = '\n';
// Get the capacity
size_t Capacity();
// Get the number of stored objects
size_t Count();
// Get the index of the last stored element
size_t LastIndex();
// Returns true if the number of elements in the last package is greater than the capacity
bool IsOverflow();
Examples
The EasyComma library includes the following examples to illustrate its use.
- EasyComma: Usage example
Installation
- Download the latest version from GitHub
- Unzip the file
- Copy to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE