I am using esp8266, i need to take first work isnide quotes after the word "+CWLAP:"
Here is my output result.
AT+CWLAP
+CWLAP:(3,"MainNetwork",-76,"00:15:6d:a6:61:fa",1,107,0,5,3,3,0)
+CWLAP:(1,"SubNetwork",-79,"00:4f:62:19:19:43",9,85,0,0,0,3,0)
OK
I need to print MainNetwork and subNetwork. Here is what i am trying to do
char *p =strtok(Receive.bytes, "+CWLAP:");
p = strtok(NULL, "\"");
p = strtok(NULL, "\"");
printf("%s\r\n",p);
__delay_ms(20);
p = strtok(NULL, "+CWLAP:");
p = strtok(NULL, "\"");
p = strtok(NULL, "\"");
printf("%s\r\n",p);
__delay_ms(20);
For some reson i am taking this in my output
MainNetwork
,1,107,0,5,3,3,0)
+CWLAP:(1,
replace
char *p =strtok(Receive.bytes, "+CWLAP:");
p = strtok(NULL, "\"");
by
char *p =strstr(Receive.bytes, "+CWLAP:");
p = strtok(p, "\"");
because in your first strtok "+CWLAP:" is a string of separators, so strtok will give you AT placed before the '+'
is similar for the second but strtok add null characters so the second strstr must be done before any strtok .
A full solution can be :
char * p1 = strstr(Receive.bytes, "+CWLAP:");
char * p3 = strstr(p1 + 7, "+CWLAP:");
char * p2;
p2 = strtok(p1, "\"");
p2 = strtok(NULL, "\"");
printf("%s\r\n",p2);
p2 = strtok(p3, "\"");
p2 = strtok(NULL, "\"");
printf("%s\r\n",p2);
Additional remark, strtok modify Receive.bytes adding null characters, may be you need first to duplicate the string ( strdup ) ?
Execution :
MainNetwork
SubNetwork
And to manage any number of lines :
char * p1 = strstr(Receive.bytes, "+CWLAP:");
while (p1 != NULL) {
p1 += 7; /* bypass +CWLAP: */
char * p2 = strstr(p1, "+CWLAP:");
char * s = strtok(strchr(p1, '"') + 1, "\"");
printf("%s\r\n", s);
p1 = p2;
}
I replaced the first strtok by a strchr because we don't need the intermediate string
Since strtok
modifies the source array, strcspn
can be used to iterate through the array along with strstr
.
The search can be wrapped in a loop to find all the matches.
strstr
will find an exact match of the sub-array in the source array.
From the match, strcspn
will count the characters to next character in the source that matches any character in the delimiter string. Here only a quote is used as a delimiter. If strcspn
does not find a match it counts to the terminating zero.
This prints the matching string but strncpy
could be use to copy the string. Pay attention to the terminating zero.
#include <stdio.h>
#include <string.h>
int main()
{
char bytes[] = "AT+CWLAP\n+CWLAP:(3,\"MainNetwork\",-76,\"00:15:6d:a6:61:fa\",1,107,0,5,3,3,0)\n+CWLAP:(1,\"SubNetwork\",-79,\"00:4f:62:19:19:43\",9,85,0,0,0,3,0)";
char *quote = "\"";
char *cwlap = "+CWLAP:";
char *match = NULL;
char *openquote = NULL;
char *closequote = NULL;
size_t skip = 0;
size_t span = 0;
closequote = bytes;
while ( closequote && *closequote) {//not NULL and not zero terminator
if ( ( match = strstr ( closequote, cwlap))) {//find exact sub-string match
skip = strcspn ( match, quote);//count characters to quote
openquote = match + skip;//point to quote
if ( *openquote) {//not at terminating zero
openquote++;//point to character after quote
span = strcspn ( openquote, quote);//count characters to quote
closequote = openquote + span;//point to quote
if ( *closequote) {//not at terminating zero
printf ( "%.*s\n", (int)span, openquote);//print span number of characters
closequote++;//point to character after quote
}
}
else {
closequote = NULL;
}
}
else {
closequote = NULL;
}
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.