.NET Compact Framework下的GPS NMEA data数据分析(二)转

简介: .NET Compact Framework下的GPS NMEA data数据分析(二)转
public class NmeaParser
    {
        public struct Coordinate
        {
            public int Hours;
            public int Minutes;
            public double Seconds;
        }
        public enum FixStatus
        {
            NotSet,
            Obtained, //A
            Lost //V
        }
        public enum FixMode
        {
            Auto,   //A
            Manual
        }
        public enum FixMethod
        {
            NotSet,
            Fix2D,
            Fix3D
        }
        public enum DifferentialGpsType
        {
            NotSet, 
            SPS,    
            DSPS,   
            PPS,    
            RTK     
        }
        public class GpsSatellite
        {
            public int PRC { get; set; }
            public int Elevation { get; set; }
            public int Azimuth { get; set; }
            public int SNR { get; set; }
            public bool InUsed { get; set; }
            public bool InView { get; set; }
            public bool NotTracking { get; set; }
        }
        private static readonly CultureInfo NmeaCultureInfo = new CultureInfo("en-US");
        private static readonly decimal KMpHPerKnot = decimal.Parse("1.852", NmeaCultureInfo);
        private Coordinate latitude;
        private Coordinate longitude;
        private decimal altitude = 0;
        private DateTime utcDateTime;
        private decimal velocity = 0;
        private decimal azimuth = 0;
        private FixStatus fixStatus;
        private DifferentialGpsType differentialGpsType;
        private FixMode fixMode;
        private FixMethod fixMethod;
        private int satellitesInView;
        private int satellitesInUsed;
        private readonly Dictionary<int, GpsSatellite> satellites;
        private decimal horizontalDilutionOfPrecision = 50;
        private decimal positionDilutionOfPrecision = 50;
        private decimal verticalDilutionOfPrecision = 50;
        public NmeaParser()
        {
            satellites = new Dictionary<int, GpsSatellite>();
        }
        public bool Parse(string sentence)
        {
            string rawData = sentence;
            try
            {
                if (!IsValid(sentence))
                {
                    return false;
                }
                sentence = sentence.Substring(1, sentence.IndexOf('*') - 1);
                string[] Words = Getwords(sentence);
                switch (Words[0])
                {
                    case "GPRMC":
                        return ParseGPRMC(Words);
                    case "GPGGA":
                        return ParseGPGGA(Words);
                    case "GPGSA":
                        return ParseGPGSA(Words);
                    case "GPGSV":
                        return ParseGPGSV(Words);
                    default:
                        return false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + rawData);
                return false;
            }
        }
        private bool IsValid(string sentence)
        {
            // GPS data can't be zero length
            if (sentence.Length == 0)
            {
                return false;
            }
            // first character must be a $
            if (sentence[0] != '$')
            {
                return false;
            }
            // GPS data can't be longer than 82 character
            if (sentence.Length > 82)
            {
                return false;
            }
            try
            {
                string checksum = sentence.Substring(sentence.IndexOf('*') + 1);
                return Checksum(sentence, checksum);
            }
            catch (Exception e)
            {
                Console.WriteLine("Checksum failure. " + e.Message);
                return false;
            }
        }
        private bool Checksum(string sentence, string checksumStr)
        {
            int checksum = 0;
            int length = sentence.IndexOf('*') - 1;
            // go from first character upto last *
            for (int i = 1; i <= length; ++i)
            {
                checksum = checksum ^ Convert.ToByte(sentence[i]);
            }
            return (checksum.ToString("X2") == checksumStr);
        }
        // Divides a sentence into individual Words
        private static string[] Getwords(string sentence)
        {
            return sentence.Split(',');
        }
        private bool ParseGPRMC(string[] Words)
        {
            if (Words[1].Length > 0 & Words[9].Length > 0)
            {
                int UtcHours = Convert.ToInt32(Words[1].Substring(0, 2));
                int UtcMinutes = Convert.ToInt32(Words[1].Substring(2, 2));
                int UtcSeconds = Convert.ToInt32(Words[1].Substring(4, 2));
                int UtcMilliseconds = 0;
                // Extract milliseconds if it is available
                if (Words[1].Length > 7)
                {
                    UtcMilliseconds = Convert.ToInt32(Words[1].Substring(7));
                }
                int UtcDay = Convert.ToInt32(Words[9].Substring(0, 2));
                int UtcMonth = Convert.ToInt32(Words[9].Substring(2, 2));
                // available for this century
                int UtcYear = Convert.ToInt32(Words[9].Substring(4, 2)) + 2000;
                utcDateTime = new DateTime(UtcYear, UtcMonth, UtcDay, UtcHours, UtcMinutes, UtcSeconds, UtcMilliseconds);
            }
            fixStatus = (Words[2][0] == 'A') ? FixStatus.Obtained : FixStatus.Lost;
            if (Words[3].Length > 0 & Words[4].Length == 1 & Words[5].Length > 0 & Words[6].Length == 1)
            {
                latitude.Hours = int.Parse(Words[3].Substring(0, 2));
                latitude.Minutes = int.Parse(Words[3].Substring(2, 2));
                latitude.Seconds = Math.Round(double.Parse(Words[3].Substring(5, 4)) * 6 / 1000.0, 3);
                if ("S" == Words[4])
                {
                    latitude.Hours = -latitude.Hours;
                }
                longitude.Hours = int.Parse(Words[5].Substring(0, 3));
                longitude.Minutes = int.Parse(Words[5].Substring(3, 2));
                longitude.Seconds = Math.Round(double.Parse(Words[5].Substring(6, 4)) * 6 / 1000.0, 3);
                if ("W" == Words[6])
                {
                    longitude.Hours = -longitude.Hours;
                }
            }
            if (Words[8].Length > 0)
            {
                azimuth = decimal.Parse(Words[8], NmeaCultureInfo);
            }
            if (Words[7].Length > 0)
            {
                velocity = decimal.Parse(Words[7], NmeaCultureInfo) * KMpHPerKnot;
            }
            return true;
        }
        private bool ParseGPGGA(string[] Words)
        {
            if (Words[6].Length > 0)
            {
                switch (Convert.ToInt32(Words[6]))
                {
                    case 0:
                        differentialGpsType = DifferentialGpsType.NotSet;
                        break;
                    case 1:
                        differentialGpsType = DifferentialGpsType.SPS;
                        break;
                    case 2:
                        differentialGpsType = DifferentialGpsType.DSPS;
                        break;
                    case 3:
                        differentialGpsType = DifferentialGpsType.PPS;
                        break;
                    case 4:
                        differentialGpsType = DifferentialGpsType.RTK;
                        break;
                    default:
                        differentialGpsType = DifferentialGpsType.NotSet;
                        break;
                }
            }
            if (Words[7].Length > 0)
            {
                satellitesInUsed = Convert.ToInt32(Words[7]);
            }
            if (Words[8].Length > 0)
            {
                horizontalDilutionOfPrecision = Convert.ToDecimal(Words[8]);
            }
            if (Words[9].Length > 0)
            {
                altitude = Convert.ToDecimal(Words[9]);
            }
            return true;
        }
        private bool ParseGPGSA(string[] Words)
        {
            if (Words[1].Length > 0)
            {
                fixMode = Words[1][0] == 'A' ? FixMode.Auto : FixMode.Manual;
            }
            if (Words[2].Length > 0)
            {
                switch (Convert.ToInt32(Words[2]))
                {
                    case 1:
                        fixMethod = FixMethod.NotSet;
                        break;
                    case 2:
                        fixMethod = FixMethod.Fix2D;
                        break;
                    case 3:
                        fixMethod = FixMethod.Fix3D;
                        break;
                    default:
                        fixMethod = FixMethod.NotSet;
                        break;
                }
            }
            foreach (GpsSatellite s in satellites.Values)
            {
                s.InUsed = false;
            }
            satellitesInUsed = 0;
            for (int i = 0; i < 12; ++i)
            {
                string id = Words[3 + i];
                if (id.Length > 0)
                {
                    int nId = Convert.ToInt32(id);
                    if (!satellites.ContainsKey(nId))
                    {
                        satellites[nId] = new GpsSatellite();
                        satellites[nId].PRC = nId;
                    }
                    satellites[nId].InUsed = true;
                    ++satellitesInUsed;
                }
            }
            if (Words[15].Length > 0)
            {
                positionDilutionOfPrecision = Convert.ToDecimal(Words[15]);
            }
            if (Words[16].Length > 0)
            {
                horizontalDilutionOfPrecision = Convert.ToDecimal(Words[16]);
            }
            if (Words[17].Length > 0)
            {
                verticalDilutionOfPrecision = Convert.ToDecimal(Words[17]);
            }
            return true;
        }
        private bool ParseGPGSV(string[] Words)
        {
            int messageNumber = 0;
            if (Words[2].Length > 0)
            {
                messageNumber = Convert.ToInt32(Words[2]);
            }
            if (Words[3].Length > 0)
            {
                satellitesInView = Convert.ToInt32(Words[3]);
            }
            if (messageNumber == 0 || satellitesInView == 0)
            {
                return false;
            }
            for (int i = 1; i <= 4; ++i)
            {
                if ((Words.Length - 1) >= (i * 4 + 3))
                {
                    int nId = 0;
                    if (Words[i * 4].Length > 0)
                    {
                        string id = Words[i * 4];
                        nId = Convert.ToInt32(id);
                        if (!satellites.ContainsKey(nId))
                        {
                            satellites[nId] = new GpsSatellite();
                            satellites[nId].PRC = nId;
                        }
                        satellites[nId].InView = true;
                    }
                    if (Words[i * 4 + 1].Length > 0)
                    {
                        satellites[nId].Elevation = Convert.ToInt32(Words[i * 4 + 1]);
                    }
                    if (Words[i * 4 + 2].Length > 0)
                    {
                        satellites[nId].Azimuth = Convert.ToInt32(Words[i * 4 + 2]);
                    }
                    if (Words[i * 4 + 3].Length > 0)
                    {
                        satellites[nId].SNR = Convert.ToInt32(Words[i * 4 + 3]);
                        satellites[nId].NotTracking = false;
                    }
                    else
                    {
                        satellites[nId].NotTracking = true;
                    }
                }
            }
            return true;
        }
    }
