private void HandleStandardMessage(object sender, StandardGpsMessageEventArgs e) { switch (e.Message.Kind) { case StandardGpsMessageKind.Rmc: HandleRmc((RmcStandardGpsMessage)e.Message); break; case StandardGpsMessageKind.Vtg: HandleVtg((VtgStandardGpsMessage)e.Message, e.SystemReceivedUtcDateTime); break; case StandardGpsMessageKind.Gga: HandleGga((GgaStandardGpsMessage)e.Message, e.SystemReceivedUtcDateTime); break; case StandardGpsMessageKind.Gsa: break; case StandardGpsMessageKind.Gsv: break; case StandardGpsMessageKind.Gll: break; case StandardGpsMessageKind.Txt: break; default: throw new ArgumentOutOfRangeException(); } OnStandardMessage?.Invoke(this, e); }
private async Task PerpetualGpsRead() { const uint chunkLength = 100; var text = new StringBuilder(); DateTime systemBeginReceiveMessageDateTime = DateTime.UtcNow; try { using (var dataReader = new DataReader(_serialDevice.InputStream)) { while (!_isDisposed) { uint bytesToRead = await dataReader.LoadAsync(chunkLength); if (bytesToRead == 0) { continue; } var buffer = new byte[bytesToRead]; dataReader.ReadBytes(buffer); for (int i = 0; i < bytesToRead; i++) { char c = (char)buffer[i]; if (c == '\n') { string packetText = text.ToString(); text.Clear(); //Debug.WriteLine($"Raw message: \"{packetText.Trim()}\""); GpsMessage message; if (GpsMessageSerializer.TryDeserialize(packetText, out message)) { StandardGpsMessage standardGpsMessage; if (StandardGpsMessageConverter.TryConvertToStandardGpsMessage(message, out standardGpsMessage)) { OnStandardMessage?.Invoke(this, new StandardGpsMessageEventArgs(standardGpsMessage, systemBeginReceiveMessageDateTime)); } else { OnCustomMessage?.Invoke(this, new GpsMessageEventArgs(message, systemBeginReceiveMessageDateTime)); } } else { Debug.WriteLine($"Invalid GPS message: \"{packetText}\""); } } else { if (text.Length == 0) { systemBeginReceiveMessageDateTime = DateTime.UtcNow; } text.Append(c); } } } } } catch { if (_isDisposed) { return; } throw; } finally { _serialDevice.Dispose(); } }