Ejemplo n.º 1
0
        protected override async Task <IModbusResponse> ReadResponseAsync <TResponse>(CancellationToken token = default)
        {
            var buffer = await PipeResource.ReadAsync(token).ConfigureAwait(false);

            while (buffer.Length < MbapHeaderSizeOnResponse)
            {
                PipeResource.AdvanceTo(buffer.Start);
                buffer = await PipeResource.ReadAsync(token).ConfigureAwait(false);
            }

            var frameLength = (ushort)IPAddress.HostToNetworkOrder(NetCoreBitConverter.ToInt16(buffer.Slice(4, 2).ToSpan()));

            while (buffer.Length < MbapHeaderSizeOnResponse + frameLength)
            {
                PipeResource.AdvanceTo(buffer.Start);
                buffer = await PipeResource.ReadAsync(token).ConfigureAwait(false);
            }

            var processedSequence = buffer.Slice(0, MbapHeaderSizeOnResponse + frameLength);

            var response = ModbusResponseFactory.CreateResponse <TResponse>(
                processedSequence.Slice(MbapHeaderSizeOnResponse, processedSequence.Length - MbapHeaderSizeOnResponse).ToSpan());

            response.TransactionId = (ushort)IPAddress.NetworkToHostOrder(NetCoreBitConverter.ToInt16(buffer.Slice(0, 2).ToSpan()));

            PipeResource.AdvanceTo(processedSequence.End);

            return(response);
        }
Ejemplo n.º 2
0
        public void NetCoreBitConverterToInt16ThrowsOnInvalidSpanSize()
        {
            // Arrange
            var array = new byte[1];

            // Act/Assert
            Assert.Throws <ArgumentOutOfRangeException>("value", () => NetCoreBitConverter.ToInt16(array));
        }
Ejemplo n.º 3
0
        public override void Initialize(ReadOnlySpan <byte> frame)
        {
            base.Initialize(frame);

            StartAddress = (ushort)IPAddress.NetworkToHostOrder(NetCoreBitConverter.ToInt16(frame.Slice(2, 2)));
        }