Esempio n. 1
0
        private async Task SoftReset()
        {
            // INS12350-Serial-API-Host-Appl.-Prg.-Guide | 6.1 Initializatio
            // 1) Close host serial port if it is open
            // 2) Open the host serial port at 115200 baud 8N1.
            // 3) Send the NAK
            // 4) Send SerialAPI command: FUNC_ID_SERIAL_API_SOFT_RESET
            // 5) Wait 1.5s

            var message = new RequestMessage(new Payload((byte)Function.SerialApiSoftReset));
            var frame   = MessageBroker.Encode(message);

            var writer = new FrameWriter(Port);
            await writer.Write(Frame.NAK, CancellationToken.None);

            await writer.Write(frame, CancellationToken.None);

            await Task.Delay(1500);
        }
        private void WriteAndRead(FrameWriter writer, FrameReader reader, Input.DirectX.Identifier input, Output.Joystick.Identifier output, Axis axis, double value)
        {
            writer.Write(new Dictionary <Output.IIdentifier, State>
            {
                { output, new State()
                  {
                      Axis = new Dictionary <Axis, double>
                      {
                          { axis, value }
                      }
                  } }
            });
            Thread.Sleep(1);
            var actual = reader.Read()[input].Axis[axis];

            Assert.IsTrue(value <= actual + 0.001 && actual - 0.001 <= value);
        }
Esempio n. 3
0
        public async Task WriteReadDataFrame()
        {
            var input  = new byte[] { 1, 12, 0, 4, 0, 15, 6, 49, 5, 4, 34, 0, 3, 239 };
            var stream = new MockDuplexStream();

            stream.Input.Write(input, 0, input.Length);
            stream.Input.Position = 0;
            var reader   = new FrameReader(stream);
            var response = (DataFrame)(await reader.Read(CancellationToken.None));

            var request = new DataFrame(DataFrameType.REQ, response.Payload);
            var writer  = new FrameWriter(stream);
            await writer.Write(request, CancellationToken.None);

            var output = stream.Output.ToArray();

            Assert.IsTrue(input.SequenceEqual(output));
        }
Esempio n. 4
0
        public async Task WriteReadAckFrame()
        {
            var stream = new MockDuplexStream();

            var writer  = new FrameWriter(stream);
            var request = Frame.ACK;
            await writer.Write(request, CancellationToken.None);

            var data = stream.Output.ToArray();
            await stream.Input.WriteAsync(data, 0, data.Length);

            stream.Input.Position = 0;

            var reader   = new FrameReader(stream);
            var response = await reader.Read(CancellationToken.None);

            Assert.AreEqual(request, response);
        }
Esempio n. 5
0
        private Task Execute(IObserver <Frame> observer, CancellationToken cancellationToken = default)
        {
            if (observer == null)
            {
                throw new ArgumentNullException(nameof(observer));
            }

            return(_task = Task.Run(async() =>
            {
                _logger.LogDebug("Started");

                // execute until externally cancelled
                while (!cancellationToken.IsCancellationRequested)
                {
                    var frame = default(Frame);
                    try
                    {
                        // read next frame
                        frame = await _reader.Read(cancellationToken);
                    }
                    catch (StreamClosedException ex)
                    {
                        // most likely removal of ZStick from host
                        _logger.LogDebug(ex.Message);

                        observer.OnError(ex);

                        return;
                    }
                    catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested)
                    {
                        observer.OnCompleted();

                        // the read was cancelled by the passed cancellationtoken so end gracefully
                        break;
                    }
                    catch (ChecksumException ex)
                    {
                        // checksum failure on received frame, might happen during startup when we
                        // are not synchronized and receive a partially frame
                        _logger.LogWarning(ex.Message);

                        // send NACK and hopefully the controller will send this frame again
                        _logger.LogDebug($"Writing: {Frame.NAK}");
                        await _writer.Write(Frame.NAK, cancellationToken);

                        // wait for next frame
                        continue;
                    }

                    //
                    if (frame == Frame.ACK || frame == Frame.NAK || frame == Frame.CAN)
                    {
                        if (frame == Frame.ACK)
                        {
                            _logger.LogDebug($"Received: {frame}");
                        }
                        else
                        {
                            _logger.LogWarning($"Received: {frame}");
                        }

                        // publish the frame
                        observer.OnNext(frame);

                        // wait for next frame
                        continue;
                    }

                    if (frame is DataFrame dataFrame)
                    {
                        _logger.LogDebug($"Received: {frame}");

                        // dataframes must be aknowledged
                        _logger.LogDebug($"Writing: {Frame.ACK}");
                        await _writer.Write(Frame.ACK, cancellationToken);

                        // publish the frame
                        observer.OnNext(dataFrame);

                        // wait for next frame
                        continue;
                    }
                }

                _logger.LogDebug("Completed");
            }, cancellationToken));
        }