Example #1
0
 public void ReadDiscretesInputs()
 {
     ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadInputs, 1, 2, new DiscreteCollection(true, false, true, false, true, false, true, false, true));
     ReadCoilsInputsResponse response = ModbusSlave.ReadDiscretes(new ReadCoilsInputsRequest(Modbus.ReadInputs, 1, 1, 9), _testDataStore, _testDataStore.InputDiscretes);
     AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
     Assert.AreEqual(expectedResponse.ByteCount, response.ByteCount);
 }
        public void ToString_Coils()
        {
            ReadCoilsInputsResponse response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 5, 2,
                                                                           new DiscreteCollection(true, true, true, true, true, true, false, false, true, true, false));

            Assert.Equal((string)"Read 11 coils - {1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0}.", (string)response.ToString());
        }
Example #3
0
        internal bool[] ReadDiscretes(byte functionCode, byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {
            ReadCoilsInputsRequest  request  = new ReadCoilsInputsRequest(functionCode, slaveAddress, startAddress, numberOfPoints);
            ReadCoilsInputsResponse response = Transport.UnicastMessage <ReadCoilsInputsResponse>(request);

            return(response.Data.Slice(0, request.NumberOfPoints).ToArray());
        }
        internal static ReadCoilsInputsResponse ReadDiscretes(ReadCoilsInputsRequest request, ModbusDataCollection<bool> dataSource)
        {
            DiscreteCollection data = DataStore.ReadData<DiscreteCollection, bool>(dataSource, request.StartAddress, request.NumberOfPoints);
            ReadCoilsInputsResponse response = new ReadCoilsInputsResponse(request.FunctionCode, request.SlaveAddress, data.ByteCount, data);

            return response;
        }
        public void CreateModbusMessageReadCoilsResponseWithInvalidDataSize()
        {
            byte[] frame = { 11, Modbus.ReadCoils, 4, 1, 2, 3 };
            ReadCoilsInputsResponse response = ModbusMessageFactory.CreateModbusMessage <ReadCoilsInputsResponse>(frame);

            Assert.Fail();
        }
Example #6
0
        public void ProtocolDataUnitReadCoilsResponse()
        {
            ModbusMessageWithData <DiscreteCollection> message = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 2, new DiscreteCollection(true));

            byte[] expectedResult = { 1, 2, 1 };
            Assert.AreEqual(expectedResult, message.ProtocolDataUnit);
        }
Example #7
0
        public void ReadResponse()
        {
            MockRepository     mocks     = new MockRepository();
            ModbusRtuTransport transport =
                mocks.PartialMock <ModbusRtuTransport>(MockRepository.GenerateStub <IStreamResource>());

            Expect.Call(transport.Read(ModbusRtuTransport.ResponseFrameStartLength))
            .Return(new byte[] { 1, 1, 1, 0 });

            Expect.Call(transport.Read(2))
            .Return(new byte[] { 81, 136 });

            mocks.ReplayAll();

            ReadCoilsInputsResponse response =
                transport.ReadResponse <ReadCoilsInputsResponse>() as ReadCoilsInputsResponse;

            Assert.IsNotNull(response);
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1,
                                                                                   new DiscreteCollection(false));

            Assert.AreEqual(response.MessageFrame, expectedResponse.MessageFrame);

            mocks.VerifyAll();
        }
