public static byte CRC(NmeaMessage msg, NmeaFormat format)
        {
            byte crc = 0;

            if (!string.IsNullOrEmpty(msg.Header))
            {
                for (int i = 0; i < msg.Header.Length; i++)
                {
                    crc ^= (byte)msg.Header[i];
                }
            }

            if (msg.Fields != null)
            {
                foreach (string field in msg.Fields)
                {
                    crc ^= Convert.ToByte(format.Separator);
                    if (!string.IsNullOrEmpty(field))
                    {
                        for (int i = 0; i < field.Length; i++)
                        {
                            crc ^= (byte)field[i];
                        }
                    }
                }
            }

            return(crc);
        }
        /// <summary>Tworzy łańcuch z komunikatem NMEA wg podanego formatu</summary>
        /// <param name="msg">Komunikat NMEA</param>
        /// <param name="format">Definicja sposobu formatowania komunikatu</param>
        /// <returns>Sformatowany komunikat NMEA</returns>
        public static string ToString(NmeaMessage msg, NmeaFormat format)
        {
            if (msg == null)
            {
                throw new ArgumentNullException();
            }

            lock (text)
            {
                text.Remove(0, text.Length);

                text.Append(format.Prefix);
                if (!string.IsNullOrEmpty(msg.Header))
                {
                    text.Append(msg.Header);
                }

                if (msg.Fields != null)
                {
                    foreach (string f in msg.Fields)
                    {
                        text.Append(format.Separator);
                        if (!string.IsNullOrEmpty(f))
                        {
                            text.Append(f);
                        }
                    }
                }

                text.Append(format.Suffix);

                byte crc = NmeaCrcCalculator.CRC(msg, format);
                text.Append(crc.ToString("X02"));

                if (!string.IsNullOrEmpty(format.Terminator))
                {
                    text.Append(format.Terminator);
                }

                return(text.ToString());
            }
        }
 public static NmeaMessage FromString(string msg, NmeaFormat format)
 {
     return(new NmeaMessage(msg.TrimStart(format.Prefix).Remove(msg.IndexOf(format.Suffix) - 1)));
 }
 public NmeaMessage(string header, string[] fields, NmeaFormat format)
 {
     this.Header = header;
     this.Fields = fields;
     this.Format = format;
 }