GPRM/GNRMC定位信息的读取与解析

帧头

UTC时间

状态

纬度

北纬/南纬

经度

东经/西经

速度

$GPRMC

hhmmss.sss

A/V

ddmm.mmmm

N/S

dddmm.mmmm

E/W

 

 

 

 

方位角

UTC日期

磁偏角

磁偏角方向

模式

校验

回车换行

ddmmyy

000 - 180

E/W

A/D/E/N

*hh

CR+LF

 


 

 

 

 

 

格 式: $GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11>,<12>*hh<CR><LF>
$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
说 明:
字段 0:$GPRMC,语句ID,表明该语句为Recommended Minimum Specific GPS/TRANSIT Data(RMC)推荐最小定位信息
          字段 1:UTC时间,hhmmss.sss格式
          字段 2:状态,A=定位,V=未定位
          字段 3:纬度ddmm.mmmm,度分格式(前导位数不足则补0)
          字段 4:纬度N(北纬)或S(南纬)
          字段 5:经度dddmm.mmmm,度分格式(前导位数不足则补0)
          字段 6:经度E(东经)或W(西经)
          字段 7:速度,节,Knots(一节也是1.852千米/小时)
          字段 8:方位角,度(二维方向指向,相当于二维罗盘)
          字段 9:UTC日期,DDMMYY格式
          字段10:磁偏角,(000 - 180)度(前导位数不足则补0)
          字段11:磁偏角方向,E=东,W=西
          字段12:模式,A=自动,D=差分,E=估测,N=数据无效(3.0协议内容)
          字段13:校验值

 /// <summary>
    /// GPS信息
    /// </summary>
    public class GPSInfo
    {
        public string Longitude;//经度
        public string Latitude; //纬度
        public string Speed;    //速度
        public string GPSStatus;//GPS状态 A=数据有效;V=数据无效
        public string GPSTime;//GPS时间
        public string GPSHeading;//航向
    }

    /// <summary>
    /// GPS/BD定位信息解析
    /// </summary>
    public static class GPSAnalysisClass
    {
        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="_SerialPort">SerialPort</param>
        /// <param name="_PortName">PortName</param>
        /// <param name="_BaudRate">BaudRate</param>
        /// <returns></returns>
        public static bool OpenSerialPort(SerialPort _SerialPort, string _PortName, int _BaudRate)
        {
            bool Ret = false;
            try
            {
                _SerialPort.Close();
                _SerialPort.PortName = _PortName;
                _SerialPort.BaudRate = _BaudRate;
                _SerialPort.NewLine = Environment.NewLine;
                _SerialPort.Open();
                if (_SerialPort.IsOpen)
                    Ret = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Ret = false;
            }
            return Ret;
        }

        /// <summary>
        /// GNRMC解析[北斗]
        /// </summary>
        /// <param name="_RecString">原始字符串</param>
        /// <returns>北斗定位信息</returns>
        public static GPSInfo GNRMCAnalysis(string _RecString)
        {
            GPSInfo gpsInfo = null;
            string[] strtemp = _RecString.Split('\n');
            for (int i = 0; i < strtemp.Length; i++)
            {
                string[] strtemp1 = strtemp[i].Split(',');
                if (strtemp1.Length >= 12)
                {
                    if (strtemp1[0] == "$GNRMC")
                    {
                        gpsInfo = new GPSInfo();
                        gpsInfo.GPSStatus = strtemp1[2];
                        gpsInfo.GPSHeading = strtemp1[8];
                        gpsInfo.Speed = strtemp1[7] == "" ? "" : Convert.ToDouble(Convert.ToDouble(strtemp1[7]) * 1.852).ToString("0.0");
                        gpsInfo.Latitude = strtemp1[3] == "" ? "" : GPSTransforming(strtemp1[3]).ToString("0.000000");
                        gpsInfo.Longitude = strtemp1[5] == "" ? "" : GPSTransforming(strtemp1[5]).ToString("0.000000");
                        gpsInfo.GPSTime = strtemp1[9] == "" ? "" : "20" + strtemp1[9].Substring(4, 2) + "-" + strtemp1[9].Substring(2, 2) + "-" + strtemp1[9].Substring(0, 2) + " " + strtemp1[1].Substring(0, 2) + ":" + strtemp1[1].Substring(2, 2) + ":" + strtemp1[1].Substring(4, 2);
                    }
                }
            }
            return gpsInfo;
        }

        /// <summary>
        /// GPRM字符串解析[GPS]
        /// </summary>
        /// <param name="_RecString">原始字符串</param>
        /// <returns>GPS定位信息</returns>
        public static GPSInfo GPRMCAnalysis(string _RecString)
        {
            GPSInfo gpsInfo = null;
            if (!string.IsNullOrEmpty(_RecString))
            {
                _RecString = _RecString.Contains("\r") ? _RecString.Substring(0, _RecString.IndexOf("\r")) : _RecString;
                string[] seg = _RecString.Split(',');
                if (seg.Length >= 12)
                {
                    gpsInfo = new GPSInfo();
                    gpsInfo.GPSStatus = seg[2];//状态
                    gpsInfo.GPSHeading = seg[8];//角度
                    gpsInfo.Speed = seg[7] == "" ? "" : (Convert.ToDouble(seg[7]) * 1.852).ToString("0.0");//速度
                    gpsInfo.Latitude = seg[4] == "" ? "" : GPSTransforming(seg[3]).ToString("0.000000");
                    gpsInfo.Longitude = seg[6] == "" ? "" : GPSTransforming(seg[5]).ToString("0.000000"); ;
                    gpsInfo.GPSTime = seg[9] == "" ? "" : string.Format("20{0}-{1}-{2} {3}:{4}:{5}", seg[9].Substring(4), seg[9].Substring(2, 2), seg[9].Substring(0, 2), seg[1].Substring(0, 2), seg[1].Substring(2, 2), seg[1].Substring(4));
                }
            }
            return gpsInfo;
        }

        /// <summary>
        /// 降度分秒格式经纬度转换为小数经纬度
        /// </summary>
        /// <param name="_Value">度分秒经纬度</param>
        /// <returns>小数经纬度</returns>
        private static double GPSTransforming(string _Value)
        {
            double Ret = 0.0;
            string[] TempStr = _Value.Split('.');
            string x = TempStr[0].Substring(0, TempStr[0].Length - 2);
            string y = TempStr[0].Substring(TempStr[0].Length - 2, 2);
            string z = TempStr[1].Substring(0, 4);
            Ret = Convert.ToDouble(x) + Convert.ToDouble(y) / 60 + Convert.ToDouble(z) / 600000;
            return Ret;
        }
    }

 

 

你可能感兴趣的:(GPRM/GNRMC定位信息的读取与解析)