|
|
View previous topic :: View next topic |
Author |
Message |
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
help for function extract string |
Posted: Tue Mar 31, 2020 12:22 pm |
|
|
Hi, I have these strings:
+CBC: <bcs>, <bcl>,<voltage>
+CSQ: <rssi>,<ber>
Using strtok I can separate by "," but i get one array where i need clean.
So i search a function for extract "+CBC: " and "+CSQ: ", but the idea is it can work for any "header" like "ABCD: " or "12346786A: "
This for can get bcs and rssi or any data just after ": " from clean way.
My code
Code: | char string[50], *ptr;
char delimitador[]=": ,\r"; // Token
CHECK_VOLT(); // AT+CBC
strcpy(string,Receive_String_uart2); // +CBC: #,###,#### +CBC: <bcs>, <bcl>,<voltage>
ptr = strtok(string, delimitador); //
i=0; //
while(ptr != NULL){ //
if(*ptr != '\n'){ //
strcpy(&dat_batt[i][0],ptr); //
i++; //
} //
ptr = strtok(NULL,delimitador); //
}
|
out:
Quote: | +CBC: bcs
bcl
voltage
|
So after i need get bcs, or any data without "header".
I don't want to make manual way like this:
Code: | //! ------ CSQ----
if (CHECK_SNG()) { // CSQ: ##,## +CSQ: <rssi>,<ber>
n=0;i=0; //
for(i=0;i<5;i++){ //
SGN[i]=Receive_String_uart2[n+15]; n++;
} //
} |
|
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue Mar 31, 2020 2:08 pm |
|
|
I'm not sure if I understand you completely, but you can use a function such as strstr() to find if your string contains CBC or CSQ. You can use strchr() to find the first occurrence of ':' and then set your string to the address of the character after ':'
When I was searching for stuff in my RS232 communications I used this page a lot:
http://www.cplusplus.com/reference/cstring/ |
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
|
Posted: Tue Mar 31, 2020 3:06 pm |
|
|
dluu13 wrote: | I'm not sure if I understand you completely, but you can use a function such as strstr() to find if your string contains CBC or CSQ. You can use strchr() to find the first occurrence of ':' and then set your string to the address of the character after ':'
When I was searching for stuff in my RS232 communications I used this page a lot:
http://www.cplusplus.com/reference/cstring/ |
thanks you, yes that can be a way... I will study it.
EDIT
one solution like say dluu13, but i want find the way to search with "custom" header and not with char
Code: | int main ()
{
int i;
char str[] = "+CBC: 145frfrfrrfrhy";
char * pch;
int index=0;
char word[50];
pch=strrchr(str,' ');
index=pch-str+1;
printf ("Last occurence of ' ' found at %d \n",index);
i=0;
for(str[index];str[index+i]!=0;i++){ //
word[i]=str[index+i];
}
printf ("word: %s \n",word);
return 0;
} |
out
Quote: | Last occurence of ' ' found at 6
word: 145frfrfrrfrhyfrfrfrrfrhy |
tested in https://www.onlinegdb.com/online_c_compiler
EDIT 2
My code like function
Code: | char string[] = "+CBC: 456";
char start=' ';
char extract_string_since_char (char *str, char chr){
int i=0;
char temp[10];
char * pch;
int index=0;
char result[10];
strcpy(temp,str);
pch=strrchr(temp,chr);
index=pch-temp+1;
//printf ("Last occurence of ' ' found at %d \n",index);
for(;temp[index+i]!=0;i++)
{ //
result[i]=temp[index+i];
}
// printf ("word: %s \n",result);
return result;
}
void main() {
char word[10];
strcpy(word,extract_string_since_char(string,start));
printf("%s", word);
} |
out
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|