How to Parse Serial Data in Arduino: Simple Guide
To parse serial data in Arduino, use
Serial.available() to check if data is ready, then read it with Serial.read() or Serial.readStringUntil(). You can convert the received data into usable values by splitting strings or using functions like toInt().Syntax
Here are the main functions to read serial data in Arduino:
Serial.available(): Returns the number of bytes available to read.Serial.read(): Reads one byte (character) from the serial buffer.Serial.readStringUntil(char terminator): Reads characters until the terminator is found.
You use these to check for incoming data and then read it for parsing.
arduino
if (Serial.available() > 0) { char incomingByte = Serial.read(); // process incomingByte } String data = Serial.readStringUntil('\n'); // reads until newline character
Example
This example reads a line of serial data ending with a newline, splits it by commas, and converts each part to an integer.
arduino
void setup() { Serial.begin(9600); while (!Serial) { ; } Serial.println("Send numbers separated by commas, e.g. 10,20,30"); } void loop() { if (Serial.available()) { String input = Serial.readStringUntil('\n'); int values[3]; int index = 0; int start = 0; for (int i = 0; i <= input.length(); i++) { if (input.charAt(i) == ',' || i == input.length()) { String part = input.substring(start, i); values[index++] = part.toInt(); start = i + 1; } } Serial.print("Parsed values: "); for (int i = 0; i < index; i++) { Serial.print(values[i]); Serial.print(" "); } Serial.println(); } }
Output
Send numbers separated by commas, e.g. 10,20,30
Parsed values: 10 20 30
Common Pitfalls
Common mistakes when parsing serial data include:
- Not checking
Serial.available()before reading, which can cause errors. - Assuming all data arrives at once; serial data can come slowly and in parts.
- Not handling the end of data properly, like missing newline terminators.
- Using
Serial.read()without converting bytes to the correct data type.
Always wait for complete data and parse carefully.
arduino
/* Wrong way: reading without checking availability */ if (Serial.available() > 0) { char c = Serial.read(); // process c } /* Right way: */ if (Serial.available() > 0) { char c = Serial.read(); // process c }
Quick Reference
| Function | Description |
|---|---|
| Serial.available() | Returns number of bytes available to read |
| Serial.read() | Reads one byte from serial buffer |
| Serial.readStringUntil(char) | Reads characters until the specified terminator |
| String.toInt() | Converts string to integer |
| String.substring(start, end) | Extracts part of a string |
Key Takeaways
Always check Serial.available() before reading serial data.
Use Serial.readStringUntil() to read complete lines ending with a terminator.
Parse the received string by splitting and converting parts to needed data types.
Handle incomplete or slow serial data carefully to avoid errors.
Convert bytes or strings properly to integers or other types before use.