Example #8
0
        public void ModbusMessageWithDataFixtureCtorInitializesProperties()
        {
            ModbusMessageWithData <DiscreteCollection> message = new ReadCoilsInputsResponse(Modbus.ReadCoils, 10, 1, new DiscreteCollection(true, false, true));

            Assert.AreEqual(Modbus.ReadCoils, message.FunctionCode);
            Assert.AreEqual(10, message.SlaveAddress);
        }
        public void CreateModbusMessageReadCoilsResponseWithNoByteCount()
        {
            byte[] frame = { 11, Modbus.ReadCoils };
            ReadCoilsInputsResponse response = ModbusMessageFactory.CreateModbusMessage <ReadCoilsInputsResponse>(frame);

            Assert.Fail();
        }
 public void CreateModbusMessageReadCoilsResponse()
 {
     ReadCoilsInputsResponse response = ModbusMessageFactory.CreateModbusMessage<ReadCoilsInputsResponse>(new byte[] { 11, Modbus.ReadCoils, 1, 1 });
     ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 11, 1, new DiscreteCollection(true, false, false, false));
     AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
     Assert.AreEqual(expectedResponse.Data.NetworkBytes, response.Data.NetworkBytes);
 }
        public void UnicastMessage()
        {
            MockRepository  mocks     = new MockRepository();
            ModbusTransport transport = mocks.PartialMock <ModbusTransport>();

            transport.Write(null);
            LastCall.IgnoreArguments();
            // read 4 coils from slave id 2
            Expect.Call(transport.ReadResponse <ReadCoilsInputsResponse>())
            .Return(new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1,
                                                new DiscreteCollection(true, false, true, false, false, false, false, false)));
            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();

            ReadCoilsInputsRequest  request          = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 3, 4);
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1,
                                                                                   new DiscreteCollection(true, false, true, false, false, false, false, false));
            ReadCoilsInputsResponse response = transport.UnicastMessage <ReadCoilsInputsResponse>(request);

            Assert.Equal(expectedResponse.MessageFrame, response.MessageFrame);

            mocks.VerifyAll();
        }
Example #12
0
        internal static ReadCoilsInputsResponse ReadDiscretes(ReadCoilsInputsRequest request, DataStore dataStore, ModbusDataCollection <bool> dataSource)
        {
            DiscreteCollection      data     = DataStore.ReadData <DiscreteCollection, bool>(dataStore, dataSource, request.StartAddress, request.NumberOfPoints, dataStore.SyncRoot);
            ReadCoilsInputsResponse response = new ReadCoilsInputsResponse(request.FunctionCode, request.SlaveAddress, data.ByteCount, data);

            return(response);
        }
        public void ToString_Inputs()
        {
            ReadCoilsInputsResponse response = new ReadCoilsInputsResponse(Modbus.ReadInputs, 5, 2,
                new DiscreteCollection(true, true, true, true, true, true, false, false, true, true, false));

            Assert.AreEqual("Read 11 inputs - {1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0}.", response.ToString());
        }
 public void DataReadCoilsResponse()
 {
     DiscreteCollection col = new DiscreteCollection(false, true, false, true, false, true, false, false, false, false);
     ModbusMessageWithData<DiscreteCollection> message = new ReadCoilsInputsResponse(Modbus.ReadCoils, 11, 1, col);
     Assert.AreEqual(col.Count, message.Data.Count);
     Assert.AreEqual(col.NetworkBytes, message.Data.NetworkBytes);
 }
Example #15
0
        public void ToString_Inputs()
        {
            ReadCoilsInputsResponse response = new ReadCoilsInputsResponse(Modbus.ReadInputs, 5, 2,
                                                                           new DiscreteCollection(true, true, true, true, true, true, false, false, true, true, false));

            Assert.AreEqual("Read 11 inputs - {1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0}.", response.ToString());
        }
 public void ModbusMessageWithDataFixtureCtorInitializesProperties()
 {
     AbstractModbusMessageWithData<DiscreteCollection> message = new ReadCoilsInputsResponse(Modbus.ReadCoils, 10, 1,
         new DiscreteCollection(true, false, true));
     Assert.Equal(Modbus.ReadCoils, message.FunctionCode);
     Assert.Equal(10, message.SlaveAddress);
 }
 public void ProtocolDataUnitReadCoilsResponse()
 {
     AbstractModbusMessageWithData<DiscreteCollection> message = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 2,
         new DiscreteCollection(true));
     byte[] expectedResult = {1, 2, 1};
     Assert.Equal(expectedResult, message.ProtocolDataUnit);
 }
