/*********************************************************************************
* Copyright: (C) 2017 zoulei
* All rights reserved.
*
* Filename: gps_analyse.c
* Description: This file
*
* Version: 1.0.0(2017年06月19日)
* Author: zoulei <zoulei121@gmail.com>
* ChangeLog: 1, Release initial version on "2017年06月19日 12时16分39秒"
*
********************************************************************************/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "gps.h"
typedef unsigned int UINT;
typedef int BYTE;
typedef struct __gprmc__
{
UINT time;/* gps定位时间 */
char pos_state;/*gps状态位*/
float latitude;/*纬度 */
float longitude;/* 经度 */
float speed; /* 速度 */
float direction;/*航向 */
UINT date; /*日期 */
float declination; /* 磁偏角 */
char dd;
char mode;/* GPS模式位 */
}GPRMC;
int gps_analyse (char *buff,GPRMC *gps_data)
{
char *ptr=NULL;
if(gps_data==NULL)
{
return -1;
}
if(strlen(buff)<10)
{
return -1;
}
/* 如果buff字符串中包含字符"$GPRMC"则将$GPRMC的地址赋值给ptr */
/* sscanf函数为从字符串输入,意思是将ptr内存单元的值作为输入分别输入到后面的结构体成员 */
if(NULL != (ptr=strstr(buff,"$GPRMC")))
sscanf(ptr,"$GPRMC,%d.000,%c,%f,N,%f,E,%f,%f,%d,,,%c*",&(gps_data->time),&(gps_data->pos_state),&(gps_data->latitude),&(gps_data->longitude),&(gps_data->speed),&(gps_data->direction),&(gps_data->date),&(gps_data->mode));
else if(NULL != (ptr=strstr(buff,"$GNRMC")))
sscanf(ptr,"$GNRMC,%d.000,%c,%f,N,%f,E,%f,%f,%d,,,%c*",&(gps_data->time),&(gps_data->pos_state),&(gps_data->latitude),&(gps_data->longitude),&(gps_data->speed),&(gps_data->direction),&(gps_data->date),&(gps_data->mode));
return 0;
}
//
// Created by sdufe on 2022-10-14.
//
/* find "$GPRMC" from raw_buf */
#include <string.h>
#include <stdio.h>
int main(void)
{
char *raw_buf="$GPRMC,121252.000,A,3958.3032,N,11629.*6,E,15.15,359.95,070306,,,A*54";
char* wellhandled_string;
if((wellhandled_string = strstr(raw_buf,"$GPRMC")) != NULL)
{
for (int i=0; i<strlen(wellhandled_string); i++)
{
if (wellhandled_string[i] == '/n')
{
wellhandled_string[i] = '/0'; //replace ‘/n’ with null
}
}
}
printf("%s",wellhandled_string);
return 0;
}