private void InitializeGpsVariables() { //Set up delegates for each string type to be used m_DelegateParseGSV = new DelegateParseGSV(ParseGSV); m_DelegateParseGGA = new DelegateParseGGA(ParseGGA); m_DelegateParseGSA = new DelegateParseGSA(ParseGSA); m_DelegateParseRMC = new DelegateParseRMC(ParseRMC); //Add the delegates to the dictionary to be called for each type ParsingFunctions = new Dictionary<NmeaString, Delegate>(); ParsingFunctions.Add(NmeaString.GSV, m_DelegateParseGSV); ParsingFunctions.Add(NmeaString.GSA, m_DelegateParseGSA); ParsingFunctions.Add(NmeaString.GGA, m_DelegateParseGGA); ParsingFunctions.Add(NmeaString.RMC, m_DelegateParseRMC); Error = GpsErrorType.NoError; _bursts = 0; _UseFile = false; FileOpen = false; GpsFile = String.Empty; }
//GPS Connection and Read private void GpsWorker(object o) { _UseFile = false; NmeaBurst burst = new NmeaBurst(); _bursts = 0; try { #region File if (!Port.ToLower().Contains("com")) { _UseFile = true; GpsFile = Port; try { try { sr = new StreamReader(GpsFile); FileOpen = true; } catch(Exception ex) { TtUtils.WriteError(ex.Message, String.Format("GpsAccess:GpsWorker:OpenFile [{0}]", GpsFile), ex.StackTrace); Error = GpsErrorType.FileOpenError; if (GpsError != null) GpsError(); Cancel(); return; } if (sr.Peek() < 1) { Error = GpsErrorType.FileNull; if (GpsError != null) GpsError(); Cancel(); return; } while (true) { if (_cancelled) { Cancel(); return; } try { sentence = sr.ReadLine(); if (sentence == null) { sr.Close(); FileOpen = false; sr = new StreamReader(GpsFile); FileOpen = true; sentence = sr.ReadLine(); } sentence = sentence.Trim(); } catch (Exception ex) { TtUtils.WriteError(ex.Message, "GpsAccess:GpsWorker:Readline", ex.StackTrace); Error = GpsErrorType.FileReadError; if (GpsError != null) GpsError(); Cancel(); return; } //create new burst if no burst or prev burst was completed if (burst == null || burst.complete) { burst = new NmeaBurst(); } ParseSentence(sentence, ref burst); //if all lines are parsed mark as complete if (burst != null) { if (burst.bGGA && burst.bGSA && burst.bGSV && burst.bRMC) burst.Complete(); } //trigger complete burst completed #region Trigger NmeaBurstReceived Event if (burst != null && !_cancelled && burst.complete) { _bursts++; try { OnBurstReceived(burst); } catch (Exception ex) { TtUtils.WriteError(ex.Message, "GpsAccess:GpsWorker:BurstReceived:Delegate", ex.StackTrace); Error = GpsErrorType.BurstDelegateError; if (GpsError != null) GpsError(); Cancel(); return; } Thread.Sleep(1000); } #endregion } } catch (IOException ioEx) { TtUtils.WriteError(ioEx.Message, "GpsAccess:GpsWorker(IO)", ioEx.StackTrace); Error = GpsErrorType.UnknownFileError; if (GpsError != null) GpsError(); Cancel(); } catch (Exception ex) { TtUtils.WriteError(ex.Message, "GpsAccess:GpsWorker", ex.StackTrace); Error = GpsErrorType.UnknownError; if (GpsError != null) GpsError(); Cancel(); } } #endregion #region GpsDevice else { _port = new SerialPort(Port, Rate); _port.Parity = Parity.None; _port.StopBits = StopBits.One; _port.DataBits = 8; _port.Handshake = Handshake.None; _port.ReadTimeout = Values.Settings.DeviceOptions.GpsTimeout; _port.Open(); ignoreFirst = true; //ignore first GPS string (bad data) #region Trigger Event //Trigger Event if (GpsStarted != null) { GpsStarted(); } #endregion if (_test) { sentence = _port.ReadLine().TrimEx(); _test = false; } else { sentence = SyncGps().TrimEx(); } while (true) { //create new burst if no burst or prev burst was completed if (burst == null || burst.complete) { burst = new NmeaBurst(); } ParseSentence(sentence, ref burst); if(burst != null) { if (burst.bGGA && burst.bGSA && burst.bGSV && burst.bRMC) burst.Complete(); #region Trigger NmeaBurstReceived Event if (burst.complete && BurstReceived != null && !_cancelled) { _bursts++; try { if (burst.IsValidNmea) { OnBurstReceived(burst); } else { burst.drop = true; OnBurstReceived(burst); burst = null; } } catch (Exception ex) { TtUtils.WriteError(ex.Message, "GpsAccess:BurstReceived:Delegate", ex.StackTrace); } } #endregion } if (_cancelled) { Cancel(); return; } sentence = _port.ReadLine().TrimEx(); } } } #endregion #region Catch Exceptions catch (TimeoutException toEx) { Error = GpsErrorType.GpsTimeout; TtUtils.WriteError(toEx.Message, "GpsAccess", toEx.StackTrace); #region trigger ComTimeout event if (ComTimeout != null) ComTimeout(); #endregion CloseGpsPort(); } catch(System.IO.IOException ioEx) { Error = GpsErrorType.ComNotExist; TtUtils.WriteError(ioEx.Message, "GpsAccess", ioEx.StackTrace); #region trigger Error event if (GpsError != null) GpsError(); #endregion CloseGpsPort(); } catch (Exception ex) { Error = GpsErrorType.UnknownError; TtUtils.WriteError(ex.Message, "GpsAccess", ex.StackTrace); #region trigger Error event if (GpsError != null) GpsError(); #endregion CloseGpsPort(); } #endregion }