TA的每日心情 | 奋斗 2022-9-16 05:52 |
---|
签到天数: 1368 天 连续签到: 1 天 [LV.10]以坛为家III
|
手上的模块不能闲着,能连接的都连接上。
一般的定位模块都是通过串口连接,输出NM0183格式的数据,只要在MCU中解析就可以了。
这款北斗&GPS模块也不例外,只比普通的GPS模块多输出个BD开头的数据而已,格式都是一样的。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct GPS
{
char AV; //'A|V'
char DT[13]; //"150622181500"
float Lon; //"117.3185"
float Lat; //"39.0940\0"
char EWNS[3]; //"E|W & N|S"
float Speed; //"xxx.xx\0" *1.852
float Dir; //"xxx.x\0"
float High; //"\0"
unsigned char Star; //"0~F"
unsigned char ANT; //'0'正常;'1'短路;'2'开路
} gpsData;
float LonLat(char* ll, int len)
{
//printf("%s - %d", ll, len);
char* s;
memcpy(s, ll, len);
//printf("%s", s);
printf("%f", atof(s));
return atof(s);
}
float Speed(char* speed, int len)
{
char* s = memmove(s, speed, len);
return atof(s);
}
void RMC(char* data)
{
char* s = NULL;
int len = 0;
//Time
char* p = strstr(data, ",");
int i = p - data + 1;
gpsData.DT[6] = data; i++;
gpsData.DT[7] = data; i++;
gpsData.DT[8] = data; i++;
gpsData.DT[9] = data; i++;
gpsData.DT[10] = data; i++;
gpsData.DT[11] = data; i++;
//AV
p = strstr(data + i, ",");
i = p - data + 1;
gpsData.AV = data; i++;
//Lat
p = strstr(data + i, ",");
i = p++ - data + 1;
s = strstr(p + 1, ",");
len = s - p;
gpsData.Lat = LonLat(p, len);
printf("%f", gpsData.Lat);
//NS
p = strstr(data + i, ",");
i = p++ - data + 1;
gpsData.EWNS[1] = data; i++;
//Lon
p = strstr(data + i, ",");
i = p++ - data + 1;
s = strstr(p + 1, ",");
len = s - p;
gpsData.Lon = LonLat(p, len);
//EW
p = strstr(data + i, ",");
i = p++ - data + 1;
gpsData.EWNS[0] = data; i++;
//Speed
p = strstr(data + i, ",");
i = p++ - data + 1;
s = strstr(p + 1, ",");
len = s - p;
gpsData.Speed = Speed(p, len);
//Dir
p = strstr(data + i, ",");
i = p++ - data + 1;
s = strstr(p + 1, ",");
len = s - p;
gpsData.Dir = atof(s);
//Date
p = strstr(data + i, ",");
i = p++ - data + 1;
memmove(&gpsData.DT[4], data + i, 2); i += 2;
memmove(&gpsData.DT[2], data + i, 2); i += 2;
memmove(&gpsData.DT[0], data + i, 2); i += 2;
printf("%f", gpsData.Lon);
}
void GGA(char* data)
{
}
void GSA(char* data)
{
}
void GSV(char* data)
{
}
void TXT(char* data)
{
}
void TD3020C(char* data)
{
switch (data[1])
{
case 'G': //GNRMC; GNGGA; GPGSA; GPGSV; GNTXT;
switch (data[2])
{
case 'N': //GNRMC; GNGGA; GNTXT;
switch (data[4])
{
case 'M': //GNRMC
RMC(data);
break;
case 'G': //GNGGA
GGA(data);
break;
case 'S': //GPGSA
GSA(data);
break;
}
break;
case 'P': //GPGSA; GPGSV;
switch (data[5])
{
case 'A': //GPGSA
GSA(data);
break;
case 'V': //GPGSV
GSV(data);
break;
}
break;
}
break;
case 'B': //BDGSA; BDGSV;
switch (data[5])
{
case 'A': //BDGSA
GSA(data);
break;
case 'V': //BDGSV
GSV(data);
break;
}
break;
}
}
int main(void)
{
TD3020C(GPSData);
}
解析的函数封装到一个文件里了,不过只需要调用一个TD3020C(GPSData);就够了。
GPSData就是串口获取到的定位模块的数据,解析后的结果保存在struct GPS里。
可以在程序的任何地方调用。 |
|