#include<stdio.h>
#include<string.h>
#include <stdlib.h>
char* myStrtok(char* str_arr,const char* delimiters,char**temp_str)
{
char*b_temp;
if(str_arr == NULL)
{
str_arr =*temp_str;
}
if(*str_arr =='\0')
{
return NULL;
}
b_temp = str_arr;
str_arr = strpbrk(str_arr, delimiters);
if(str_arr == NULL)
{
*temp_str = strchr(b_temp,'\0');
}
else
{
*str_arr ='\0';
*temp_str = str_arr +1;
}
return b_temp;
}
int splitestr(char str[], char res[11][20], char* separator)
{
int i=0;
char *token;
char*temp_str = NULL;
token = myStrtok((char *)str, separator,&temp_str);
while( token != NULL ) {
strcpy(res[i++],token);
token = myStrtok(NULL, separator,&temp_str);
}
return i;
}
int main(void)
{
char msg[]="063416.40,3143.2951,N,11713.0655,E,0.6,224.9,2,11,17.6,9.5,110620,07";
int BUFLEN=240;
char res[12][20];
splitestr(msg,res,",");
for(int i=0;i<11;i++)
{
printf("res[%d]:%s\r\n",i,res[i]);
}
char time[20]="20";
memcpy(time+2,res[11]+4,2);
memcpy(time+4,res[11]+2,2);
memcpy(time+6,res[11],2);
memcpy(time+8,res[0],7);
res[1][9]='\0';
res[3][10]='\0';
double lng=atof(res[3])/100;
double lat=atof(res[1])/100;
lng=(lng-(int)lng)*100/60+(int)lng;
lat=(lat-(int)lat)*100/60+(int)lat;
char pubmsg[255]="";
char groupid[20]="GPS";
memcpy(groupid+3,time,strlen(time));
sprintf(pubmsg,"{\"group\":\"%s\",\"longitude\":%.14lf,\"latitude\":%.14lf,\"altitude\":%.2lf,\"speed\":%.2lf,\"COG\":%.2lf,\"accuracy\":%.2lf,\"area\":\"\",\"time\":\"%s\"}"
,groupid,lng,lat,atof(res[6]),atof(res[9]),atof(res[8]),atof(res[5]),time);
printf("\r\n%d=====sendstr:%s\r\n",strlen(pubmsg), pubmsg);
return 0;
}