コード例 #1
0
 protected override bool RetryReadResponse(IModbusRequest request, IModbusResponse response)
 {
     // Do not retry for these on invalid function code or slave address
     return(request.FunctionCode == response.FunctionCode &&
            request.SlaveAddress == response.SlaveAddress &&
            request.TransactionId > response.TransactionId &&
            request.TransactionId - response.TransactionId < RetryOnOldResponseThreshold);
 }
コード例 #2
0
        protected override void Validate(IModbusRequest request, IModbusResponse response)
        {
            request.Validate(response);

            if (request.TransactionId != response.TransactionId)
            {
                throw new IOException($"Received unexpected transaction Id. Expected: {request.TransactionId}. Received: {response.TransactionId}.");
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
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);
        }
コード例 #5
0
 protected override void Validate(IModbusRequest request, IModbusResponse response)
 {
     request.Validate(response);
 }
コード例 #6
0
 protected override bool RetryReadResponse(IModbusRequest request, IModbusResponse response)
 {
     return(false);
 }
コード例 #7
0
 internal static void SetupValidate(this Mock <ModbusTransport> mock, IModbusRequest request, IModbusResponse response)
 {
     mock.Protected().As <IModbusTransportMock>().Setup(x => x.Validate(request, response));
 }
コード例 #8
0
        internal static void SetupRetryReadResponse(this Mock <ModbusTransport> mock, IModbusRequest request, IModbusResponse response, params bool[] results)
        {
            if (results.Length == 1)
            {
                mock.Protected()
                .As <IModbusTransportMock>()
                .Setup(x => x.RetryReadResponse(request, response))
                .Returns(results[0]);
                return;
            }

            var sequenceMock = mock.Protected()
                               .As <IModbusTransportMock>()
                               .SetupSequence(x => x.RetryReadResponse(request, response));

            foreach (var item in results)
            {
                sequenceMock
                .Returns(item);
            }
        }
コード例 #9
0
 public void ValidatesResponse(IModbusRequest request, IModbusResponse response)
 {
     // Arrange/Act/Assert
     Assert.Throws <IOException>(() => request.Validate(response));
 }