//======================================================================= #region -= static methods =- /// <summary> /// must be in the format 239.02,E /// </summary> /// <param name="inputString"></param> /// <returns></returns> public static MagneticVariation Parse(string inputString) { //---- declare vars MagneticVariation magVar = new MagneticVariation(); string[] halves = inputString.Split(','); if (halves.Length < 2) { throw new FormatException("Input string must be in the format of 33.02,E"); } if (string.IsNullOrEmpty(halves[0])) { magVar.Degrees = 0.0M; } else { magVar.Degrees = decimal.Parse(halves[0]); } magVar.Direction = DirectionUtil.Parse(halves[1]); //---- return return(magVar); }
/// <summary> /// must be in the format 239.02,E /// </summary> /// <param name="inputString"></param> /// <returns></returns> public static bool TryParse(string inputString, out MagneticVariation magneticVariation) { try { magneticVariation = Parse(inputString); return(true); } catch (Exception) { magneticVariation = null; return(false); } }
//======================================================================= //======================================================================= #region -= constructors =- #endregion //======================================================================= //======================================================================= #region -= public methods =- #endregion //======================================================================= //======================================================================= #region -= static methods =- public static RmcData Parse(string inputString) { //---- declare vars RmcData rmcData = new RmcData(); string dataString = inputString.Substring(0, inputString.IndexOf('*')); // strip off the checksum string[] values = dataString.Split(','); //---- if we don't have 12 (header + 11), it's no good if (values.Length < 12) { throw new FormatException(); } //---- Status if (values[2].ToUpper() == "A") { rmcData.Status = Status.Valid; } else { rmcData.Status = Status.Invalid; } if (rmcData.Status == Status.Invalid) { return(rmcData); } if (values[1].Length == 6 && values[9].Length == 6) {//---- if the date and time both are six digits //---- make sure that they're actually numbers int temp; bool isparse = true; try { temp = int.Parse(values[1]); temp = int.Parse(values[9]); } catch (ArgumentException) { isparse = false; } if (isparse) { //---- should add more validation here int day = int.Parse(values[9].Substring(0, 2)); int month = int.Parse(values[9].Substring(2, 2)); int year = int.Parse(values[9].Substring(4, 2)); int hour = int.Parse(values[1].Substring(0, 2)); int minute = int.Parse(values[1].Substring(2, 2)); int second = int.Parse(values[1].Substring(4, 2)); rmcData.UtcDateTime = new DateTime(year, month, day, hour, minute, second); } else { throw new FormatException("Date or time string is invalid"); } } else if (values[1].Length == 10 && values[9].Length == 6) {//---- if the date and time both are six digits //---- make sure that they're actually numbers int temp; double timeTemp; bool isparse = true; try { timeTemp = double.Parse(values[1]); temp = int.Parse(values[9]); } catch (ArgumentException) { isparse = false; } if (isparse) { //---- should add more validation here int day = int.Parse(values[9].Substring(0, 2)); int month = int.Parse(values[9].Substring(2, 2)); int year = int.Parse(values[9].Substring(4, 2)); int hour = int.Parse(values[1].Substring(0, 2)); int minute = int.Parse(values[1].Substring(2, 2)); int second = int.Parse(values[1].Substring(4, 2)); int millisecond = int.Parse(values[1].Substring(7, 3)); rmcData.UtcDateTime = new DateTime(year, month, day, hour, minute, second); } else { throw new FormatException("Date or time string is invalid"); } } else { throw new FormatException("Date or time string is invalid"); } //---- lat/long position rmcData.Position = Position.Parse(values[3] + "," + values[4] + ";" + values[5] + "," + values[6]); //---- speed in knots rmcData.GroundSpeed = Decimal.Parse(values[7]); //---- true course rmcData.Heading = Decimal.Parse(values[8]); //---- magnetic declination rmcData.MagneticVariation = MagneticVariation.Parse(values[10] + "," + values[11]); //---- return return(rmcData); //1 220516 Time Stamp //2 A validity - A-ok, V-invalid //3 5133.82 current Latitude //4 N North/South //5 00042.24 current Longitude //6 W East/West //7 173.8 Speed in knots //8 231.8 True course //9 130694 Date Stamp //10 004.2 Variation //11 W East/West //12 *70 checksum }