Example #18
0
        public void DataReadCoilsResponse()
        {
            DiscreteCollection col = new DiscreteCollection(false, true, false, true, false, true, false, false, false, false);
            ModbusMessageWithData <DiscreteCollection> message = new ReadCoilsInputsResponse(Modbus.ReadCoils, 11, 1, col);

            Assert.AreEqual(col.Count, message.Data.Count);
            Assert.AreEqual(col.NetworkBytes, message.Data.NetworkBytes);
        }
        public void CreateModbusMessageReadCoilsResponse()
        {
            ReadCoilsInputsResponse response         = ModbusMessageFactory.CreateModbusMessage <ReadCoilsInputsResponse>(new byte[] { 11, Modbus.ReadCoils, 1, 1 });
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 11, 1, new DiscreteCollection(true, false, false, false));

            AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
            Assert.AreEqual(expectedResponse.Data.NetworkBytes, response.Data.NetworkBytes);
        }
Example #20
0
        public bool[] GetReadInputsResponse(byte[] readBytes, int numberOfPoints, IModbusMessage request)
        {
            ReadCoilsInputsResponse response = (ReadCoilsInputsResponse)Transport.CreateResponse <ReadCoilsInputsResponse>(readBytes);

            Transport.ValidateResponse(request, response);

            return(response.Data.Take(numberOfPoints).ToArray());
        }
Example #21
0
        public void ReadDiscretesInputs()
        {
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadInputs, 1, 2, new DiscreteCollection(true, false, true, false, true, false, true, false, true));
            ReadCoilsInputsResponse response         = ModbusSlave.ReadDiscretes(new ReadCoilsInputsRequest(Modbus.ReadInputs, 1, 1, 9), _testDataStore, _testDataStore.InputDiscretes);

            AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
            Assert.AreEqual(expectedResponse.ByteCount, response.ByteCount);
        }
        public void ShouldRetryResponse_ReturnsFalse_IfDifferentMessage()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub <IStreamResource>());
            IModbusMessage    request   = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 1, 1);
            IModbusMessage    response  = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            Assert.False(transport.ShouldRetryResponse(request, response));
        }
 public void CreateReadInputsResponse()
 {
     ReadCoilsInputsResponse response = new ReadCoilsInputsResponse(Modbus.ReadInputs, 5, 2, new DiscreteCollection(true, true, true, true, true, true, false, false, true, true, false));
     Assert.AreEqual(Modbus.ReadInputs, response.FunctionCode);
     Assert.AreEqual(5, response.SlaveAddress);
     Assert.AreEqual(2, response.ByteCount);
     DiscreteCollection col = new DiscreteCollection(true, true, true, true, true, true, false, false, true, true, false);
     Assert.AreEqual(col.NetworkBytes, response.Data.NetworkBytes);
 }
 public void CreateResponse()
 {
     ModbusAsciiTransport transport = new ModbusAsciiTransport(MockRepository.GenerateStub<IStreamResource>());
     ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1, new DiscreteCollection(true, false, false, false, false, false, false, true));
     byte lrc = ModbusUtility.CalculateLrc(expectedResponse.MessageFrame);
     ReadCoilsInputsResponse response = transport.CreateResponse<ReadCoilsInputsResponse>(new byte[] { 2, Modbus.ReadCoils, 1, 129, lrc }) as ReadCoilsInputsResponse;
     Assert.IsNotNull(response);
     AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
 }
    public void ShouldRetryResponse_ReturnsFalse_IfDifferentMessage()
    {
        Mock <ModbusTransport>?mock      = new(MockBehavior.Strict) { CallBase = true };
        ModbusTransport?       transport = mock.Object;

        IModbusMessage request  = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 1, 1);
        IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

        Assert.False(transport.ShouldRetryResponse(request, response));
    }
