Serial Parsing Long Mixed Sting and Integer CSV


I'm really struggling with this. I am receiving serial input in the form of something like:

MACRO, ?Q600, 45987

Also, I know the input from the control begins with > and ends with /r/n. I need just the final value (45987 in this case), which is a timer in seconds. The issues are:

  1. How to use the commas to separate out the third entry and store it as a variable?
  2. The third number is sometimes very long, causing a buffer overflow sometimes. How can I deal with this.

Below is pasted the portino of my code which is causing the problem. I know it's wrong, but at least a start to help explain what I need.

Any help is greatly appreciated. Thanks in advance

if (Serial.available() > 0) {
int first = Serial.parseInt();
int second = Serial.parseInt();
feedTime = second;

could you simply read a character at a time until you receive the second comma then call Serial.parseInt() ?
the value 45987 will not fit into a 16 bit int - after receiving the comma you could call Serial.readString() then call atol() to parse the String into a long int

Read Robin2's updated serial input basics thread.

Once you have the data in a nul terminated character array, you can use strrchr to find the last comma and take it from there.

Thanks, guys. I’m gonna have to struggle through them a bunch, but these both seem like very plausible solutions. I will try them both out at work tomorrow. Thanks for such fast responses too, wow. This forum great!

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.


Hi all thanks for the help so far. Robin 2, your guide is fantastic. With your help I have made significant progress and am almost there. I modified my code and tested it using serial monitor with an arduino connected to my computer, like that it works perfectly.

However, when I connect it to the machine that I am trying to pull the data from (a Haas VF-2 CNC mill) it just says zero. I know I can communicate with the machine because I have gotten it to report jumbled versions of the message with various old versions of my code. I have also communicated with the machine using Putty and a laptop. But, it will not work for some reason.

Could you please look at what I have below and see if you can suggest anything I might be missing? thanks in advance again.

Here is what I have:

-Haas’s website says the machine will respond with this format: Begins with > and ends with /r/n (aren’t those slashes backwards? Could that be a clue?) Here is link to Haas’s info if that helps.

-When I communicate with the machine through putty with my laptop it works and the response is shown in attached picture

-Here is the relevant portion of the code I using:

void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = ‘>’;
char endMarker = ‘/r/n’;
char rc;

while (Serial.available() > 0 && newData == false) {
Serial.write(“Q600 3022 \n”);
delay (500);
rc =;

if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
if (ndx >= numChars) {
ndx = numChars - 1;
else {
receivedChars[ndx] = ‘\0’; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;

else if (rc == startMarker) {
recvInProgress = true;

Attached also is the entire (somewhat messy and glued together) code I’m using if it helps. For context, I am reading the timer value off the Haas, doing some logic, and showing a number on an LCD display as well as on a web page. Right now I’m just trying to even communicate with the haas and read it’s value correctly. The rest of the code works without problems.

sketch_jul19a.ino (9.11 KB)

Don't stick random bits of code into my function

  while (Serial.available() > 0 && newData == false) {
     Serial.write("Q600 3022 \n");
     delay (500);

That's going to screw up my nicely constructed system. I went to considerable trouble to avoid having any delay()s.

Put your code in your own function and call it from loop(). Then tell us what happens.

And if you do need timing intervals use millis() rather than delay() - this is illustrated in Several Things at a Time


You can not have multiple characters for the end marker.

Try '\r'. You are correct that it should be a backslash.

The '\n' will (eventually) be ignored.

Thanks for the response, I think the multiple character ending sounds like it could be an issue, I'll give that a try. @Robin2...(edited) oh now I see what you mean, I will try to add it in loop as suggested. I didn't think that would work correctly before, but you obviously intended it to be that way so it could work. okay, will try.