Good evening!
First here is my function code:
char * SIM908::get_value(char * chaine, byte index)
{
// +CENG:0,0872,21,99,228,02,11,7979,10,00,1770,255 <= ind
// Initiate the
byte ind = 0; // position where "are" in the above char
byte x = 0; // index of the variable val. which return the desired value
byte ic = 0; // Index of the variable chaine.
bool flag = false; // True when we are in the position to return (for exemple 7979)
char val[10]; // Variable which contain the value to return
// if the need the first block (+CENG1), flag must be true at the begining
if(index == 0)
{
flag = true;
}
do // Read 'chaine' carcater by carater
{
switch(chaine[ic]){
case ',': // if it read a comma, it increment ind, because we will go to the next block
ind++; // See fefault:
ic++;
continue; // Retunr to the begining of the do loop
default:
if(ind == index) // if ind is egal to index (index is the block we want to return)
{
val[x] = chaine[ic]; // The store the carater to val
// Serial.print(F("Def")); Serial.println(val[x]);
x++;
}
}
ic++;
//Serial.print(F("co")); Serial.println(co);
}while( ic < strlen(chaine)); // Do as long as ix is smaller than the lenght of 'chaine
val[x]='\0'; // Terminate the char with \0
Serial.print(F("sr:"));Serial.println(val);
return val; // return the caracter/value we want to treat.
}
This is the value (char) which pass to *chaine of the function
+CENG:0,0872,21,99,228,02,11,7979,10,00,1770,255
My goal is the return 228,02,7979 and 1770.
Serial.println(get_value(response[2],4));
Serial.println(get_value(response[2],5));
Serial.println(get_value(response[2],7));
Serial.println(get_value(response[2],10));
response[1] contains +CENG:0,0872,21,99,228,02,11,7979,10,00,1770,255.
Explication
This code works partialy and the problem is around the return.
Before the return I print the value of 'val
Serial.println(val);
and this display what I expected for.
However, when I
Serial.println(get_value(response[2],4));
it print nothing correct. (keep in mind that response[1] contain CENG:0,0872,21,99,228,02,11,7979,10,00,1770,255.
Here is what display my terminal with this code
Serial.println(response[2]);
Serial.println(get_value(response[2],0));
Serial.println(get_value(response[2],4));
Serial.println(get_value(response[2],5));
Serial.println(get_value(response[2],7));
Serial.println(get_value(response[2],10));
+CENG:0,0875,25,99,228,02,12,750d,28,00,1388,255
sr:+CENG:0
=�v�
sr:228sr:02
sr:750d
sr:1388
You can observe that the “sr:” display the correct value but
Serial.println(get_value(response[2],7));
dont display the same value.
I am asking my self if it’s because I am using a local variable. But the local variable is return before the function ends???
How can you suggest me to perfect my function?
Many thanks