Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / Java

NMEA 0183 sentence parser/builder

Rate me:
Please Sign up or sign in to vote.
4.78/5 (25 votes)
10 Dec 2013CPOL6 min read 249.2K   15K   97  
Library for working with NMEA0183 devices
using System;
using System.Text;
using NMEA;

namespace GNSSView
{
    // Code by Aleksandr Dikarev, dikarev-aleksandr@yandex.ru

    #region Custom EventArgs

    public class NewNMEAMessageEventArgs : EventArgs
    {
        #region Properties

        public string Message { get; private set; }

        #endregion

        #region Constructor

        public NewNMEAMessageEventArgs(string message)
        {
            Message = message;
        }

        #endregion
    }

    #endregion

    public abstract class NMEAPort
    {
        #region Properties

        public abstract bool IsOpen { get; }

        StringBuilder buffer;

        #endregion

        #region Methods

        #region Abstract

        public abstract void SendData(string message);
        public abstract void Open();
        public abstract void Close();

        #endregion

        #region Protected

        protected void OnIncomingData(string data)
        {
            buffer.Append(data);
            var temp = buffer.ToString();

            int lIndex = temp.LastIndexOf(NMEAParser.SentenceEndDelimiter);
            if (lIndex >= 0)
            {
                buffer = buffer.Remove(0, lIndex + 2);
                if (lIndex + 2 < temp.Length)
                    temp = temp.Remove(lIndex + 2);

                temp = temp.Trim(new char[] { '\0' });

                var lines = temp.Split(NMEAParser.SentenceEndDelimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                if (NewNMEAMessage != null)
                {
                    for (int i = 0; i < lines.Length; i++)
                        NewNMEAMessage(this, new NewNMEAMessageEventArgs(string.Format("{0}{1}", lines[i], NMEAParser.SentenceEndDelimiter)));
                }
            }

            if (buffer.Length >= ushort.MaxValue)
                buffer.Remove(0, short.MaxValue);
        }

        protected void OnConnectionOpening()
        {
            buffer = new StringBuilder();
        }

        protected void OnConnectionClosing()
        {
            buffer = new StringBuilder();
        }

        #endregion        

        #endregion

        #region Events

        public EventHandler<NewNMEAMessageEventArgs> NewNMEAMessage;

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) JSC "SHTIL"
Russian Federation Russian Federation
underwater acoustics, communication and positioning.
Embedded software development. Digital signal processing.

Graduate of Volgograd Technical State University (2007),
Postgraduate student in Kovrov Technological State Academy (2012).

Comments and Discussions