/// <summary> /// Open a GPS serial port. /// </summary> /// <param name="comPort"></param> /// <param name="baudRate"></param> /// <returns>true for success</returns> public bool Open(int comPort, int baudRate) { if (_serialPort != null) { Close(); } if (!ValidBaudRate(baudRate)) { throw new System.ArgumentException(Resources.InvalidBaudRate, "baudRate"); } _serialPort = new SerialPort("COM" + comPort.ToString(CultureInfo.InvariantCulture), baudRate, Parity.None, 8, StopBits.One); _serialPort.Handshake = Handshake.None; _serialPort.Encoding = Encoding.ASCII; _serialPort.NewLine = "\r\n"; _serialPort.ReadTimeout = 1100; if (_gpsDataPort == null) { _gpsDataPort = new GpsDataPort(); } bool serialPortOpened = false; try { if (comPort > 0) { if (TrySerialPort(_serialPort.PortName, baudRate)) { _serialPort.Open(); serialPortOpened = true; _serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); } } } catch { if (serialPortOpened) { serialPortOpened = false; _serialPort.DataReceived -= new SerialDataReceivedEventHandler(serialPort_DataReceived); } } return(serialPortOpened); }
/// <summary> /// Open a Gps Log file and return output to the Gps Service /// </summary> /// <param name="fileName"></param> /// <returns></returns> public bool Open(string fileName) { if (_serialPort != null) { Close(); } if (_gpsDataPort == null) { _gpsDataPort = new GpsDataPort(); } if (File.Exists(fileName)) { StreamReader r = File.OpenText(fileName); while (!r.EndOfStream) { string line = r.ReadLine().Trim(); int ix = line.IndexOf('*'); if (ix < 0) { _gpsDataPort.Post(new InvalidDataException(Resources.InvalidGpsStream)); } else { if (ValidChecksum(line)) { string[] fields = line.Substring(0, ix).Split(','); _gpsDataPort.Post(fields); } else { _gpsDataPort.Post(new InvalidDataException(Resources.FailedChecksumValidation + line)); } } } r.Close(); return(true); } return(false); }
/// <summary> /// Default GpsConnection constructor /// </summary> /// <param name="config"></param> /// <param name="gpsDataPort"></param> public GpsConnection(MicrosoftGpsConfig config, GpsDataPort gpsDataPort) { this._config = config; this._gpsDataPort = gpsDataPort; }