Example #1
0
        private void Send(Packet packet)
        {
#if DEBUG
            using (var stream = new MemoryStream())
            {
                Serializer.Serialize(stream, Constants.SyncCharacters);
                Serializer.Serialize(stream, packet);

                var data = stream.ToArray();

                Debug.WriteLine($"Sending: {BitConverter.ToString(data)}");
            }
#endif

            using (var stream = new SerialDeviceStream(_serialDevice))
            {
                Serializer.Serialize(stream, Constants.SyncCharacters);
                Serializer.Serialize(stream, packet);
            }
        }
Example #2
0
        private async void ListenAsync(bool once = false)
        {
            var cancellationToken = _listenCancellationTokenSource.Token;

            do
            {
                using (var stream = new SerialDeviceStream(_serialDevice))
                {
                    var sync1 = new byte[1];
                    var sync2 = new byte[1];

                    while (sync2[0] != 0x62)
                    {
                        while (sync1[0] != 0xb5)
                        {
                            await stream.ReadAsync(sync1, 0, sync1.Length, cancellationToken);
                        }

                        await stream.ReadAsync(sync2, 0, sync2.Length, cancellationToken);
                    }

                    Debug.WriteLine("Found sync");

                    var packet = await Serializer.DeserializeAsync <Packet>(stream, cancellationToken)
                                 .ConfigureAwait(false);

                    Debug.WriteLine($"Recieved {packet.Content.MessageId}");

                    var messageId = packet.Content.MessageId;
                    switch (messageId)
                    {
                    case MessageId.ACK_ACK:
                    case MessageId.ACK_NAK:
                        _commandCompletionSource?.SetResult(packet);
                        break;

                    case MessageId.MON_VER:
                    {
                        var monVer           = (MonVer)packet.Content.Payload;
                        var extensions       = monVer.Extensions;
                        var protVerExtension =
                            extensions.FirstOrDefault(
                                extension => extension.Value.StartsWith(ProtocolVersionExtensionPrefix));

                        if (protVerExtension != null)
                        {
                            var extensionParts  = protVerExtension.Value.Split(' ');
                            var protocolVersion = extensionParts[1];
                            //_protocolVersionTaskCompletionSource.SetResult(true);
                        }

                        break;
                    }

                    case MessageId.RXM_RAWX:
                    {
                        RawDataProductVariantUpdated?.Invoke(this, new RawMeasurementDataEventArgs((RxmRawx)packet.Content.Payload));
                        break;
                    }

                    case MessageId.NAV_PVT:
                    {
                        PositionVelocityTimeUpdated?.Invoke(this,
                                                            new PositionVelocityTimeEventArgs((NavPvt)packet.Content.Payload));

                        break;
                    }

                    case MessageId.HNR_PVT:
                    {
                        HighNavRatePositionVelocityTimeUpdated?.Invoke(this,
                                                                       new HighNavRatePositionVelocityTimeEventArgs((HnrPvt)packet.Content.Payload));

                        break;
                    }
                    }
                    RawPacketHandler?.Invoke(this, new RawPacketHandlerEventArgs(messageId, packet.Content.Payload));
                }
            } while (!cancellationToken.IsCancellationRequested && !once);
        }