int ParseGPSData(char *szGPSData, TGPSPosition *pGPSPosition) { // NMEA 0183格式:$GPRMC,204700,A,3403.868,N,11709.432,W,001.9,336.9,170698,013.6,E*6E char *pBegin; char *pTemp; char *pTime; char szTime[3]; char *pbAvail; char *pLatitude; char *pBearing; char *pbEasting; char *pDate; char *pSpeed; char szDate[3]; char *pLongitude; char *pbNorthing; int Y; int X; TGPSPosition *pGPSPosition;
memset(pGPSPosition, 0, sizeof(TGPSPosition)); pBegin = strstr(szGPSData, "$"); if (pBegin == NULL) { return -1; } pTemp = strstr(pBegin+3, "RMC"); if (pTemp != (pBegin+3)) { return -1; } //时间 pBegin = strchr(pBegin, ','); if (pBegin == NULL) { return -2; } pTime = pBegin + 1; memset(szTime, 0, sizeof(szTime)); szTime[0] = pTime[0]; szTime[1] = pTime[1]; pGPSPosition->wFixHour = atoi(szTime); memset(szTime, 0, sizeof(szTime)); szTime[0] = pTime[2]; szTime[1] = pTime[3]; pGPSPosition->wFixMinute = atoi(szTime); memset(szTime, 0, sizeof(szTime)); szTime[0] = pTime[4]; szTime[1] = pTime[5]; pGPSPosition->wFixSecond = atoi(szTime); //是否否有效的GPS pBegin = strchr(pTime, ','); if (pBegin == NULL) { return -2; } pbAvail = pBegin+1; if (*pbAvail != 'A') { return -3; } //纬度 pBegin = strchr(pbAvail, ','); if (pBegin == NULL) { return -5; } pLatitude = pBegin + 1; pGPSPosition->dbLatitude = atof(pLatitude)/100; Y = (int)pGPSPosition->dbLatitude; pGPSPosition->dbLatitude = pGPSPosition->dbLatitude - Y; pGPSPosition->dbLatitude = pGPSPosition->dbLatitude*100/60 + Y; /* if (thePrefile.m_dbMaxY < pGPSPosition->dbLatitude || thePrefile.m_dbMinY > pGPSPosition->dbLatitude) { return - 5; } */ //是否北半球 pBegin = strchr(pLatitude, ','); if (pBegin == NULL) { return -2; } pbNorthing = pBegin+1; if (*pbNorthing == 'N') { pGPSPosition->bNorthing = XOS_TRUE; } else if (*pbNorthing == 'S') { pGPSPosition->bNorthing = XOS_FALSE; } else { return -4; } //经度 pBegin = strchr(pbNorthing, ','); if (pBegin == NULL) { return -5; } pLongitude = pBegin + 1; pGPSPosition->dbLongitude = atof(pLongitude)/100; X = (int)pGPSPosition->dbLongitude; pGPSPosition->dbLongitude = pGPSPosition->dbLongitude - X; pGPSPosition->dbLongitude = pGPSPosition->dbLongitude*100/60 + X; /* if (thePrefile.m_dbMaxX < pGPSPosition->dbLongitude || thePrefile.m_dbMinX > pGPSPosition->dbLongitude) { return - 5; } */ //是否东半球 pBegin = strchr(pLongitude, ','); if (pBegin == NULL) { return -6; } pbEasting = pBegin+1; if (*pbEasting == 'E') { pGPSPosition->bEasting = XOS_TRUE; } else if (*pbEasting == 'W') { pGPSPosition->bEasting = XOS_FALSE; } else { return -7; } //速度 pBegin = strchr(pbEasting, ','); if (pBegin == NULL) { return -8; } pSpeed = pBegin + 1; pGPSPosition->dbSpeed = atof(pSpeed)*1.852; if (pGPSPosition->dbSpeed > 150) { return -9; } //方向 pBegin = strchr(pSpeed, ','); if (pBegin == NULL) { return -10; } pBearing = pBegin + 1; pGPSPosition->dbBearing = atof(pBearing); //日期 pBegin = strchr(pBearing, ','); if (pBegin == NULL) { return -11; } pDate = pBegin + 1; memset(szDate, 0, sizeof(szDate)); szDate[0] = pDate[0]; szDate[1] = pDate[1]; pGPSPosition->wFixDay = atoi(szDate); memset(szDate, 0, sizeof(szDate)); szDate[0] = pDate[2]; szDate[1] = pDate[3]; pGPSPosition->wFixMonth = atoi(szTime); memset(szDate, 0, sizeof(szDate)); szDate[0] = pDate[4]; szDate[1] = pDate[5]; pGPSPosition->wFixYear = atoi(szDate); if ((pGPSPosition->dbLatitude == 0) && (pGPSPosition->dbLongitude == 0)) { return -12; } return 0; }