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

时间:2021-06-21 09:20:20

帧头

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 = ; i < strtemp.Length; i++)
{
string[] strtemp1 = strtemp[i].Split(',');
if (strtemp1.Length >= )
{
if (strtemp1[] == "$GNRMC")
{
gpsInfo = new GPSInfo();
gpsInfo.GPSStatus = strtemp1[];
gpsInfo.GPSHeading = strtemp1[];
gpsInfo.Speed = strtemp1[] == "" ? "" : Convert.ToDouble(Convert.ToDouble(strtemp1[]) * 1.852).ToString("0.0");
gpsInfo.Latitude = strtemp1[] == "" ? "" : GPSTransforming(strtemp1[]).ToString("0.000000");
gpsInfo.Longitude = strtemp1[] == "" ? "" : GPSTransforming(strtemp1[]).ToString("0.000000");
gpsInfo.GPSTime = strtemp1[] == "" ? "" : "" + strtemp1[].Substring(, ) + "-" + strtemp1[].Substring(, ) + "-" + strtemp1[].Substring(, ) + " " + strtemp1[].Substring(, ) + ":" + strtemp1[].Substring(, ) + ":" + strtemp1[].Substring(, );
}
}
}
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(, _RecString.IndexOf("\r")) : _RecString;
string[] seg = _RecString.Split(',');
if (seg.Length >= )
{
gpsInfo = new GPSInfo();
gpsInfo.GPSStatus = seg[];//状态
gpsInfo.GPSHeading = seg[];//角度
gpsInfo.Speed = seg[] == "" ? "" : (Convert.ToDouble(seg[]) * 1.852).ToString("0.0");//速度
gpsInfo.Latitude = seg[] == "" ? "" : GPSTransforming(seg[]).ToString("0.000000");
gpsInfo.Longitude = seg[] == "" ? "" : GPSTransforming(seg[]).ToString("0.000000"); ;
gpsInfo.GPSTime = seg[] == "" ? "" : string.Format("20{0}-{1}-{2} {3}:{4}:{5}", seg[].Substring(), seg[].Substring(, ), seg[].Substring(, ), seg[].Substring(, ), seg[].Substring(, ), seg[].Substring());
}
}
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[].Substring(, TempStr[].Length - );
string y = TempStr[].Substring(TempStr[].Length - , );
string z = TempStr[].Substring(, );
Ret = Convert.ToDouble(x) + Convert.ToDouble(y) / + Convert.ToDouble(z) / ;
return Ret;
}
}