q
That your string contains hex digits doesn't matter, does it? Whatever is in the string you get in response to one command is what you want to send in another command, isn't it?
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
...R
capture and parse like this:
#define MAX_MESSAGE_LENGTH 64
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (char* newMessage = checkForNewMessage(Serial, '\n'))
{
if (strstr(newMessage, "CGREG"))
{
Serial.println(newMessage);
char mssg[strlen(newMessage) +1];
strcpy(mssg, newMessage); // newMessage is immutable
//+CGREG: 2,1,31D5,0214F203,7
strtok(mssg, " ,");
Serial.println(atoi(strtok(NULL, " ,")));
Serial.println(atoi(strtok(NULL, " ,")));
Serial.println(strtol(strtok(NULL, ","), NULL, 16));
Serial.println(strtol(strtok(NULL, " ,"), NULL, 16));
Serial.println(atoi(strtok(NULL, " ,")));
}
}
}
const char* checkForNewMessage(Stream& stream, const char endMarker)
{
static char incomingMessage[MAX_MESSAGE_LENGTH] = "";
static byte idx = 0;
if(stream.available())
{
incomingMessage[idx] = stream.read();
if(incomingMessage[idx] == endMarker)
{
incomingMessage[idx] = '\0';
idx = 0;
return incomingMessage;
}
else
{
idx++;
if(idx > MAX_MESSAGE_LENGTH - 1)
{
//stream.print(F("{\"error\":\"message too long\"}\n")); //you can send an error to sender here
idx = 0;
incomingMessage[idx] = '\0';
}
}
}
return NULL;
}