public void Get_reader_serial_packet_data()
        {
            var pk  = new CommandDataPacket(ReaderCommand.GetReaderSerialNumber);
            var buf = pk.Serialize();

            buf.Length.Should().Be(5);
            buf[0].Should().Be((byte)4);
            buf[1].Should().Be((byte)0);
            buf[2].Should().Be((byte)ReaderCommand.GetReaderSerialNumber);
            buf[3].Should().Be((byte)0x3a);
            buf[4].Should().Be((byte)0xd2);
        }
Beispiel #2
0
        public async Task <IEnumerable <ResponseDataPacket> > SendReceive(CommandDataPacket command)
        {
            using (sendReceiveSemaphore.UseOnce())
            {
                try
                {
                    var port   = streamFactory.DataStream;
                    var buffer = command.Serialize();
                    await port.WriteAsync(buffer, 0, buffer.Length);

                    var responsePackets = new List <ResponseDataPacket>();
                    ResponseDataPacket lastResponse;
                    do
                    {
                        var packet = await MessageParser.ReadPacket(port);

                        //TODO: Receive failures should be handled by reopening port.
                        if (!packet.Success)
                        {
                            throw new ReceiveFailedException(
                                      $"Failed to read response from {streamFactory.Description} {packet.ResultType}");
                        }
                        responsePackets.Add(lastResponse = new ResponseDataPacket(command.Command, packet.Data));
                    } while (MessageParser.ShouldReadMore(lastResponse));

                    return(responsePackets);
                }
                catch (ReceiveFailedException)
                {
                    Dispose();
                    throw;
                }
                catch (NotSupportedException)
                {
                    Dispose();
                    throw;
                }
            }
        }
Beispiel #3
0
        public async Task <IEnumerable <ResponseDataPacket> > SendReceive(CommandDataPacket command)
        {
            using (sendReceiveSemaphore.UseOnce())
            {
                try
                {
                    var port   = streamFactory.DataStream;
                    var buffer = command.Serialize();
                    var sw     = Stopwatch.StartNew();
                    await port.WriteAsync(buffer, 0, buffer.Length);

                    var responsePackets = new List <ResponseDataPacket>();
                    ResponseDataPacket lastResponse;
                    do
                    {
                        var packet = await MessageParser.ReadPacket(port, sw);

                        sw = null;
                        //TODO: Receive failures should be handled by reopening port.
                        if (!packet.Success)
                        {
                            throw new ReceiveFailedException(
                                      $"Failed to read response from {streamFactory.Description} {packet.ResultType}");
                        }
                        responsePackets.Add(lastResponse = new ResponseDataPacket(command.Command, packet.Data, elapsed: packet.Elapsed,
                                                                                  errorsObserver: ThrowOnIllegalCommandError ? null : errors));
                    } while (MessageParser.ShouldReadMore(lastResponse));

                    return(responsePackets);
                }
                catch
                {
                    Dispose();
                    throw;
                }
            }
        }