Cherry-picking sections of received serial data; Is it possible?...

Hello all, and thank you in advance for your help and suggestions,

I’m receiving data from a device on the serial line, on a loop. It comes in and prints to serial monitor like this…

+CREG?
2, 1, “00C3”, “00000D62”
(Blank Line)
OK

+CREG?
2, 1, “00C3”, “00000D62”
(Blank Line)
OK

+CREG?
2, 1, “00C3”, “00000D62”
(Blank Line)
OK

And so on…….

It works great but the problem is, I’m really only interested in the part “00C3”, “00000D62”.
Therefore, is it possible to cherry-pick specific parts of the data for processing so that it prints only “00C3”, “00000D62”, and just discards the rest?

Many thanks
Matt

Therefore, is it possible to cherry-pick specific parts of the data

Of course. To do that, you need to save the data until the end of the important stuff has arrived. We can't tell from your code whether you do that, or not.

Hi Paul,
Thanks for your input. This is my current code....
Many thanks, Matt.

String inData;
#include <SoftwareSerial.h>
SoftwareSerial ericsson(6, 7);
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;

void setup() {

Serial.begin(9600);
delay(100);
Serial.println("serial debug begin");
delay(500);

Serial.println("Initializing SD card...");
pinMode(10, OUTPUT);
delay(500);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");

return;
}
Serial.println("card initialized.");
delay(500);

ericsson.begin(9600);
delay(3000);
Serial.println("serial with phone begin");
delay(500);

ericsson.println("AT"); //Asking the phone if the connection is working
delay(2000);

ericsson.println("AT+CGREG=2"); //Put phone into correct mode
delay(2000);
}

void loop() {

ericsson.println("AT+CGREG?"); //Ask phone for cell data
Serial.println("sending AT+CGREG?...");
delay(2000);

while (ericsson.available() > 0)
{

char recieved = ericsson.read();
inData += recieved;

// Process message when new line character is recieved
if (recieved == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(inData);

if(inData == "+++\n"){ // DON'T forget to add "\n" at the end of the string.
Serial.println("OK. Press h for help.");
}

File dataFile = SD.open("datalog.txt", FILE_WRITE);

if (dataFile) {
dataFile.println(inData);
dataFile.close();
}

// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}

inData = ""; // Clear recieved buffer
}
}
}

So, you are collecting all the data available at a given time into a String, inData, and then writing that String to the SD card. You do not assure that a complete packet has arrived, nor is it clear that you know what constitutes a complete packet.

Before writing to the file, yo need to make sure that inData contains a complete packet, and the extract from it just the parts that you want to write to the file.

Paul,

So, you are collecting all the data available at a given time into a String, inData, - Yes

and then writing that String to the SD card - Yes

You do not assure that a complete packet has arrived - this is because I do not know how to

nor is it clear that you know what constitutes a complete packet - this is because I'm not.

See where this is going?....

I'm happy to do the leg work here, I just need a little guidance. So when you say "Before writing to the file, yo need to make sure that inData contains a complete packet, and the extract from it just the parts that you want to write to the file", are you aware of any examples of this that I can learn from.

Thanks again.

You do not assure that a complete packet has arrived - this is because I do not know how to

One thing that you do look for is a \n. Does that represent an end of packet marker?

What does the data in the file look like?

are you aware of any examples of this that I can learn from.

If you weren't using Strings, yes. For the time being, though, let's assume that \n is an end of packet marker. You can use the String::indexOf() method to locate the commas. The data of interest if between two commas. The String::substring() method allows you to copy the portion starting after one comma, with the number characters equal to the delta of the comma positions.

Hi Paul,

Much of this code evolved from snippets here and there from other sketches.
I was of the understanding that the \n represented the end of the incoming data, but I could be wrong.

You say you are aware of some examples if I weren't using strings.... If I don't have to use strings then I'm open to alternative methods.

However, based on the current string method then, you say I can use the String::indexOf() method to locate two commas either side of my desired data - which sounds quite cool.

Presumably the String::substring() method is yet another way of doing it, by picking out the data at a set length after a pre-set comma. This would also be quite good because the length of my incoming sentence is always exactly the same up until where my data of interest begins.

Have I understood correctly? If so, I think I'll try the String::indexOf() method first. I guess I'll find examples of this within this site.

Thanks
Matt

Have I understood correctly?

I don't think so, because I was very careful to use string where I meant string, and String where I meant String. You weren't, so, your questions are not clear.