We have a series of posts aimed at delving deeper into the use of the serial port on a processor like Arduino. In this post, we are going to see how to receive a comma-separated array.
Previously, we have seen how to receive characters and text strings, and how to receive integers and floating-point numbers. Along with these, receiving a comma-separated array is another of the most common requests regarding serial port usage.
Of course, although we refer to a comma-separated array, the code is extensible to any other delimiter, such as a period, semicolon, or any other character you might need.
But, before addressing the problem, it’s good to reflect on why you want to receive a comma-separated array. If what we want is to send/receive a series of elements (ints, floats…) between two devices, it’s better to define a frame and send the bytes directly.
If out of ignorance (or laziness), we are going to send a series of elements as a comma-separated array and then perform the text conversion, we are making the process inefficient unnecessarily, both in speed and processor load.
Therefore, we should not use a comma-separated array when we control both agents (sender and receiver). Moreover, in this case, we should never send anything other than bytes. That’s right, the big boys send bytes.
Unfortunately, we don’t always have the possibility to define the sent/received message. Sometimes the frame will be defined by a device, or a service we want to communicate with. In that case, yes, we will have no choice but to deal with the comma-separated array (or whatever delimiter applies).
Fortunately, it’s not a complicated process. As in previous posts, there is more than one way to perform the process, with its advantages and disadvantages. Here’s how to receive the array with the help of the String class and “manually”.
In the examples, we are going to use an int array, because it’s the simplest and most common case, although it’s easy to modify it to receive a float or other type of variables.
The DEBUG functions are solely for visualizing the results; when you use this code you can remove the parts related to their definition and use.
Receive with String class
The first method we are going to use the String class to receive, split the array, and perform the conversion.
First, we read an entire line with Serial.readStringUntil(). Then, we split and convert the string using the functions of the String class.
#define DEBUG_ARRAY(a) {for (int index = 0; index < sizeof(a) / sizeof(a[0]); index++) {Serial.print(a[index]); Serial.print('\t');} Serial.println();};
String str = "";
const char separator = ',';
const int dataLength = 3;
int data[dataLength];
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
str = Serial.readStringUntil('\n');
for (int i = 0; i < dataLength ; i++)
{
int index = str.indexOf(separator);
data[i] = str.substring(0, index).toInt();
str = str.substring(index + 1);
}
DEBUG_ARRAY(data);
}
}
This method is simple and efficient, and should be the preferred option for receiving comma-separated arrays.
Receive with char array and naive method
If we cannot, or do not want to use the functions of the String class, we can perform the process “manually”, which we have become accustomed to calling the “naive” method in previous posts.
#define DEBUG_ARRAY(a) {for (int index = 0; index < sizeof(a) / sizeof(a[0]); index++) {Serial.print(a[index]); Serial.print('\t');} Serial.println();};
String str = "";
const char separator = ',';
const int dataLength = 3;
int dataArray[dataLength];
int data = 0;
int dataIndex = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available())
{
char incomingChar = Serial.read();
if (incomingChar >= '0' && incomingChar <= '9')
{
data = (data * 10) + (incomingChar - '0');
}
else if (incomingChar == separator)
{
dataArray[dataIndex] = data;
if (++dataIndex >= dataLength) dataIndex = 0;
data = 0;
}
else if (incomingChar == '\n')
{
dataArray[dataIndex] = data;
data = 0;
dataIndex = 0;
DEBUG_ARRAY(dataArray);
}
}
}
The process is not much more complex, although the code is longer, so we should only use it when it is really not possible to use the String class.
EasyComma Library
What if we put it in a library? Of course we can! Here you have the EasyComma library, which allows us to read a series of integers delimited by a separator (comma by default) in a comfortable and easy way. Enjoy!
Download the code
All the code from this post is available for download on Github.

