cattledog:
Instead of String Object functions like .indexof(), I use the c-string functions like strstr(), strcmp(), strtok() and strcpy() to parse information from the received message, as a null terminated character array.
The specifics of how to do that will depend on the messages you receive, but you have refused to post any message, in spite of being asked several times.
Here is an example how I parse the number and message from a +CMT response
//+CMT: "+447xxxxxxxx","","18/09/21,20:54:59+04" SITE WHATS HAPPENING
//enter the message from serial monitor for testing
//actual modem rely message begins with
char Grsp[100];//size for max message
char str1[30];//size appropriately for parsed characters
char str2[30];//size appropriately for parsed characters
boolean newMessage = false;
boolean prefixMatch = false;
void setup()
{
Serial.begin(115200);
//set ms timeout for Serial.readBytes()
Serial.setTimeout(100);
}
void loop() {
while (Serial.available() > 0)
{
//readBytes returns number read not zero referenced
byte numChars = Serial.readBytes(Grsp, 100);
Grsp[numChars] = '\0';//null Terminate
newMessage = true;
Serial.println(Grsp);
}
//can define a series of prefix messages for match
//if (strncmp(Grsp, "+CMT", 4) == 0 && newMessage == true)
//change to accomodate lead characters
if(strstr(Grsp, "+CMT") != 0 and newMessage == true)
{
Serial.println("prefixMatch +CMT");
prefixMatch = true;
newMessage = false;
}
else if (newMessage == true)
{
Serial.println("no prefixMatch");
newMessage = false;
}
if (prefixMatch == true)
{
prefixMatch = false;
char* strtokIndx;
//find first "
strtokIndx = strtok(Grsp, " " ");//need excape for " delimiter
//find second "
strtokIndx = strtok(NULL, " " ");
strcpy(str1, strtokIndx); //characters between first set of ""
for (byte i = 0; i < 3; i++) //skip3 " delimiters
{
strtokIndx = strtok(NULL, " " ");
Serial.println(strtokIndx);
}
//find ending character message
strtokIndx = strtok(NULL, ""); //last " to end of string no delimiter \0 terminator
strcpy(str2, strtokIndx + 1); //skip space preceding SITE
Serial.print("Number = ");
Serial.println(str1);
Serial.print("Ending message = ");
Serial.println(str2);
}
}
Thank you for your ansewer I tried , the code with GSM module but I can get an ansewer from the GSM Module connected to Serial1.
I used this code :
char Grsp[100];//size for max message
char str1[30];//size appropriately for parsed characters
char str2[30];//size appropriately for parsed characters
boolean newMessage = false;
boolean prefixMatch = false;
void setup() {
Serial.begin(9600);
Serial.setTimeout(100);
Serial1.begin(9600);
//set ms timeout for Serial.readBytes()
Serial1.setTimeout(100);
// gsminit();
}
void loop() {
while (Serial1.available() > 0)
{
//readBytes returns number read not zero referenced
byte numChars = Serial1.readBytes(Grsp, 100);
delay(100);
Grsp[numChars] = '\0';//null Terminate
newMessage = true;
Serial.print("Grsp :"); Serial.println(Grsp);
}
//can define a series of prefix messages for match
//if (strncmp(Grsp, "+CMT", 4) == 0 && newMessage == true)
//change to accomodate lead characters <CR> <LF>
if(strstr(Grsp, "+CIEV") != 0 and newMessage == true)
{
Serial.println("prefixMatch +CMT");
prefixMatch = true;
newMessage = false;
}
else if (newMessage == true)
{
Serial.println("no prefixMatch");
newMessage = false;
}
if (prefixMatch == true)
{
prefixMatch = false;
char* strtokIndx;
//find first "
strtokIndx = strtok(Grsp, " \" ");//need excape for " delimiter
//find second "
strtokIndx = strtok(NULL, " \" ");
strcpy(str1, strtokIndx); //characters between first set of ""
for (byte i = 0; i < 3; i++) //skip3 " delimiters
{
strtokIndx = strtok(NULL, " \" ");
Serial.println(strtokIndx);
}
//find ending character message
strtokIndx = strtok(NULL, ""); //last " to end of string no delimiter \0 terminator
strcpy(str2, strtokIndx + 1); //skip space preceding SITE
Serial.print("Number = ");
Serial.println(str1);
Serial.print("Ending message = ");
Serial.println(str2);
}
}
void gsminit()
{
delay(5000);
Serial1.println("AT"); // Lancement module GMS
Serial1.println("AT+CMGF=1"); // Lancement mode SMS
Serial1.println("AT+CNMI=2,2,0,0,0"); // En attente reception
}
The consol text :
12:16:28.440 -> Grsp :
12:16:28.440 -> no prefixMatch
12:16:28.682 -> Grsp :
12:16:28.888 -> Grsp :
12:16:28.922 -> no prefixMatch
12:16:29.128 -> Grsp :
12:16:29.369 -> Grsp :
12:16:29.369 -> no prefixMatch
12:16:29.608 -> Grsp :
12:16:29.848 -> Grsp :
12:16:29.848 -> no prefixMatch
12:16:30.086 -> Grsp :
12:16:30.359 -> Grsp :
12:16:30.568 -> Grsp :
12:16:30.774 -> Grsp :
12:16:30.808 -> no prefixMatch