public static IGPSMessage Parse(string sentence) { IGPSMessage message = null; int checksumIndex = sentence.IndexOf("*"); if (checksumIndex == -1) { throw new Exception("No checksum found"); } if (checksumIndex == sentence.Length - 1) { throw new Exception("No checksum after marker"); } string checksum = sentence.Substring(checksumIndex + 1, sentence.Length - checksumIndex - 1); sentence = sentence.Substring(0, checksumIndex); string[] messageFields = sentence.Split(new char[] { ',' }); if (messageFields == null || messageFields.Length == 0) { throw new Exception("No fields in the message"); } switch (messageFields[0]) { case "$GPGGA": { FixedDataMessage fdMessage = new FixedDataMessage(); fdMessage.Timestamp = ParseTime(messageFields[1]); fdMessage.Latitude = messageFields[2]; fdMessage.NSIndicator = ParseChar(messageFields[3]); fdMessage.Longitude = messageFields[4]; fdMessage.EWIndicator = ParseChar(messageFields[5]); fdMessage.PositionFix = ParsePositionFixIndicator(messageFields[6]); fdMessage.SatallitesUsed = messageFields[7]; ParseDouble(messageFields[8], out fdMessage.HDOP, out fdMessage.IsHDOPValid); ParseDouble(messageFields[9], out fdMessage.MSLAltitude, out fdMessage.IsMSLAltitudeValid); fdMessage.MSLAltitudeUnits = ParseChar(messageFields[10], out fdMessage.IsMSLAltitudeUnitsValid); ParseDouble(messageFields[11], out fdMessage.GeoidSeperation, out fdMessage.IsGeoidSeperationValid); fdMessage.GeoidSeperationUnits = ParseChar(messageFields[12], out fdMessage.IsGeoidSeperationUnitsValid); ParseInt(messageFields[13], out fdMessage.AgeOfDifferentialCorrection, out fdMessage.IsAgeOfDifferentialCorrectionValid); fdMessage.DifferentialReferenceStationId = messageFields[14]; message = fdMessage; break; } } return(message); }
private GPSPosition ParsePosition(FixedDataMessage message) { GPSPosition position = new GPSPosition(); position.HasFix = (message.PositionFix != PositionFixIndicator.Invalid); position.IsEstimate = (message.PositionFix == PositionFixIndicator.Estimated); if (position.HasFix) { position.Latitude = Convert.ToDouble(message.Latitude.Substring(0, 2)) + (Convert.ToDouble(message.Latitude.Substring(2, 7)) / 60); if (message.NSIndicator == 'S') position.Latitude = -position.Latitude; position.Longitude = Convert.ToDouble(message.Longitude.Substring(0, 3)) + (Convert.ToDouble(message.Longitude.Substring(3, 7)) / 60); if (message.EWIndicator == 'W') position.Longitude = -position.Longitude; } return position; }
public static IGPSMessage Parse(string sentence) { IGPSMessage message = null; int checksumIndex = sentence.IndexOf("*"); if (checksumIndex == -1) { throw new Exception("No checksum found"); } if (checksumIndex == sentence.Length - 1) { throw new Exception("No checksum after marker"); } string checksum = sentence.Substring(checksumIndex + 1, sentence.Length - checksumIndex - 1); sentence = sentence.Substring(0, checksumIndex); string[] messageFields = sentence.Split(new char[] { ',' }); if (messageFields == null || messageFields.Length == 0) { throw new Exception("No fields in the message"); } switch (messageFields[0]) { case "$GPGGA": { FixedDataMessage fdMessage = new FixedDataMessage(); fdMessage.Timestamp = ParseTime(messageFields[1]); fdMessage.Latitude = messageFields[2]; fdMessage.NSIndicator = ParseChar(messageFields[3]); fdMessage.Longitude = messageFields[4]; fdMessage.EWIndicator = ParseChar(messageFields[5]); fdMessage.PositionFix = ParsePositionFixIndicator(messageFields[6]); fdMessage.SatallitesUsed = messageFields[7]; ParseDouble(messageFields[8], out fdMessage.HDOP, out fdMessage.IsHDOPValid); ParseDouble(messageFields[9], out fdMessage.MSLAltitude, out fdMessage.IsMSLAltitudeValid); fdMessage.MSLAltitudeUnits = ParseChar(messageFields[10], out fdMessage.IsMSLAltitudeUnitsValid); ParseDouble(messageFields[11], out fdMessage.GeoidSeperation, out fdMessage.IsGeoidSeperationValid); fdMessage.GeoidSeperationUnits = ParseChar(messageFields[12], out fdMessage.IsGeoidSeperationUnitsValid); ParseInt(messageFields[13], out fdMessage.AgeOfDifferentialCorrection, out fdMessage.IsAgeOfDifferentialCorrectionValid); fdMessage.DifferentialReferenceStationId = messageFields[14]; message = fdMessage; break; } } return message; }