Example #26
0
        public void CreateResponse()
        {
            var            transport        = new ModbusAsciiTransport(StreamResource, new ModbusFactory(), NullModbusLogger.Instance);
            var            expectedResponse = new ReadCoilsInputsResponse(ModbusFunctionCodes.ReadCoils, 2, 1, new DiscreteCollection(true, false, false, false, false, false, false, true));
            byte           lrc      = ModbusUtility.CalculateLrc(expectedResponse.MessageFrame);
            IModbusMessage response = transport.CreateResponse <ReadCoilsInputsResponse>(new byte[] { 2, ModbusFunctionCodes.ReadCoils, 1, 129, lrc });

            Assert.IsType <ReadCoilsInputsResponse>(response);
            ModbusMessageFixture.AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
        }
        public void CreateResponse()
        {
            ModbusAsciiTransport    transport        = new ModbusAsciiTransport(MockRepository.GenerateStub <IStreamResource>());
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1, new DiscreteCollection(true, false, false, false, false, false, false, true));
            byte lrc = ModbusUtility.CalculateLrc(expectedResponse.MessageFrame);
            ReadCoilsInputsResponse response = transport.CreateResponse <ReadCoilsInputsResponse>(new byte[] { 2, Modbus.ReadCoils, 1, 129, lrc }) as ReadCoilsInputsResponse;

            Assert.IsNotNull(response);
            AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
        }
        public void CreateReadInputsResponse()
        {
            ReadCoilsInputsResponse response = new ReadCoilsInputsResponse(Modbus.ReadInputs, 5, 2, new DiscreteCollection(true, true, true, true, true, true, false, false, true, true, false));

            Assert.AreEqual(Modbus.ReadInputs, response.FunctionCode);
            Assert.AreEqual(5, response.SlaveAddress);
            Assert.AreEqual(2, response.ByteCount);
            DiscreteCollection col = new DiscreteCollection(true, true, true, true, true, true, false, false, true, true, false);

            Assert.AreEqual(col.NetworkBytes, response.Data.NetworkBytes);
        }
Example #29
0
        public void OnShouldRetryResponse_ReturnsFalse_IfThresholdDisabled()
        {
            var transport = new ModbusIpTransport(StreamResourceMock.Object);
            var request   = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            var response  = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            request.TransactionId  = 5;
            response.TransactionId = 4;
            transport.RetryOnOldResponseThreshold = 0;

            Assert.False(transport.OnShouldRetryResponse(request, response));
        }
Example #30
0
        public void OnShouldRetryResponse_ReturnsFalse_IfOutsideThreshold()
        {
            var transport = new ModbusIpTransport(StreamResourceMock, new ModbusFactory(), NullModbusLogger.Instance);
            var request   = new ReadCoilsInputsRequest(ModbusFunctionCodes.ReadCoils, 1, 1, 1);
            var response  = new ReadCoilsInputsResponse(ModbusFunctionCodes.ReadCoils, 1, 1, null);

            request.TransactionId  = 5;
            response.TransactionId = 2;
            transport.RetryOnOldResponseThreshold = 3;

            Assert.False(transport.OnShouldRetryResponse(request, response));
        }
        public void OnShouldRetryResponse_ReturnsFalse_IfOutsideThreshold()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub <IStreamResource>());
            IModbusMessage    request   = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            IModbusMessage    response  = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            request.TransactionId  = 5;
            response.TransactionId = 2;
            transport.RetryOnOldResponseThreshold = 3;

            Assert.False(transport.OnShouldRetryResponse(request, response));
        }
        public void OnShouldRetryResponse_ReturnsFalse_IfOutsideThreshold()
        {
            var transport = new ModbusIpTransport(StreamResourceMock);
            var request   = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            var response  = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            request.TransactionId  = 5;
            response.TransactionId = 2;
            transport.RetryOnOldResponseThreshold = 3;

            Assert.False((bool)transport.OnShouldRetryResponse(request, response));
        }
        public void ValidateResponse_RequestAheadOfResponse_RequestsReread()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub <IStreamResource>());

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);

            request.TransactionId = 5;
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            response.TransactionId = 4;

            Assert.IsFalse(transport.ValidateResponse(request, response));
        }
