Пример #1
0
        private void UpdateGps()
        {
            int timeoutCounter = 0;

            _terminate.Reset();

            //Infinite loop!
            while (true)
            {
                try
                {
                    int eventIdx = WaitHandle.WaitAny(_waitHandles, 100, false);
                    if (eventIdx == WaitHandle.WaitTimeout)
                    {
                        if (_port != null && _port.IsOpen)
                        {
                            string sentence = string.Empty;
                            try
                            {
                                //Console.WriteLine(_port.ReadExisting());
                                sentence       = _port.ReadLine();
                                timeoutCounter = 0;
                                HasTalked      = true;
                                TimedOut       = false;
                            }
                            //TODO: Find out why ArgumentOutOfRangeException gets thrown and catch if necessary
                            catch (ArithmeticException)
                            {
                                // TODO: wtf??
                            }
                            catch (TimeoutException)
                            {
                                //TODO: make GPS keep listening and just notify that no GPS is found, check cables and make sure the GPS is powered on
                                // Give up after the maximum timeout is reached
                                // The read timeout is 500ms, so the number of timeouts to seconds is a ratio of 2 timeouts for 1 second
                                if (!HasTalked && MaxTimeout <= timeoutCounter)
                                {
                                    InvokeGpsTimeout();
                                    //Reset the timeout count
                                    //Stop();
                                    break;
                                }
                                timeoutCounter++;
                            }
                            catch (IOException)
                            {
                                ClosePort();
                                break;
                            }

                            //Process sentence
                            NmeaParser.SentenceType type = _nmea.Parse(sentence);

                            switch (type)
                            {
                            case NmeaParser.SentenceType.Gprmc:
                                MyGpsData.Latitude      = _nmea.Latitude;
                                MyGpsData.Longitude     = _nmea.Longitude;
                                MyGpsData.Speed         = _nmea.Speed;
                                MyGpsData.SatelliteTime = _nmea.SatelliteTime;
                                MyGpsData.Course        = _nmea.Course;
                                MyGpsData.MagVar        = _nmea.MagVar;

                                HasFix = _nmea.HasFix;
                                Time   = _nmea.Timestamp;

                                InvokeGpsLocationUpdated();
                                break;

                            case NmeaParser.SentenceType.Gpgsv:
                                Satellites        = _nmea.Satellites;
                                SatellitesVisible = _nmea.SatelliteCount;
                                InvokeGpsStatUpdated();
                                break;

                            case NmeaParser.SentenceType.Gpgsa:
                                MyGpsData.Pdop           = _nmea.Pdop;
                                MyGpsData.Hdop           = _nmea.Hdop;
                                MyGpsData.Vdop           = _nmea.Vdop;
                                MyGpsData.SatellitesUsed = _nmea.SatellitesUsed;

                                InvokeGpsStatUpdated();
                                break;

                            case NmeaParser.SentenceType.Gpgga:
                                MyGpsData.Latitude  = _nmea.Latitude;
                                MyGpsData.Longitude = _nmea.Longitude;
                                MyGpsData.Altitude  = _nmea.Altitude;

                                Time = _nmea.Timestamp;

                                InvokeGpsLocationUpdated();
                                break;

                            case NmeaParser.SentenceType.Gpvtg:
                                MyGpsData.Speed = _nmea.Speed;
                                break;

                            default:
                                //gpsConnected = false;
                                break;
                            }
                        }
                        else
                        {
                            // The port was previously closed due to being out of sync, reopen
                            Thread.Sleep(500);
                            ClosePort();
                            OpenPort();
                        }

                        InvokeGpsUpdated();
                    }
                    else //Terminate was signaled
                    {
                        break;
                    }
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
            }
        }