GPS源碼共享
GPS定制開發、無線通訊方案請聯系作者。手機/微信:13794466265
//********************************************************
//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);
}
}
GPS源碼共享