Example #34
0
        public void ShouldRetryResponse_ReturnsFalse_IfDifferentMessage()
        {
            var mock = new Mock <ModbusTransport>(MockBehavior.Strict, new ModbusFactory(), NullModbusLogger.Instance)
            {
                CallBase = true
            };
            ModbusTransport transport = mock.Object;

            IModbusMessage request  = new ReadCoilsInputsRequest(ModbusFunctionCodes.ReadCoils, 2, 1, 1);
            IModbusMessage response = new ReadCoilsInputsResponse(ModbusFunctionCodes.ReadCoils, 1, 1, null);

            Assert.False(transport.ShouldRetryResponse(request, response));
        }
Example #35
0
        public void ValidateResponse_MismatchingTransactionIds()
        {
            var transport = new ModbusIpTransport(StreamResourceMock, new ModbusFactory(), NullModbusLogger.Instance);

            var request = new ReadCoilsInputsRequest(ModbusFunctionCodes.ReadCoils, 1, 1, 1);

            request.TransactionId = 5;
            var response = new ReadCoilsInputsResponse(ModbusFunctionCodes.ReadCoils, 1, 1, null);

            response.TransactionId = 6;

            Assert.Throws <IOException>(() => transport.ValidateResponse(request, response));
        }
        public void ValidateResponse_MismatchingTransactionIds()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub <IStreamResource>());

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);

            request.TransactionId = 5;
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            response.TransactionId = 6;

            Assert.Throws <IOException>(() => transport.ValidateResponse(request, response));
        }
Example #37
0
        public void ValidateResponse_MismatchingTransactionIds()
        {
            var transport = new ModbusIpTransport(StreamResourceMock.Object);

            var request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);

            request.TransactionId = 5;
            var response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            response.TransactionId = 6;

            Assert.Throws <IOException>(() => transport.ValidateResponse(request, response));
        }
