Example #1
0
 public void TestReceiveMessage()
 {
     for (;;)
     {
         GPSInfo device = new GPSInfo();
         device.ID   = "26887";
         device.Lat  = 40.04933;
         device.Lon  = 116.31224;
         device.Time = DateTime.Now.ToString();
         RaiseReceiveGPS(device);
         Thread.Sleep(10 * 1000);
     }
 }
Example #2
0
 /// <summary>
 /// 上报接收到GPS信号事件
 /// </summary>
 /// <param name="info">GPS信息</param>
 private void RaiseReceiveGPS(GPSInfo info)
 {
     OnReceiveGPSInfo?.Invoke(info);
 }
Example #3
0
        /// <summary>
        /// 接收消息处理
        /// </summary>
        private void ReceiveMessage(CancellationToken ct)
        {
            //监听所有设备发来的UDP消息
            IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Any, Port);

            while (!LifeTimeControl.closing)
            {
                if (ct.IsCancellationRequested)
                {
                    udpcRecv.Close();
                    logger.Info("GPS UDP监听器已关闭");
                    return;
                }

                try
                {
                    //UDP接收GPS数据格式:
                    //4字节:Type = 5;
                    //4字节:设备ID;
                    //8字节:时间戳;
                    //后续数据:标准的NMEA 0183协议GPS数据
                    byte[] bytes = udpcRecv.Receive(ref ipendpoint);

                    int deviceId = bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7];

                    byte[] gpsdata = new byte[bytes.Length - GPS_MESSAGE_HEADER_LENGTH];
                    for (int i = GPS_MESSAGE_HEADER_LENGTH; i < bytes.Length; i++)
                    {
                        gpsdata[i - GPS_MESSAGE_HEADER_LENGTH] = bytes[i];
                    }
                    string message = Encoding.Default.GetString(gpsdata, 0, gpsdata.Length);
                    logger.Info("收到来自ID为“" + deviceId +
                                "”的UDP消息:“" + message + "”。");

                    if (message.StartsWith("$GPRMC"))
                    {
                        //切割字符串
                        string[] temp = message.Split(',');
                        GPSInfo  info = new GPSInfo();
                        info.ID = (bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7]).ToString();  //设备的ID
                        try
                        {
                            //如果接收到的经纬度信息为空,则继续下次数据接收
                            if (temp[3].Trim() == "" || temp[5].Trim() == "")
                            {
                                logger.Warn("接收到的GPS位置信号为空!");
                                continue;
                            }

                            double[] latLon = GPS2BD09.wgs2bd(Double.Parse(temp[3].Substring(0, 2))
                                                              + Double.Parse(temp[3].Substring(2)) / 60.0,
                                                              Double.Parse(temp[5].Substring(0, 3))
                                                              + Double.Parse(temp[5].Substring(3)) / 60.0);
                            info.Lat = latLon[0];
                            info.Lon = latLon[1];
                        }
                        catch (Exception ex)
                        {
                            //经纬度解析异常,继续下次数据接收
                            logger.Error("接收到的GPS位置信号有问题!", ex);
                            continue;
                        }
                        info.Time = DateTime.Now.ToString();
                        //保存GPS信息到相应文件中
                        FileUtils.AppendGPSInfoToFile(info);
                        //上报GPS更新信息
                        RaiseReceiveGPS(info);
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("接收数据出现异常", ex);
                }
            }
        }