GPS定位器源码分享

时间:2024-04-05 17:28:53

GPS定位器源码分享GPS定位器源码分享(技术交流)


//********************************************************

//GPS包接收处理

//完整包为 "$GPRMC,054209.000,A,2232.9850,N,11404.9159,E,1.37,,080408,,,D*7F\0x0d\0x0a";

void GpsPacketParse(void)

{

    u32 ulTmp;

    unsigned char i, ucPos, tmp;

 

    //GPS初始为无信号

    GpsData.GpsSignalFlag = FALSE;

 

    //校验数据包是否正确

    GpsData.bGpsPacketRcvSuccess = FALSE;

    if (CheckGpsPacket(ucGpsDataBuffer) != CHECK_SUCCESS)

    {

    return;

    }

 

    //只需要对"GPRMC"包进行处理

    if (CompareStr(ucGpsDataBuffer, "GPRMC", 5) == TRUE)

    {

    ucPos = 13;

    while (ucGpsDataBuffer[ucPos++] != ',') //查找时间后面的逗号

    {

        if (ucPos > 250)

        {

        return;

        }

    }

 

    GetGpsTime();  //更新时间

    if (ucGpsDataBuffer[ucPos] != 'A')

    {

        return;

    }

 

    //获取时间

    //获取小时

    GpsData.UTC_Time[0] = ucGpsDataBuffer[6];

    GpsData.UTC_Time[1] = ucGpsDataBuffer[7];

        GpsData.uiTime = CharToValue(ucGpsDataBuffer[6]);

        GpsData.uiTime = GpsData.uiTime * 10 + CharToValue(ucGpsDataBuffer[7]);

    GpsData.uiTime *= 3600;  // 1 Hour = 3600 Second

    if (IsNumber(GpsData.UTC_Time, 2) == FALSE)

    {

        return;

    }

 

    //获取分钟

    GpsData.UTC_Time[2] = ':';

    GpsData.UTC_Time[3] = ucGpsDataBuffer[8];

    GpsData.UTC_Time[4] = ucGpsDataBuffer[9];

    GpsData.uiTime += CharToValue(ucGpsDataBuffer[8]) * 10 * 60;

    GpsData.uiTime += CharToValue(ucGpsDataBuffer[9]) * 60;

    if (IsNumber(GpsData.UTC_Time + 3, 2) == FALSE)

    {

        return;

    }

 

    //获取秒

    GpsData.UTC_Time[5] = ':';

    GpsData.UTC_Time[6] = ucGpsDataBuffer[10];

    GpsData.UTC_Time[7] = ucGpsDataBuffer[11];

    GpsData.uiTime += CharToValue(ucGpsDataBuffer[10]);

    GpsData.uiTime += CharToValue(ucGpsDataBuffer[11]);

    if (IsNumber(GpsData.UTC_Time + 6, 2) == FALSE)

    {

        return;

    }

 

    ucPos = 12;

    while (ucGpsDataBuffer[ucPos++] != ',') //查找时间后面的逗号

    {

        if (ucPos > 250)

        {

        return;

        }

    }

      while (ucGpsDataBuffer[ucPos++] != ',') //查找'A'后面的逗号

    {

        if (ucPos > 250)

        {

        return;

        }

    }

    //获取维度

    //保存未处理的纬度数据

    for (i = 0; i < 9; i++)

    {

           GpsData.InitLatitude[i] = ucGpsDataBuffer[ucPos + i];

    }

        GpsData.Latitude[0] = ucGpsDataBuffer[ucPos++];

    GpsData.Latitude[1] = ucGpsDataBuffer[ucPos++];

    if (IsNumber(GpsData.Latitude, 2) == FALSE)

    {

        return;

}

GpsData.Latitude[2] = '.';

        ulTmp = CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ucPos++;//'.'

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 5 / 3;

i = ulTmp / 100000;

        GpsData.Latitude[3] = ValueToChar(i);

        i = ulTmp % 100000 / 10000;

        GpsData.Latitude[4] = ValueToChar(i);

        i = ulTmp % 10000 / 1000;

        GpsData.Latitude[5] = ValueToChar(i);

i = ulTmp % 1000 / 100;

        GpsData.Latitude[6] = ValueToChar(i);

i = ulTmp % 100 / 10;

        GpsData.Latitude[7] = ValueToChar(i);

i = ulTmp % 10;

        GpsData.Latitude[8] = ValueToChar(i);

        if (IsNumber(GpsData.Latitude + 3, 6) == FALSE)

{

return;

}

    while (ucGpsDataBuffer[ucPos++] != ',')

{

    if (ucPos > 250)

{

    return;

}

}

//维度指示符,指示北维或南维

GpsData.LatitudeIndicator = ucGpsDataBuffer[ucPos++];

        ucPos++; //','

 

//获取经度

 

//保存未处理的经度数据

for (i = 0; i < 10; i++)

{

   GpsData.InitLongitude[i] = ucGpsDataBuffer[ucPos + i];

}

 

        GpsData.Longitude[0] = ucGpsDataBuffer[ucPos++];

GpsData.Longitude[1] = ucGpsDataBuffer[ucPos++];

GpsData.Longitude[2] = ucGpsDataBuffer[ucPos++];

        if (IsNumber(GpsData.Longitude, 3) == FALSE)

{

return;

}

GpsData.Longitude[3] = '.';

        ulTmp = CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ucPos++;//'.'

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

ulTmp = ulTmp * 5 / 3;

i = ulTmp / 100000;

        GpsData.Longitude[4] = ValueToChar(i);

        i = ulTmp % 100000 / 10000;

        GpsData.Longitude[5] = ValueToChar(i);

        i = ulTmp % 10000 / 1000;

        GpsData.Longitude[6] = ValueToChar(i);

i = ulTmp % 1000 / 100;

        GpsData.Longitude[7] = ValueToChar(i);

i = ulTmp % 100 / 10;

        GpsData.Longitude[8] = ValueToChar(i);

i = ulTmp % 10;

        GpsData.Longitude[9] = ValueToChar(i);

        if (IsNumber(GpsData.Longitude + 4, 3) == FALSE)

{

return;

}

    while (ucGpsDataBuffer[ucPos++] != ',')

{

    if (ucPos > 250)

{

    return;

}

}

//获取经度指示符

GpsData.LongitudeIndicator = ucGpsDataBuffer[ucPos++];

        ucPos++;//','

 

if (ucGpsDataBuffer[ucPos] != ',')

{

    //有速度

//获取速度

for (i = 0; i < 6; i ++)

{

     GpsData.InitSpeed[i] = '0';

}

GpsData.InitSpeed[3] = '.';

tmp = 0;

while (ucGpsDataBuffer[ucPos + tmp] != '.')

{

    tmp ++;

if (tmp > 3)

{

    break;

}

}

for (i = 0; i < tmp; i ++)

{

     GpsData.InitSpeed[3 - tmp + i] = ucGpsDataBuffer[ucPos + i];

}

GpsData.InitSpeed[4] = ucGpsDataBuffer[ucPos + tmp + 1];

 

ulTmp = CharToValue(ucGpsDataBuffer[ucPos++]);

while (ucGpsDataBuffer[ucPos] != '.')

{

     ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

}

ucPos++;//'.'

ulTmp = ulTmp * 10 + CharToValue(ucGpsDataBuffer[ucPos++]);

         GpsData.uiSpeed = ulTmp * 10;  //uiSpeed * 1.852 / 100 = km/h

         ulTmp = ulTmp * 1852;

ulTmp /= 1000;

GpsData.Speed[0] = ValueToChar(ulTmp / 1000);

         GpsData.Speed[1] = ValueToChar(ulTmp % 1000 / 100);

GpsData.Speed[2] = ValueToChar(ulTmp % 100 / 10);

if (IsNumber(GpsData.Speed, 3) == FALSE)

{

return;

}

GpsData.Speed[3] = '.';

GpsData.Speed[4] = ValueToChar(ulTmp % 10);

         if (IsNumber(GpsData.Speed + 4, 1) == FALSE)

{

return;

}

if ((DeviceInfo.CarAccWork == TRUE)

             || (GpsData.uiSpeed > 500))

{

     AddSumMeter((ulTmp + 18) / 36);

}

else

{

for (i = 0; i < 5; i ++)

{

     GpsData.Speed[i] = '0';

}

GpsData.Speed[3] = '.';

}

}

        while (ucGpsDataBuffer[ucPos] != ',')

{

    if (ucPos > 250)

{

    return;

}

ucPos ++;

}

ucPos ++; //','

 

    if (ucGpsDataBuffer[ucPos] != ',')

{

    //有方向数据

    for (i = 0; i < 6; i ++)

    {

        GpsData.Direction[i] = '0';

    }

 

i = 0;

while (ucGpsDataBuffer[ucPos] != ',')

{

    GpsData.Direction[i] = ucGpsDataBuffer[ucPos];

    i ++;

ucPos ++;

if (i >= 6)

    {

break;

    }

}

        }

        ucPos ++; //','

 

    //获取日期

        GpsData.UTC_Date[0] = ucGpsDataBuffer[ucPos++];

GpsData.UTC_Date[1] = ucGpsDataBuffer[ucPos++];

if (IsNumber(GpsData.UTC_Date, 2) == FALSE)

{

return;

}

GpsData.UTC_Date[2] = '/';

        GpsData.UTC_Date[3] = ucGpsDataBuffer[ucPos++];

GpsData.UTC_Date[4] = ucGpsDataBuffer[ucPos++];

if (IsNumber(GpsData.UTC_Date + 3, 2) == FALSE)

{

return;

}

GpsData.UTC_Date[5] = '/';

GpsData.UTC_Date[6] = ucGpsDataBuffer[ucPos++];

GpsData.UTC_Date[7] = ucGpsDataBuffer[ucPos++];

if (IsNumber(GpsData.UTC_Date + 6, 2) == FALSE)

{

return;

}

 

//收到一些正确包后把数据状态变新,

    if (GpsData.bDataState == GPS_OLD_DATA)

        {

            GpsData.GpsRcvPacketCnt++;

            if (GpsData.GpsRcvPacketCnt >= 8)

{

GpsData.bDataState = GPS_NEW_DATA;

GpsData.GpsRcvPacketCnt = 0;

}

        }

 

//GPS有信号

GpsData.GpsSignalFlag = TRUE;

GpsData.SendGpsPacketValid = TRUE;

SpeedAlertData.NewGpsData = TRUE;

GeoFenceData.NewGpsData = TRUE;

GpsData.bTimeZoneCalibrate = FALSE;

 

//把字符型经维度转换成值型经维度

GpsData.ulLatitude  = StrToValue(GpsData.Latitude, 2);

GpsData.ulLatitude  = GpsData.ulLatitude * 1000000UL + StrToValue(GpsData.Latitude + 3, 6);

 

GpsData.ulLongitude = StrToValue(GpsData.Longitude, 3);

GpsData.ulLongitude = GpsData.ulLongitude * 1000000UL + StrToValue(GpsData.Longitude + 4, 6);

}

}