Approach to reading multiple Serial line over rs232

Im looking into reading multiple lines of data from a device using an rs232 to UART adapter.
The string takes two forms and will look like this




The main difference being the line with the '-' in it.
So the data could have 3 or 4 lines, i only want the 3rd and 4th line or the 4th if it exists.
Also the data doesn't come in that often maybe a couple of times a minute.
I want to be able to capture both lines if they exist or just the one if the one with the - doesn't exist.

I know it mightnt be ideal but could i use the serial.readuntil function to read into a newline is received.

I haven't access to the piece of equipment that provides the data only the format above that it takes.

So i was thinking could i read until a newline is received, if the indexOf the received line is true for '-' i have the 3rd line so for example 2021-22. I could also check the indexof FR"EIDVID" is equal to -1 to ignore the first line.

I could also do the same for the ? in the 2nd line by doing the same as the line above.

I want to keep the third and 4th lines.
Also to get the last line i was thinking what if i check that the indexOf the read string is not equal to ? and doesnt contain FR"EIDVID" and also doesn't contain a -.

I would read until a \n was received and then use if statements to check the above using if statements.
If i did get a - id save that string In a separate variable to the 4th line.

My question was whether that approach would work or would i lose the remaining data if any of those conditions were met.

I only want the last two lines if the 4 lines exist or the last one if the data only has 3 lines e.g. the one with the - is missing.

Sorry if the question is long and awkward.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

The second example will receive your data line by line. When a full line is received the variable newData will be set to true.

When a message has been received you can check (using the strcmp() function) if it is the first line. If it is set a variable to 1. If the next line is '?' then update the variable to 2. When the next line arrives it will be line 3.


Hello Robin2,
Thank you for the response and the sample code I will attempt to implement it for my uses.
Ok so i think if i use you're advice id 1: read the line until a newline character is received. I'll check if it's equal to FR"EIDVID" and set the variable to 1.
Check line two if it's equal to ?, then the variable is set to 2. Check the next line for the hyphen, if its there the variable is set to 3, otherwise its 4... i think. The 3rd line is not guaranteed to be there but the 4th one will be there although itll take the place of 3rd one. I'll have data which consists of either 3 or 4 lines, ideally 4 if the equipment is set up correctly.
I prefer reading into a char array as opposed to that readstringUntil function.
Then if new data comes in, the variable will reset again and start over.
That sounds like the way forward, if you think ive missed something please feel free to correct me.
Also thank you for your help i really appreciate it!!

That sounds like the way forward,

The Arduino system is great for learning-by-doing.