Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
 private void HandleCustomMessage(object sender, GpsMessageEventArgs e)
 {
     OnCustomMessage?.Invoke(this, e);
 }