protected override async Task WriteRequestAsync(IModbusRequest request, CancellationToken token = default)
        {
            using var memoryOwner = MemoryPool <byte> .Shared.Rent(MbapHeaderSizeOnRequest + request.ByteSize);

            var memory = memoryOwner.Memory;

            BitConverter.TryWriteBytes(memory.Slice(0, 2).Span, IPAddress.HostToNetworkOrder((short)request.TransactionId));
            memory.Span[2] = 0;
            memory.Span[3] = 0;
            BitConverter.TryWriteBytes(memory.Slice(4, 2).Span, IPAddress.HostToNetworkOrder((short)(request.ByteSize + 1)));
            memory.Span[6] = request.SlaveAddress;

            request.WriteTo(memory.Slice(MbapHeaderSizeOnRequest, request.ByteSize));

            await PipeResource.WriteAsync(memory.Slice(0, MbapHeaderSizeOnRequest + request.ByteSize), token).ConfigureAwait(false);
        }
Example #2
0
        protected override async Task WriteRequestAsync(IModbusRequest request, CancellationToken token = default)
        {
            var totalSize = SlaveAddressSize + CrcSize + request.ByteSize;

            using var memoryOwner = MemoryPool <byte> .Shared.Rent(totalSize);

            var memory = memoryOwner.Memory;

            memory.Span[0] = request.SlaveAddress;
            request.WriteTo(memory.Slice(SlaveAddressSize, request.ByteSize));

            ushort crc = crcCalculator.Calculate(memory.Slice(0, SlaveAddressSize + request.ByteSize));

            BitConverter.TryWriteBytes(memory.Slice(SlaveAddressSize + request.ByteSize, CrcSize).Span, crc);

            await PipeResource.WriteAsync(memory.Slice(0, totalSize), token).ConfigureAwait(false);
        }