示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Usage: Ntrip.exe comport");
            Console.WriteLine("Usage: Ntrip.exe comport lat lng alt");
            Console.CancelKeyPress += Console_CancelKeyPress;
            AppDomain.CurrentDomain.DomainUnload += (sender, eventArgs) =>
            {
                stop = true;
                Thread.Sleep(1000);
            };
            var tcp = TcpListener.Create(2101);

            tcp.Start();

            tcp.BeginAcceptTcpClient(processclient, tcp);

            var comport = args[0];
            var port    = new SerialPort(comport, 115200);

            var      ubx      = new Ubx();
            var      rtcm     = new rtcm3();
            Stream   file     = null;
            DateTime filetime = DateTime.MinValue;

            Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            while (!stop)
            {
                try
                {
                    if (file == null || DateTime.UtcNow.Day != filetime.Day)
                    {
                        if (file != null)
                        {
                            file.Close();
                        }
                        string fn = "";
                        int    no = 0;
                        while (File.Exists((fn = DateTime.UtcNow.ToString("yyyy-MM-dd") + "-" + no + ".rtcm.Z")))
                        {
                            no++;
                        }

                        file = new GZipStream(new BufferedStream(new FileStream(fn,
                                                                                FileMode.OpenOrCreate)), CompressionMode.Compress);
                        filetime = DateTime.UtcNow;
                    }

                    if (!port.IsOpen)
                    {
                        port.Open();
                        ubx.SetupM8P(port, true, false);

                        if (args.Length == 4)
                        {
                            ubx.SetupBasePos(port,
                                             new PointLatLngAlt(double.Parse(args[1]), double.Parse(args[2]), double.Parse(args[3])),
                                             60, 2);
                        }
                        else
                        {
                            ubx.SetupBasePos(port, PointLatLngAlt.Zero, 60, 2);
                        }
                    }

                    var btr = port.BytesToRead;
                    if (btr > 0)
                    {
                        var buffer = new byte[btr];
                        port.Read(buffer, 0, btr);

                        foreach (byte by in buffer)
                        {
                            btr--;

                            if (ubx.Read((byte)by) > 0)
                            {
                                rtcm.resetParser();
                                //Console.WriteLine(DateTime.Now + " new ubx message");
                            }
                            else if (by >= 0 && rtcm.Read((byte)by) > 0)
                            {
                                ubx.resetParser();
                                //Console.WriteLine(DateTime.Now + " new rtcm message");
                                gotRTCMData?.Invoke(rtcm.packet, rtcm.length);
                                file.Write(new ReadOnlySpan <byte>(rtcm.packet, 0, rtcm.length));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                Thread.Sleep(100);
            }

            if (file != null)
            {
                file.Close();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Usage: Ntrip.exe comport");
            Console.WriteLine("Usage: Ntrip.exe comport lat lng alt");
            Console.CancelKeyPress += Console_CancelKeyPress;
            AppDomain.CurrentDomain.DomainUnload += (sender, eventArgs) =>
            {
                stop = true;
                Thread.Sleep(1000);
            };
            var tcp = TcpListener.Create(2101);

            Console.WriteLine("Listerning on 2101");

            tcp.Start();

            tcp.BeginAcceptTcpClient(processclient, tcp);

            var        ubx      = new Ubx();
            var        rtcm     = new rtcm3();
            var        can      = new UAVCAN.uavcan();
            Stream     file     = null;
            DateTime   filetime = DateTime.MinValue;
            SerialPort port     = null;

            everyminute = DateTime.Now.Minute;

            Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            // setup allocator
            can.SourceNode = 127;
            can.SetupDynamicNodeAllocator();

            // feed the rtcm data into the rtcm parser if we get a can message
            can.MessageReceived += (frame, msg, id) =>
            {
                if (frame.MsgTypeID == (ushort)uavcan.UAVCAN_EQUIPMENT_GNSS_RTCMSTREAM_DT_ID)
                {
                    var rtcmcan = (uavcan.uavcan_equipment_gnss_RTCMStream)msg;

                    for (int a = 0; a < rtcmcan.data_len; a++)
                    {
                        int seenmsg = -1;

                        if ((seenmsg = rtcm.Read(rtcmcan.data[a])) > 0)
                        {
                            Console.WriteLine("CANRTCM" + seenmsg);
                            gotRTCMData?.Invoke(rtcm.packet, rtcm.length);
                            file.Write(rtcm.packet, 0, rtcm.length);
                        }
                    }
                }
            };

            while (!stop)
            {
                try
                {
                    if (port == null || !port.IsOpen)
                    {
                        var comport = args[0];
                        if (comport.Contains("/dev/"))
                        {
                            comport = UnixPath.GetRealPath(comport);
                        }
                        Console.WriteLine("Port: " + comport);
                        port = new SerialPort(comport, 115200);

                        port.Open();
                        ubx.SetupM8P(port, true, false);

                        if (args.Length == 4)
                        {
                            ubx.SetupBasePos(port,
                                             new PointLatLngAlt(double.Parse(args[1]), double.Parse(args[2]), double.Parse(args[3])),
                                             60, 2);
                        }
                        else
                        {
                            ubx.SetupBasePos(port, PointLatLngAlt.Zero, 60, 2);
                        }
                    }

                    if (file == null || !file.CanWrite || DateTime.UtcNow.Day != filetime.Day)
                    {
                        if (file != null)
                        {
                            file.Close();
                        }
                        string fn = "";
                        int    no = 0;
                        while (File.Exists((fn = DateTime.UtcNow.ToString("yyyy-MM-dd") + "-" + no + ".rtcm.Z")))
                        {
                            no++;
                        }

                        file = new GZipStream(new BufferedStream(new FileStream(fn,
                                                                                FileMode.OpenOrCreate)), CompressionMode.Compress);
                        filetime = DateTime.UtcNow;
                    }

                    var btr = port.BytesToRead;
                    if (btr > 0)
                    {
                        totalread += btr;
                        var buffer = new byte[btr];
                        btr = port.Read(buffer, 0, btr);

                        foreach (byte by in buffer)
                        {
                            btr--;

                            if (ubx.Read((byte)by) > 0)
                            {
                                rtcm.resetParser();
                                //Console.WriteLine(DateTime.Now + " new ubx message");
                            }
                            if (by >= 0 && rtcm.Read((byte)by) > 0)
                            {
                                ubx.resetParser();
                                //Console.WriteLine(DateTime.Now + " new rtcm message");
                                gotRTCMData?.Invoke(rtcm.packet, rtcm.length);
                                file.Write(rtcm.packet, 0, rtcm.length);
                            }
                            if ((by >= 0 && can.Read(by) > 0))// can_rtcm
                            {
                                ubx.resetParser();
                            }
                        }
                    }

                    if (DateTime.Now.Minute != everyminute)
                    {
                        Console.WriteLine("{0} bps", totalread / 60);
                        if (totalread == 0)
                        {
                            try
                            {
                                port.Close();
                                port = null;
                            }
                            catch (Exception ex)
                            {
                                port = null;
                            }
                        }
                        totalread   = 0;
                        everyminute = DateTime.Now.Minute;
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    Console.WriteLine(ex);
                    Thread.Sleep(5000);
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex);
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                Thread.Sleep(10);
            }

            if (file != null)
            {
                file.Close();
            }
        }