Newbie Question: How to read STRINGS from RX/TX

This is probably more of a C / C++ question, but I'm trying to write a sketch that will read commands (longer than one byte) from the Serial port, then execute them when the user presses enter in the terminal. I'm a bit of a C n00b, so can anyone point me at a code example or something that does this? I'm getting a bit turned around by all the pointer / array / realloc buffer stuff...


Serial data arrives at the computer much slower than the Arduino can read it. So, each pass through loop, there may or may not be serial data available to read. The Serial.available() function tells you how much is available to read.

Create a static character array, large enough to hold all the data that can reasonably be expected to be sent.

char inData[80];

Create an index to point to the next available position in the array.

int index = 0;

If there is data to be read, read a character and store it in the first available position, indicated by index:

inData[index] =;

Increment the index, to point to the next position:


NULL terminate the array:

inData[index] = '\0';

If you are using the Serial Monitor to send data, you need to re-think how to tell the Arduino that it has all the data that it needs to do something. The Serial Monitor sends data when the Send button is pushed OR when the enter key is pressed. But, the enter (carriage return/line feed) is NOT sent.

Notice that this approach does not use any pointers, malloc, realloc, etc.

Thanks PaulS! This looks very understandable (even for me!).

When you mention the "Serial Monitor", are you talking about a specific terminal program? OR some function of the Arduino IDE?

thanks again!

EDIT: Nevermind. I found it in the IDE. Thanks again!