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); }
public void NetCoreBitConverterToInt16ThrowsOnInvalidSpanSize() { // Arrange var array = new byte[1]; // Act/Assert Assert.Throws <ArgumentOutOfRangeException>("value", () => NetCoreBitConverter.ToInt16(array)); }
public override void Initialize(ReadOnlySpan <byte> frame) { base.Initialize(frame); StartAddress = (ushort)IPAddress.NetworkToHostOrder(NetCoreBitConverter.ToInt16(frame.Slice(2, 2))); }