关于GNRMC解析

关于GNRMC解析
JT808格式

关键代码C语言

#include "stdafx.h"
#include "stdafx.h"
#include 
#include 
#include 

struct GNRMC_Info
{
unsigned int year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char Minute;
unsigned char second;


unsigned int longitude; 
unsigned char longitude_suffix; // E或W

unsigned int latitude; 
unsigned char latitude_suffix; 
unsigned char position_valid; // 有效

};
struct China_Time
{
	unsigned int year;
	unsigned char month;
	unsigned char day;
	unsigned char hour;
	unsigned char Minute;
	unsigned char second;
};
struct China_Time Ch_TM;

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';
            //printf("%s\n", buf);
            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++;
    }
    // 逗号个数是12个
    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时间:第1个逗号与第2个逗号之前 (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');

    // 定位状态:第2个逗号与第3个逗号之前
    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') + 2000;

    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;
}
	/*for( int i = 0; i < 4; i++ )//这部分为发送数组赋值,整形拆分为4个字节
	{
		sy[ i ] = (char)(GNRMC.longitude >> ( 24 - i*8 ));
	}*/
char buff[]={'$','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'};//Test
int main(int argc, char* argv[])
{
	struct GNRMC_Info GNRMC;

	char* p= "$GNRMC,132222.000,A,3103.8966,N,12118.3571,E,1.69,322.39,130219,,,A*76\r\n";
	GNRMC_Parse((unsigned char *)p,&GNRMC);
	UTCToChina(GNRMC.year,GNRMC.month,GNRMC.day,GNRMC.hour,GNRMC.Minute,GNRMC.second);
	printf("时间:%d-%d-%d %d:%d:%d\n",Ch_TM.year,Ch_TM.month,Ch_TM.day,Ch_TM.hour,Ch_TM.Minute,Ch_TM.second);
	printf("%u %c ---- %u %c 定位是否有效果:%d\n",GNRMC.longitude,GNRMC.longitude_suffix,GNRMC.latitude,GNRMC.latitude_suffix,GNRMC.position_valid);

	getchar();
	return 0;
}

关于初始化部分:
GPS_Info数组初始化ID,经纬度,和时间值。目前没有添加高度和速度。

unsigned char GPS_Info[49]={' '};
unsigned char  Mac_Id[6]={0x01,0x60,0x20,0x85,0x00,0x00};//16020850000
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];
	}
}

void Longti_Lati_Set()//这部分为收到GPS数据后格式化到GPS_Info数组
{
	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;
	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;
}

校验码指从消息头开始,同后一字节异或,直到校验码前一个字节,占用一个字节。
关于校验码函数,此函数无需添加到工程,只作为测试

unsigned char verification()//取得校验码
{
	unsigned char result=0x00;
	unsigned char spk[]={0x7E, 0x02 ,0x00, 0x00, 0x22, 0x54, 0x40, 0x71 ,0x46 ,0x06 ,0x70, 0x00, 0x3E, 00 ,00, 00, 00, 00, 00, 00, 02, 01, 0x58 ,
				0xE2, 0xB4, 0x06, 0xC9, 0xAD, 0xE7, 00, 0x20, 00, 00, 00, 0x5D, 0x18, 0x09, 0x27, 0x10, 0x35, 0x12, 0x01, 0x04, 00, 00, 00, 00, 0xBA, 0x7E};
	result=GPS_Info[1];//校验码
	for(int i=2;i<=46;i++)
	{
		result^=spk[i];
	}
	return result;
}

你可能感兴趣的:(关于GNRMC解析)