BlGNSSPerception.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using ProjectBase.Data.Logs;
  2. using ProjectBase.Data.Redis;
  3. using ProjectBase.Util;
  4. using SIASUN.Autopilot.Device;
  5. using SIASUN.Autopilot.EventMessage;
  6. using SIASUN.Autopilot.GNSS;
  7. using System;
  8. namespace SIASUN.Autopilot.BLL
  9. {
  10. /// <summary>
  11. /// GNSS定位导航信息感知模块
  12. /// </summary>
  13. public class BlGNSSPerception
  14. {
  15. public static SerialPortBase GNSSSerialPort;
  16. // public event EventGNSSInfoHandler GNSSHandler;
  17. public event EventHandler<GNSSInfoEventArgs> GNSSHandler;
  18. public static BlGNSSPerception Instance { get; private set; }
  19. private string lastLatitude = "0000.00000000";
  20. private string lastlongitude = "00000.00000000";
  21. static BlGNSSPerception()
  22. {
  23. Instance = new BlGNSSPerception();
  24. }
  25. public void Init()
  26. {
  27. string PortNumber = System.Configuration.ConfigurationManager.AppSettings["PortNumber"];
  28. int baudRate = int.Parse(System.Configuration.ConfigurationManager.AppSettings["baudRate"]);
  29. int DataBits = int.Parse(System.Configuration.ConfigurationManager.AppSettings["DataBits"]);
  30. GNSSSerialPort = new SerialPortBase(PortNumber, baudRate, DataBits);
  31. GNSSSerialPort.serialDataReceived += GNSSSerialPort_serialDataReceived;
  32. }
  33. private void GNSSSerialPort_serialDataReceived(SerialPortDataReceiveEventArgs e)
  34. {
  35. LogHelper.log.Debug("接收的报文为:" + e.Value);
  36. string[] split = e.Value.Split('$');
  37. if (split.Length != 4)
  38. {
  39. LogHelper.log.Error("接收到的GNSS信号格式出现错误");
  40. return;
  41. }
  42. NMEA0183 nmea = new NMEA0183();
  43. if (!GetNMEA0183(ref split, out nmea))
  44. {
  45. return;
  46. }
  47. GNSSInfoEventArgs gnss = new GNSSInfoEventArgs(nmea);
  48. gnss.Raise(this,ref GNSSHandler);
  49. //GNSSHandler(gnss);
  50. }
  51. /// <summary>
  52. /// 解析NMEA0183协议
  53. /// </summary>
  54. /// <param name="protocol"></param>
  55. /// <param name="nmea"></param>
  56. private bool GetNMEA0183(ref string[] strNMEA, out NMEA0183 nmea)
  57. {
  58. try
  59. {
  60. string[] strValues = strNMEA;
  61. NMEA0183 temp = new NMEA0183();
  62. for (int i = 1; i < strValues.Length; i++)
  63. {
  64. string[] protocol = strValues[i].Split('*');
  65. string[] value = protocol[0].Split(',');
  66. if (value[0].Equals("GPGGA"))
  67. {
  68. if (value[2].Contains(lastLatitude.Substring(0, 9)))// 精确到分米
  69. {
  70. LogHelper.log.Debug("通过纬度去掉重复点");
  71. lastLatitude = value[2];
  72. nmea = null;
  73. return false;
  74. }
  75. lastLatitude = value[2];
  76. if (value[4].Contains(lastlongitude.Substring(0, 10)))// 精确到分米
  77. {
  78. LogHelper.log.Debug("通过经度去掉重复点");
  79. lastlongitude = value[4];
  80. nmea = null;
  81. return false;
  82. }
  83. lastlongitude = value[4];
  84. GPGGA gpgga = new GPGGA();
  85. GetGPGGA(value, out gpgga);
  86. temp.gpgga = gpgga;
  87. }
  88. else if (value[0].Equals("GPRMC"))
  89. {
  90. GPRMC gprmc = new GPRMC();
  91. GetGPRMC(value, out gprmc);
  92. temp.gprmc = gprmc;
  93. }
  94. else
  95. {
  96. string pashr = protocol[0];
  97. temp.pashr = pashr;
  98. }
  99. }
  100. nmea = temp;
  101. return true;
  102. }
  103. catch(Exception ex)
  104. {
  105. LogHelper.log.Error("nmea协议解析出现错误",ex);
  106. nmea = null;
  107. return false;
  108. }
  109. }
  110. /// <summary>
  111. /// 解析GPGGA协议
  112. /// </summary>
  113. private void GetGPGGA(string [] value, out GPGGA nmea)
  114. {
  115. GPGGA temp = new GPGGA();
  116. temp.utcTime = value[1];
  117. temp.Latitude = GetDegrees(value[2], 2);
  118. temp.LatType = value[3];
  119. temp.Longitude = GetDegrees(value[4], 3);
  120. temp.LonType = value[5];
  121. temp.GPSStatus = Convert.ToInt32(value[6]);
  122. temp.SatelliteNumber = Convert.ToInt32(value[7]);
  123. temp.Hdop = Convert.ToDecimal(value[8]);
  124. temp.Height = Convert.ToDecimal(value[9]);
  125. temp.ReleaveHeight = value[10];
  126. temp.CTKTime = value[11];
  127. temp.Check = value[12];
  128. nmea = temp;
  129. }
  130. /// <summary>
  131. /// 解析GPRMC协议
  132. /// </summary>
  133. private void GetGPRMC(string[] value, out GPRMC nmea)
  134. {
  135. GPRMC temp = new GPRMC();
  136. temp.utcTime = value[1];
  137. temp.locationStatus = value[2];
  138. temp.Latitude = GetDegrees(value[3], 2);
  139. temp.LatType = value[4];
  140. temp.Longitude = GetDegrees(value[5], 3);
  141. temp.LonType = value[6];
  142. temp.Speed = Convert.ToDecimal(value[7]);
  143. temp.Deviated = Convert.ToDecimal(value[8]);
  144. temp.Date = value[9];
  145. temp.Magnetic = Convert.ToDecimal(value[10]);
  146. temp.MagneticDirection = value[11];
  147. temp.Mode = value[12];
  148. nmea = temp;
  149. }
  150. /// <summary>
  151. /// 把经度和纬度坐标转换成度
  152. /// </summary>
  153. /// <param name="para">ddmm.mmmmmmmm</param>
  154. private decimal GetDegrees(string para, int pos)
  155. {
  156. decimal degrees = 0;
  157. try
  158. {
  159. degrees = Convert.ToDecimal(para.Substring(0, pos)) +
  160. Convert.ToDecimal(para.Substring(pos, para.Length - pos))/60;
  161. }
  162. catch (Exception ex)
  163. {
  164. LogHelper.log.Error("经纬度坐标转换出现错误", ex);
  165. }
  166. return degrees;
  167. }
  168. public void Close()
  169. {
  170. GNSSSerialPort.Close();
  171. }
  172. }
  173. /// <summary>
  174. /// 串口接收数据
  175. /// </summary>
  176. public delegate void EventGNSSInfoHandler(GNSSInfoEventArgs e);
  177. public class GNSSInfoEventArgs : EventArgs
  178. {
  179. public NMEA0183 Nmea { get; set; }
  180. public GNSSInfoEventArgs(NMEA0183 value)
  181. {
  182. this.Nmea = value;
  183. }
  184. }
  185. }