相关文章
|
1月前
|
API C++ Windows
Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法
本文介绍Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法,提供官方下载链接与系统修复工具使用指南。
477 2
|
机器学习/深度学习 人工智能 自然语言处理
如何构建企业级数据智能体:Data Agent 开发实践
本篇将介绍DMS的一款数据分析智能体(Data Agent for Analytics )产品的技术思考和实践。Data Agent for Analytics 定位为一款企业级数据分析智能体, 基于Agentic AI 技术,帮助用户查数据、做分析、生成报告、深入洞察。
|
4月前
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
341 3
|
机器学习/深度学习 人工智能 自然语言处理
构建企业级数据分析助手:Data Agent 开发实践
本篇将介绍DMS的一款数据分析智能体(Data Agent for Analytics )产品的技术思考和实践。Data Agent for Analytics 定位为一款企业级数据分析智能体, 基于Agentic AI 技术,帮助用户查数据、做分析、生成报告、深入洞察。由于不同产品的演进路径,背景都不一样,所以只介绍最核心的部分,来深入剖析如何构建企业级数据分析助手:能力边界定义,技术内核,企业级能力。希望既能作为Data Agent for Analytics产品的技术核心介绍,也能作为读者的开发实践的参考。
549 1
构建企业级数据分析助手:Data Agent 开发实践
|
1月前
|
开发框架 安全 .NET
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
.NET Framework是Windows平台的开发框架,包含CLR和FCL,支持多种语言开发桌面、Web应用。常用版本有3.5、4.5.2、4.8.1,系统可同时安装多个版本,确保软件兼容运行。
529 0
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
|
2月前
|
C++
提示缺少.NET Framework 3.5 安装错误:0x80070002、0x800F0950\0x80004002
.NET Framework常见问题及解决方法汇总,
445 0
|
3月前
.NET Framework 3.5离线安装包合集下载
本文介绍了如何获取和安装.NET Framework运行库离线合集包。用户可通过提供的链接下载安装包,安装过程简单,按提示逐步操作即可完成。安装时可选择所需版本,工具会自动适配架构,无需手动判断,方便高效。
1496 0
|
机器学习/深度学习 数据采集 数据可视化
基于爬虫和机器学习的招聘数据分析与可视化系统,python django框架,前端bootstrap,机器学习有八种带有可视化大屏和后台
本文介绍了一个基于Python Django框架和Bootstrap前端技术,集成了机器学习算法和数据可视化的招聘数据分析与可视化系统,该系统通过爬虫技术获取职位信息,并使用多种机器学习模型进行薪资预测、职位匹配和趋势分析,提供了一个直观的可视化大屏和后台管理系统,以优化招聘策略并提升决策质量。
820 4
|
数据采集 数据可视化 数据挖掘
数据分析大神养成记:Python+Pandas+Matplotlib助你飞跃!
在数字化时代,数据分析至关重要,而Python凭借其强大的数据处理能力和丰富的库支持,已成为该领域的首选工具。Python作为基石,提供简洁语法和全面功能,适用于从数据预处理到高级分析的各种任务。Pandas库则像是神兵利器,其DataFrame结构让表格型数据的处理变得简单高效,支持数据的增删改查及复杂变换。配合Matplotlib这一数据可视化的魔法棒,能以直观图表展现数据分析结果。掌握这三大神器,你也能成为数据分析领域的高手!
248 2

热门文章

最新文章