程序只针对MC20模块 STM32主控环境解析
gnrmc_analysis.c
#include "gnrmc_analysis.h"
#include
#include
#include
unsigned char GPS_Info[49];
unsigned char gps_buffer[72]={'$','G','N','R','M','C',',','1','3','2','2','2','2','.','0','0','0',',','A',',',
'3','1','0','3','.','8','9','6','6',',','N',',','1','2','1','1','8','.','3','5','7',
'1',',','E',',','1','.','6','9',',','3','2','2','.','3','9',',','1','3','0','2','1','9',
',',',',',','A','*','7','6','\r','\n'};
struct China_Time Ch_TM;
struct GNRMC_Info GNRMC;
void GPS_Info_Init()
{
for(int i=0;i<49;i++)
{
GPS_Info[i]=0x00;
}
GPS_Info[0]=GPS_Info[48]=0x7e;
GPS_Info[1]=0x02;
GPS_Info[4]=0x22;
for(int n=0;n<6;n++)
{
GPS_Info[n+5]=Mac_Id[n];
}
}
int comma_split(unsigned char *pstr, char *buf, int buflen, int s, int e)
{
int i, k;
int commaCount = 0;
int rc = -1;
i = k = 0;
if (e <= s)
{
return -1;
}
while (pstr[i] != '\0')
{
if (pstr[i] == ',')
{
commaCount++;
}
if (commaCount == s)
{
k = ++i;
break;
}
i++;
}
while (pstr[i] != '\0')
{
if (pstr[i] == ',')
{
commaCount++;
}
if (commaCount == e)
{
if (i - k + 1 > buflen)
{
return -1;
}
memset(buf, 0, sizeof(buf));
memcpy(buf, pstr + k, i - k);
rc = i - k;
buf[rc] = '\0';
break;
}
i++;
}
return rc;
}
int GNRMC_Parse(unsigned char *pstr, struct GNRMC_Info *pGNRMCInfo)
{
int len = 0;
int commaCount = 0;
double temp = 0;
char buf[32] = {0};
while (pstr[len] != '\0')
{
if (pstr[len] == ',')
{
commaCount++;
}
len++;
}
if (commaCount != 12)
{
return -1;
}
// ?:$GNRMC
if (!((pstr[0] == '$') && (pstr[1] == 'G') && (pstr[2] == 'N') && (pstr[3] == 'R') && (pstr[4] == 'M') && (pstr[5] == 'C')))
{
return -1;
}
// ?:\r\n
if ((pstr[len - 2] != '\r') && (pstr[len - 1] != '\n'))
{
return -1;
}
// UTC (eg:092846.400 hhmmss.sss )
len = comma_split(pstr, buf, sizeof(buf), 1, 2);
if (len < 0)
{
return -1;
}
pGNRMCInfo->hour = (buf[0] - '0') * 10 + (buf[1] - '0');
pGNRMCInfo->Minute = (buf[2] - '0') * 10 + (buf[3] - '0');
pGNRMCInfo->second = (buf[4] - '0') * 10 + (buf[5] - '0');
len = comma_split(pstr, buf, sizeof(buf), 2, 3);
if (len != 1)
{
return -1;
}
pGNRMCInfo->position_valid = ((buf[0] == 'A') ? 1 : 0);
// ??
len = comma_split(pstr, buf, sizeof(buf), 3, 4);
if (len < 0)
{
return -1;
}
temp = atof(buf);
temp = temp * 10000;
pGNRMCInfo->longitude = temp;
// E?W
len = comma_split(pstr, buf, sizeof(buf), 4, 5);
if (len != 1)
{
return -1;
}
pGNRMCInfo->longitude_suffix = buf[0];
len = comma_split(pstr, buf, sizeof(buf), 5, 6);
if (len < 0)
{
return -1;
}
temp = atof(buf);
temp = temp * 10000;
pGNRMCInfo->latitude = temp;
// ????N(???)?S(???)
len = comma_split(pstr, buf, sizeof(buf), 6, 7);
if (len != 1)
{
return -1;
}
pGNRMCInfo->latitude_suffix = buf[0];
// UTC??:ddmmyy(???)?? eg:070417
len = comma_split(pstr, buf, sizeof(buf), 9, 10);
if (len < 0)
{
return -1;
}
pGNRMCInfo->day = (buf[0] - '0') * 10 + (buf[1] - '0');
pGNRMCInfo->month = (buf[2] - '0') * 10 + (buf[3] - '0');
pGNRMCInfo->year = (buf[4] - '0') * 10 + (buf[5] - '0') ;
return 0;
}
void UTCToChina(unsigned int UTCYear,unsigned char UTCMonth,unsigned char UTCDay,unsigned char UTCHour,unsigned char UTCMinutes,unsigned char UTCSeconds)
{
unsigned int ChinaYear = UTCYear;
unsigned char ChinaMonth = UTCMonth;
unsigned char ChinaDay = UTCDay;
unsigned char ChinaSeconds = UTCSeconds;
unsigned char ChinaMinutes = UTCMinutes;
unsigned char ChinaHour = UTCHour + 8;
if (ChinaHour > 23)
{
ChinaHour -= 24;
ChinaDay++;
switch (ChinaMonth)
{
case 1:
if (ChinaDay > 31)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 2:
if ((0 == ChinaYear % 4 && 0 != ChinaYear % 100) || 0 == ChinaYear % 400)
{
if (ChinaDay > 29)
{
ChinaDay = 1;
ChinaMonth++;
}
}
else
{
if (ChinaDay > 28)
{
ChinaDay = 1;
ChinaMonth++;
}
}
break;
case 3:
if (ChinaDay > 31)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 4:
if (ChinaDay > 30)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 5:
if (ChinaDay > 31)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 6:
if (ChinaDay > 30)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 7:
if (ChinaDay > 31)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 8:
if (ChinaDay > 31)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 9:
if (ChinaDay > 30)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 10:
if (ChinaDay > 31)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 11:
if (ChinaDay > 30)
{
ChinaDay = 1;
ChinaMonth++;
}
break;
case 12:
if (ChinaDay > 31)
{
ChinaDay = 1;
ChinaMonth = 1;
ChinaYear++;
}
break;
default:
break;
}
}
Ch_TM.year=ChinaYear;
Ch_TM.month=ChinaMonth;
Ch_TM.day=ChinaDay;
Ch_TM.hour=ChinaHour;
Ch_TM.Minute=ChinaMinutes;
Ch_TM.second=ChinaSeconds;
}
void Longti_Lati_Set(unsigned char *pstr)
{
unsigned char result=0x00;
unsigned char bcd_year=0x00;
unsigned char bcd_month=0x00;
unsigned char bcd_day=0x00;
unsigned char bcd_hour=0x00;
unsigned char bcd_Minute=0x00;
unsigned char bcd_second=0x00;
GNRMC_Parse((unsigned char *)pstr,&GNRMC);
UTCToChina(GNRMC.year,GNRMC.month,GNRMC.day,GNRMC.hour,GNRMC.Minute,GNRMC.second);
for(int n=0;n<4;n++)
{
GPS_Info[n+21]=(char)(GNRMC.longitude >> ( 24 - n*8 ));
GPS_Info[n+25]=(char)(GNRMC.latitude >> ( 24 - n*8 ));
}
bcd_year=(Ch_TM.year/10*16)+(Ch_TM.year%10);
bcd_month=(Ch_TM.month/10*16)+(Ch_TM.month%10);
bcd_day=(Ch_TM.day/10*16)+(Ch_TM.day%10);
bcd_hour=(Ch_TM.hour/10*16)+(Ch_TM.hour%10);
bcd_Minute=(Ch_TM.Minute/10*16)+(Ch_TM.Minute%10);
bcd_second=(Ch_TM.second/10*16)+(Ch_TM.second%10);
GPS_Info[35]=bcd_year;
GPS_Info[36]=bcd_month;
GPS_Info[37]=bcd_day;
GPS_Info[38]=bcd_hour;
GPS_Info[39]=bcd_Minute;
GPS_Info[40]=bcd_second;
result=GPS_Info[1];//???
for(int i=2;i<=46;i++)
{
result^=GPS_Info[i];
}
GPS_Info[47]=result;
}
gnrmc_analysis.h
#ifndef _GNRMC_ANALYSIS_H_
#define _GNRMC_ANALYSIS_H_
struct GNRMC_Info
{
unsigned char year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char Minute;
unsigned char second;
unsigned int longitude;
unsigned char longitude_suffix;
unsigned int latitude;
unsigned char latitude_suffix;
unsigned char position_valid;
};
struct China_Time
{
unsigned char year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char Minute;
unsigned char second;
};
extern struct China_Time Ch_TM;
extern unsigned char Mac_Id[6];
extern unsigned char GPS_Info[49];
extern unsigned char gps_buffer[72];
void GPS_Info_Init(void);
int comma_split(unsigned char *pstr, char *buf, int buflen, int s, int e);
int GNRMC_Parse(unsigned char *pstr, struct GNRMC_Info *pGNRMCInfo);
void UTCToChina(unsigned int UTCYear,unsigned char UTCMonth,unsigned char UTCDay,unsigned char UTCHour,unsigned char UTCMinutes,unsigned char UTCSeconds);
void Longti_Lati_Set(unsigned char *pstr);
#endif
主函数
/**************************************************************************************************
Copyright (C), 2014-2017
File name: main.c
**************************************************************************************************/
#include "stm32f30x.h"
#include
#include
#include
#include
#include "gnrmc_analysis.h"
unsigned char Mac_Id[6]={0x01,0x60,0x20,0x85,0x00,0x00};//16020850000
int main(void)
{
GPS_Info_Init();
...//各种初始化
}
部分函数更新记录:初始化三个发送的数据
void verify_code(unsigned char * rp,int length)
{
unsigned char result=0x00;
result=rp[1];
for(int i=2;i
定期心跳包和定期GPS上传数据程序更新:
else if(System_Status==Net_Conneted)
{
/*switch(Net_Step)
{
case 1:
if(register_serial_num++>65500)register_serial_num=0;//unsigned short
Register_Info[11]=(register_serial_num & 0xFF00)>>8;
Register_Info[12]=register_serial_num & 0x00FF;
verify_code(Register_Info,72);
Uart2_SendString("AT+QISEND=72\r\n");
delay_ms(500);
Uart2_SendLength(Register_Info,72);
Uart2_SendData(0x1a);
Clear_Buffer();
Net_Step++;
break;
case 2:
if(heart_cycle_count>=120)
{
heart_cycle_count=0;
if(heart_serial_num++>65500)heart_serial_num=0;//unsigned short
Heart_Info[11]=(register_serial_num & 0xFF00)>>8;
Heart_Info[12]=register_serial_num & 0x00FF;
verify_code(Heart_Info,29);
Uart2_SendString("AT+QISEND=29\r\n");
delay_ms(500);
Uart2_SendLength(Heart_Info,29);
Uart2_SendData(0x1a);
}
if(gps_cycle_count==5)
{
gps_cycle_count++;
Uart2_SendString("AT+QGNSSRD=\"NMEA/RMC\"\r\n");
}
if(gps_cycle_count>10)
{
gps_cycle_count=0;
if(gps_buffer[18]=='A')
{
if(gps_serial_num++>65500)gps_serial_num=0;//unsigned short
GPS_Info[11]=(gps_serial_num & 0xFF00)>>8;
GPS_Info[12]=gps_serial_num & 0x00FF;
Longti_Lati_Set(gps_buffer);
Uart2_SendString("AT+QISEND=49\r\n");
delay_ms(500);
Uart2_SendLength(GPS_Info,49);
Uart2_SendData(0x1a);
gps_buffer[18]=' ';
}
}
break;
case 3:
break;
case 4:
break;
default:
break;
}*/