模块简介

我们今天主要使用这个模块来获取经纬度 ,使用串口通讯
主要解决
1.串口通讯的不定时长接收
2.数据包的解析
C语言程序测试
首先用串口接收
这个是使用串口助手接收到的数值

交给豆包分析
可以看出,豆包只需要根据
cpp
$GNRMC,093039.000,A,2503.87572,N,11017.71239,E,0.00,17.26,221125,,,A,V*3C
这一包就可以计算出经纬度,所以我们需要只需要获取这一包然后解析这一包

DevC++测试
cpp
#include <stdio.h>
#include <string.h>
typedef int uint8_t;
static char data[2][30];
static float jing, wei;
static uint8_t lentgth;
char ss[100] = "$GNRMC,093040.000,A,2503.87566,N,11017.71241,E,0.00,17.26,221125,,,A,V*38";
//$GNRMC,093040.000,A,2503.87566,N,11017.71241,E,0.00,17.26,221125,,,A,V*38
void change_buff()
{
char *p = ss;
uint8_t i = 0;
uint8_t j = 0;
while (i < lentgth)
{
if (*(p + i) == '$')
{
if (*(p + i + 1) == 'G' && *(p + i + 2) == 'N' && *(p + i + 3) == 'R' && *(p + i + 4) == 'M' && *(p + i + 5) == 'C')
{
j = 0;
while (j != 3)
{
i++;
if (*(p + i) == ',')
{
j++;
}
}
i++;
j = 0;
while (*(p + i) != ',')
{
data[0][j++] = *(p + i++);
}
data[0][j] = '\0';
while (*(p + i) != ',')
{
i++;
}
i += 3;
j = 0;
while (*(p + i) != ',')
{
data[1][j++] = *(p + i++);
}
data[1][j] = '\0';
break;
}
}
i++;
}
}
void handle_data(char *da, float *hh)
{
uint8_t i = 0, j = 0, k, zz = 0, xz = 0, yz = 0;
float x = 0;
while (da[i] != '\0')
{
if (da[i] == '.')
{
j = i;
break;
}
i++;
}
xz = (da[j - 2] - '0') * 10 + (da[j - 1] - '0');
k = j - 2;
i = 0;
while (i < k)
{
zz *= 10;
zz += (da[i++] - '0');
}
i = j + 1;
k = 3;
while (k--)
{
yz *= 10;
yz += (da[i++] - '0');
}
x = ((float)xz + (float)yz / 1000) / 60.0;
*hh = (float)zz + x;
}
int main()
{
lentgth = strlen(ss);
change_buff();
handle_data(data[0], &jing);
handle_data(data[1], &wei);
printf("精度=%.3f,纬度=%.3f\n", jing, wei);
return 0;
}
测试结果如下,成功解算出来

所以根据上述原理放入程序中即可成功解算
源代码
gps.c
cpp
#include "gps.h"
#include "usart.h"
#include "tim.h"
#include "stdio.h"
static char gps_buffer[100], gps_data[2][50];
static uint8_t gps_buff[1], length = 0, time = 0, wflagh = 0, debug = 0;
static float longitude, latitude;
void GPS_Init(void)
{
HAL_UART_Receive_IT(&gps_uart, (uint8_t *)gps_buff, 1);
HAL_TIM_Base_Start_IT(&gps_tim);
}
void GPS_Data(float *data1, float *data2)
{
*data1 = longitude;
*data2 = latitude;
}
static void handle_data(char *da, float *hh)
{
uint8_t i = 0, j = 0, k, zz = 0, xz = 0, yz = 0;
float x = 0;
while (da[i] != '\0')
{
if (da[i] == '.')
{
j = i;
break;
}
i++;
}
if (debug)
printf("come6\r\n");
xz = (da[j - 2] - '0') * 10 + (da[j - 1] - '0');
k = j - 2;
i = 0;
while (i < k)
{
zz *= 10;
zz += (da[i++] - '0');
}
i = j + 1;
k = 3;
if (debug)
printf("come7\r\n");
while (k--)
{
yz *= 10;
yz += (da[i++] - '0');
}
if (debug)
printf("come8\r\n");
x = ((float)xz + (float)yz / 1000) / 60.0;
*hh = (float)zz + x;
if (debug)
printf("hh=%f\r\n", *hh);
}
static void change_buff()
{
char *p = gps_buffer;
uint8_t i = 0;
uint8_t j = 0;
while (i < length)
{
if (*(p + i) == '$')
{
if (*(p + i + 1) == 'G' && *(p + i + 2) == 'N' && *(p + i + 3) == 'R' && *(p + i + 4) == 'M' && *(p + i + 5) == 'C')
{
j = 0;
if (debug)
printf("come1\r\n");
while (j != 3)
{
i++;
if (*(p + i) == ',')
{
j++;
}
}
if (debug)
printf("come2\r\n");
i++;
j = 0;
while (*(p + i) != ',')
{
gps_data[0][j++] = *(p + i++);
}
gps_data[0][j] = '\0';
if (debug)
printf("come3\r\n");
while (*(p + i) != ',')
{
i++;
}
i += 3;
j = 0;
if (debug)
printf("come4\r\n");
while (*(p + i) != ',')
{
gps_data[1][j++] = *(p + i++);
}
if (debug)
printf("come5\r\n");
gps_data[1][j] = '\0';
handle_data(gps_data[0], &longitude);
handle_data(gps_data[1], &latitude);
return;
}
else
{
return;
}
}
i++;
}
}
void gps_uart_handle(void)
{
if (wflagh == 0)
{
gps_buffer[length++] = gps_buff[0];
time = 0;
}
HAL_UART_Receive_IT(&gps_uart, (uint8_t *)gps_buff, 1);
}
void gps_tim_handle(void)
{
if (wflagh == 0)
{
time++;
if ((time >= 5) && (length > 0))
{
wflagh = 1;
}
}
}
void gps_while()
{
if (wflagh == 1)
{
gps_buffer[length] = '\0';
if (debug)
printf("%s\r\n", gps_buffer);
change_buff();
length = 0;
time = 0;
wflagh = 0;
}
}
gps.h
cpp
#ifndef __GPS_H
#define __GPS_H
#include "main.h"
#define gps_uart huart1
#define gps_tim htim2
#define gps_buffer_size 255
void GPS_Init(void);
void GPS_Data(float *data1, float *data2);
void gps_uart_handle(void);
void gps_tim_handle(void);
void gps_while(void);
#endif
结果验证
通过放在户外连接
最后也是成功实现功能
