public bool OpenPort() { try { Console.WriteLine(@"Navigation Module is Intializing. Please wait...."); _serialPort.Open(); Task readThread = Task.Run(() => { while (true) { string message = @""; try { if (_serialPort.BytesToRead > 0) { message = _serialPort.ReadLine(); } } catch { continue; } var tokens = message.Split(','); if (tokens[0].StartsWith("$GPGGA")) { if (!String.IsNullOrEmpty(tokens[2]) && !String.IsNullOrEmpty(tokens[4])) { Debug.WriteLine(message); Latitude = new LatitudeLongitude(tokens[2], tokens[3]); Longitude = new LatitudeLongitude(tokens[4], tokens[5]); Debug.WriteLine(@"Parsed Latitude and Longitude are {0},{1}", Latitude.ToString(), Longitude.ToString()); } } if (tokens[0].StartsWith("$GPRMC")) { if (!String.IsNullOrEmpty(tokens[7])) { Debug.WriteLine(message); Speed = new GpsSpeed(tokens[7]); Debug.WriteLine(@"Current Speed is {0},{1}", Speed.ToString()); } } } }); return(true); } catch (Exception e) { Console.WriteLine("The port may be in use - {0}", e.Message); return(false); } }
public bool OpenPort() { try { Console.WriteLine(@"Navigation Module is Intializing. Please wait...."); _serialPort.Open(); Task readThread = Task.Run(() => { while (true) { string message = @""; try { if (_serialPort.BytesToRead > 0) { message = _serialPort.ReadLine(); } } catch { continue; } var tokens = message.Split(','); if (tokens[0].StartsWith("$GPGGA")) { if (!String.IsNullOrEmpty(tokens[2]) && !String.IsNullOrEmpty(tokens[4])) { Debug.WriteLine(message); Latitude = new LatitudeLongitude(tokens[2], tokens[3]); Longitude = new LatitudeLongitude(tokens[4], tokens[5]); Debug.WriteLine(@"Parsed Latitude and Longitude are {0},{1}", Latitude.ToString(), Longitude.ToString()); } } if (tokens[0].StartsWith("$GPRMC")) { if (!String.IsNullOrEmpty(tokens[7])) { Debug.WriteLine(message); Speed = new GpsSpeed(tokens[7]); Debug.WriteLine(@"Current Speed is {0},{1}", Speed.ToString()); } } } }); return true; } catch (Exception e) { Console.WriteLine("The port may be in use - {0}", e.Message); return false; } }