Beispiel #1
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();
        }
        public void Read()
        {
            MockRepository  mocks = new MockRepository();
            IStreamResource mockSerialResource = mocks.StrictMock <IStreamResource>();

            Expect.Call(mockSerialResource.Read(new byte[5], 0, 5)).Do(((Func <byte[], int, int, int>) delegate(byte[] buf, int offset, int count)
            {
                Array.Copy(new byte[] { 2, 2, 2 }, buf, 3);
                return(3);
            }));

            Expect.Call(mockSerialResource.Read(new byte[] { 2, 2, 2, 0, 0 }, 3, 2)).Do(((Func <byte[], int, int, int>) delegate(byte[] buf, int offset, int count)
            {
                Array.Copy(new byte[] { 3, 3 }, 0, buf, 3, 2);
                return(2);
            }));

            mocks.ReplayAll();

            ModbusRtuTransport transport = new ModbusRtuTransport(mockSerialResource);

            Assert.AreEqual(new byte[] { 2, 2, 2, 3, 3 }, transport.Read(5));

            mocks.VerifyAll();
        }
        public void ReadRequest()
        {
            MockRepository     mocks     = new MockRepository();
            ModbusRtuTransport transport = mocks.PartialMock <ModbusRtuTransport>(MockRepository.GenerateStub <IStreamResource>());

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

            Expect.Call(transport.Read(1))
            .Return(new byte[] { 5 });

            mocks.ReplayAll();

            Assert.AreEqual(new byte[] { 1, 1, 1, 0, 1, 0, 0, 5 }, transport.ReadRequest());
            mocks.VerifyAll();
        }
Beispiel #4
0
        public void Read()
        {
            var mock = new Mock <IStreamResource>(MockBehavior.Strict);

            mock.Setup(s => s.Read(It.Is <byte[]>(x => x.Length == 5), 0, 5))
            .Returns((byte[] buf, int offset, int count) =>
            {
                Array.Copy(new byte[] { 2, 2, 2 }, buf, 3);
                return(3);
            });

            mock.Setup(s => s.Read(It.Is <byte[]>(x => x.Length == 5), 3, 2))
            .Returns((byte[] buf, int offset, int count) =>
            {
                Array.Copy(new byte[] { 3, 3 }, 0, buf, 3, 2);
                return(2);
            });

            var factory = new ModbusFactory();

            var transport = new ModbusRtuTransport(mock.Object, factory, NullModbusLogger.Instance);

            Assert.Equal(new byte[] { 2, 2, 2, 3, 3 }, transport.Read(5));

            mock.VerifyAll();
        }
        public void Read()
        {
            MockRepository mocks = new MockRepository();
            IStreamResource mockSerialResource = mocks.StrictMock<IStreamResource>();

            Expect.Call(mockSerialResource.Read(new byte[5], 0, 5))
                .Do(((Func<byte[], int, int, int>) delegate(byte[] buf, int offset, int count)
                {
                    Array.Copy(new byte[] {2, 2, 2}, buf, 3);
                    return 3;
                }));

            Expect.Call(mockSerialResource.Read(new byte[] {2, 2, 2, 0, 0}, 3, 2))
                .Do(((Func<byte[], int, int, int>) delegate(byte[] buf, int offset, int count)
                {
                    Array.Copy(new byte[] {3, 3}, 0, buf, 3, 2);
                    return 2;
                }));

            mocks.ReplayAll();

            ModbusRtuTransport transport = new ModbusRtuTransport(mockSerialResource);
            Assert.AreEqual(new byte[] {2, 2, 2, 3, 3}, transport.Read(5));

            mocks.VerifyAll();
        }
        public void ReadResponseSlaveException()
        {
            MockRepository     mocks     = new MockRepository();
            ModbusRtuTransport transport = mocks.PartialMock <ModbusRtuTransport>(MockRepository.GenerateStub <IStreamResource>());

            byte[] messageFrame = { 0x01, 0x81, 0x02 };
            byte[] crc          = ModbusUtility.CalculateCrc(messageFrame);

            Expect.Call(transport.Read(ModbusRtuTransport.ResponseFrameStartLength))
            .Return(messageFrame.Concat(SequenceUtility.ToSequence(crc[0])).ToArray());

            Expect.Call(transport.Read(1))
            .Return(new byte[] { crc[1] });

            mocks.ReplayAll();

            Assert.IsTrue(transport.ReadResponse <ReadCoilsInputsResponse>() is SlaveExceptionResponse);

            mocks.VerifyAll();
        }
        public void ReadResponseSlaveExceptionWithErroneousLrc()
        {
            MockRepository     mocks     = new MockRepository();
            ModbusRtuTransport transport = mocks.PartialMock <ModbusRtuTransport>(MockRepository.GenerateStub <IStreamResource>());

            byte[] messageFrame = { 0x01, 0x81, 0x02 };
            // invalid crc
            byte[] crc = { 0x9, 0x9 };

            Expect.Call(transport.Read(ModbusRtuTransport.ResponseFrameStartLength))
            .Return(messageFrame.Concat(crc[0].ToSequence()).ToArray());

            Expect.Call(transport.Read(1))
            .Return(new byte[] { crc[1] });

            mocks.ReplayAll();

            transport.ReadResponse <ReadCoilsInputsResponse>();

            mocks.VerifyAll();
        }