Example #38
0
        public void ValidateResponse()
        {
            var transport = new ModbusIpTransport(StreamResourceMock, new ModbusFactory(), NullModbusLogger.Instance);

            var request = new ReadCoilsInputsRequest(ModbusFunctionCodes.ReadCoils, 1, 1, 1);

            request.TransactionId = 5;
            var response = new ReadCoilsInputsResponse(ModbusFunctionCodes.ReadCoils, 1, 1, null);

            response.TransactionId = 5;

            // no exception is thrown
            transport.ValidateResponse(request, response);
        }
        public void ValidateResponse()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub <IStreamResource>());

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);

            request.TransactionId = 5;
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            response.TransactionId = 5;

            // no exception is thrown
            transport.ValidateResponse(request, response);
        }
        public void UnicastMessage_PurgeReceiveBuffer()
        {
            MockRepository mocks = new MockRepository();
            IStreamResource serialResource = mocks.StrictMock<IStreamResource>();
            ModbusSerialTransport transport = new ModbusRtuTransport(serialResource);

            serialResource.DiscardInBuffer();
            serialResource.Write(null, 0, 0);
            LastCall.IgnoreArguments();

            // mangled response
            Expect.Call(serialResource.Read(new byte[] {0, 0, 0, 0}, 0, 4)).Return(4);

            serialResource.DiscardInBuffer();
            serialResource.Write(null, 0, 0);
            LastCall.IgnoreArguments();

            // normal response
            ReadCoilsInputsResponse response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1,
                new DiscreteCollection(true, false, true, false, false, false, false, false));

            // read header
            Expect.Call(serialResource.Read(new byte[] {0, 0, 0, 0}, 0, 4))
                .Do(((Func<byte[], int, int, int>) delegate(byte[] buf, int offset, int count)
               {
                   Array.Copy(response.MessageFrame, 0, buf, 0, 4);
                   return 4;
               }));

            // read remainder
            Expect.Call(serialResource.Read(new byte[] {0, 0}, 0, 2))
                .Do(((Func<byte[], int, int, int>) delegate(byte[] buf, int offset, int count)
               {
                   Array.Copy(ModbusUtility.CalculateCrc(response.MessageFrame), 0, buf, 0, 2);
                   return 2;
               }));

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 3, 4);
            transport.UnicastMessage<ReadCoilsInputsResponse>(request);

            mocks.VerifyAll();
        }
        public void UnicastMessage()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock<ModbusTransport>();
            transport.Write(null);
            LastCall.IgnoreArguments();
            // read 4 coils from slave id 2
            Expect.Call(transport.ReadResponse<ReadCoilsInputsResponse>())
                .Return(new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1, new DiscreteCollection(true, false, true, false, false, false, false, false)));
            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 3, 4);
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1, new DiscreteCollection(true, false, true, false, false, false, false, false));
            ReadCoilsInputsResponse response = transport.UnicastMessage<ReadCoilsInputsResponse>(request);
            Assert.AreEqual(expectedResponse.MessageFrame, response.MessageFrame);

            mocks.VerifyAll();
        }
        public void ValidateResponse_MismatchingTransactionIds()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub<IStreamResource>());

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            request.TransactionId = 5;
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);
            response.TransactionId = 6;

            Assert.Throws<IOException>(() => transport.ValidateResponse(request, response));
        }
        public void ValidateResponse()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub<IStreamResource>());

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            request.TransactionId = 5;
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);
            response.TransactionId = 5;

            // no exception is thrown
            transport.ValidateResponse(request, response);
        }
        public void ValidateResponse_CallsOnValidateResponse()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock<ModbusTransport>();

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            Expect.Call(transport.OnValidateResponse(request, response))
                .Return(true);

            mocks.ReplayAll();
            transport.ValidateResponse(request, response);
            mocks.VerifyAll();
        }
        public void ShouldRetryResponse_ReturnsFalse_IfDifferentMessage()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub<IStreamResource>());
            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 1, 1);
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            Assert.False(transport.ShouldRetryResponse(request, response));
        }
        public void ValidateResponse_RequestAheadOfResponse_RequestsReread()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub<IStreamResource>());

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            request.TransactionId = 5;
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);
            response.TransactionId = 4;

            Assert.IsFalse(transport.ValidateResponse(request, response));
        }
        public void ValidateResponse_PassesWithMatchingTransactionDetails()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub<IStreamResource>());

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            request.TransactionId = 5;
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);
            response.TransactionId = 5;

            // no exception is thrown, and return true
            Assert.IsTrue(transport.ValidateResponse(request, response));
        }
        public void ValidateResponse()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock<ModbusTransport>();

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();
            transport.ValidateResponse(request, response);
            mocks.VerifyAll();
        }
        public void OnShouldRetryResponse_ReturnsFalse_IfOutsideThreshold()
        {
            ModbusIpTransport transport = new ModbusIpTransport(MockRepository.GenerateStub<IStreamResource>());
            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            request.TransactionId = 5;
            response.TransactionId = 2;
            transport.RetryOnOldResponseThreshold = 3;

            Assert.False(transport.OnShouldRetryResponse(request, response));
        }
        public void ReadResponse()
        {
            MockRepository mocks = new MockRepository();
            ModbusRtuTransport transport = mocks.PartialMock<ModbusRtuTransport>(MockRepository.GenerateStub<IStreamResource>());

            Expect.Call(transport.Read(ModbusRtuTransport.ResponseFrameStartLength))
                .Return(new byte[] { 1, 1, 1, 0 });

            Expect.Call(transport.Read(2))
                .Return(new byte[] { 81, 136 });

            mocks.ReplayAll();

            ReadCoilsInputsResponse response = transport.ReadResponse<ReadCoilsInputsResponse>() as ReadCoilsInputsResponse;
            Assert.IsNotNull(response);
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, new DiscreteCollection(false));
            Assert.AreEqual(response.MessageFrame, expectedResponse.MessageFrame);

            mocks.VerifyAll();
        }