Beispiel #1
0
        /// <summary>
        /// Parses the specified NMEA message.
        /// </summary>
        /// <param name="message">The NMEA message string.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">
        /// Invalid nmea message: Missing starting character '$'
        /// or checksum failure
        /// </exception>
        public static NmeaMessage Parse(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException("message");
            }

            int checksum = -1;

            if (message[0] != '$')
            {
                throw new ArgumentException("Invalid nmea message: Missing starting character '$'");
            }
            var idx = message.IndexOf('*');

            if (idx >= 0)
            {
                checksum = Convert.ToInt32(message.Substring(idx + 1), 16);
                message  = message.Substring(0, message.IndexOf('*'));
            }
            if (checksum > -1)
            {
                int checksumTest = 0;
                for (int i = 1; i < message.Length; i++)
                {
                    checksumTest ^= Convert.ToByte(message[i]);
                }
                if (checksum != checksumTest)
                {
                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid nmea message: Checksum failure. Got {0:X2}, Expected {1:X2}", checksum, checksumTest));
                }
            }

            string[] parts       = message.Split(new char[] { ',' });
            string   MessageType = parts[0].Substring(1);

            string[] MessageParts = parts.Skip(1).ToArray();
            if (messageTypes == null)
            {
                LoadResponseTypes();
            }
            NmeaMessage msg = null;

            if (messageTypes.ContainsKey(MessageType))
            {
                msg = (NmeaMessage)messageTypes[MessageType].Invoke(new object[] { });
            }
            else
            {
                msg = new UnknownMessage();
            }
            msg.MessageType  = MessageType;
            msg.MessageParts = MessageParts;
            msg.OnLoadMessage(MessageParts);
            return(msg);
        }
Beispiel #2
0
		public static NmeaMessage Parse(string message)
		{
			int checksum = -1;
			if (message[0] != '$')
				throw new ArgumentException("Invalid nmea message: Missing starting character '$'");
			var idx = message.IndexOf('*');
			if (idx >= 0)
			{
				checksum = Convert.ToInt32(message.Substring(idx + 1), 16);
				message = message.Substring(0, message.IndexOf('*'));
			}
			if (checksum > -1)
			{
				int checksumTest = 0;
				for (int i = 1; i < message.Length; i++)
				{
					if (i == 0) continue;
					checksumTest ^= Convert.ToByte(message[i]);
				}
				if (checksum != checksumTest)
					throw new ArgumentException(string.Format("Invalid nmea message: Checksum failure. Got {0:X2}, Expected {1:X2}", checksum, checksumTest));
			}

			string[] parts = message.Split(new char[] { ',' });
			string MessageType = parts[0].Substring(1);
			string[] MessageParts = parts.Skip(1).ToArray();
			if(messageTypes == null)
			{
				LoadResponseTypes();
			}
			NmeaMessage msg = null;
			if (messageTypes.ContainsKey(MessageType))
			{
				msg = (NmeaMessage)messageTypes[MessageType].Invoke(new object[] { });
			}
			else
			{
				msg = new UnknownMessage();
			}
			msg.MessageType = MessageType;
			msg.MessageParts = MessageParts;
			msg.LoadMessage(MessageParts);
			return msg;
		}