internal static ReadHoldingInputRegistersResponse ReadRegisters(ReadHoldingInputRegistersRequest request, ModbusDataCollection<ushort> dataSource)
        {
            RegisterCollection data = DataStore.ReadData<RegisterCollection, ushort>(dataSource, request.StartAddress, request.NumberOfPoints);
            ReadHoldingInputRegistersResponse response = new ReadHoldingInputRegistersResponse(request.FunctionCode, request.SlaveAddress, data);

            return response;
        }
        public void ToString_ReadInputRegistersRequest()
        {
            ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(Modbus.ReadInputRegisters, 5,
                1, 10);

            Assert.Equal("Read 10 input registers starting at address 1.", request.ToString());
        }
 public void CreateReadInputRegistersRequest()
 {
     ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(Modbus.ReadInputRegisters, 5, 1, 10);
     Assert.AreEqual(Modbus.ReadInputRegisters, request.FunctionCode);
     Assert.AreEqual(5, request.SlaveAddress);
     Assert.AreEqual(1, request.StartAddress);
     Assert.AreEqual(10, request.NumberOfPoints);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="slaveAddress"></param>
 /// <param name="startReadAddress"></param>
 /// <param name="numberOfPointsToRead"></param>
 /// <param name="startWriteAddress"></param>
 /// <param name="writeData"></param>
 public ReadWriteMultipleRegistersRequest(byte slaveAddress, ushort startReadAddress, ushort numberOfPointsToRead,
     ushort startWriteAddress, RegisterCollection writeData)
     : base(slaveAddress, Modbus.ReadWriteMultipleRegisters)
 {
     _readRequest = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, slaveAddress,
         startReadAddress, numberOfPointsToRead);
     _writeRequest = new WriteMultipleRegistersRequest(slaveAddress, startWriteAddress, writeData);
 }
Exemplo n.º 5
0
 public void CreateModbusMessageReadHoldingRegistersRequest()
 {
     ReadHoldingInputRegistersRequest request = ModbusMessageFactory.CreateModbusMessage<ReadHoldingInputRegistersRequest>(new byte[] { 17, Modbus.ReadHoldingRegisters, 0, 107, 0, 3 });
     ReadHoldingInputRegistersRequest expectedRequest = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, 17, 107, 3);
     AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
     Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
     Assert.AreEqual(expectedRequest.NumberOfPoints, request.NumberOfPoints);
 }
        protected override void InitializeUnique(byte[] frame)
        {
            if (frame.Length < _minimumFrameSize + frame[10])
                throw new FormatException("Message frame does not contain enough bytes.");

            byte[] readFrame = CollectionUtility.Slice(frame, 2, 4);
            byte[] writeFrame = CollectionUtility.Slice(frame, 6, frame.Length - 6);
            byte[] header = { SlaveAddress, FunctionCode };

            _readRequest = ModbusMessageFactory.CreateModbusMessage<ReadHoldingInputRegistersRequest>(CollectionUtility.Concat(header, readFrame));
            _writeRequest = ModbusMessageFactory.CreateModbusMessage<WriteMultipleRegistersRequest>(CollectionUtility.Concat(header, writeFrame));
        }
        protected override void InitializeUnique(byte[] frame)
        {
            if (frame.Length < _minimumFrameSize + frame[10])
            {
                throw new FormatException("Message frame does not contain enough bytes.");
            }

            byte[] readFrame  = CollectionUtility.Slice(frame, 2, 4);
            byte[] writeFrame = CollectionUtility.Slice(frame, 6, frame.Length - 6);
            byte[] header     = { SlaveAddress, FunctionCode };

            _readRequest  = ModbusMessageFactory.CreateModbusMessage <ReadHoldingInputRegistersRequest>(CollectionUtility.Concat(header, readFrame));
            _writeRequest = ModbusMessageFactory.CreateModbusMessage <WriteMultipleRegistersRequest>(CollectionUtility.Concat(header, writeFrame));
        }
Exemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="frame"></param>
        protected override void InitializeUnique(byte[] frame)
        {
            if (frame.Length < MinimumFrameSize + frame[10])
            {
                throw new FormatException("Message frame does not contain enough bytes.");
            }

            byte[] readFrame  = new byte[2 + 4];
            byte[] writeFrame = new byte[frame.Length - 6 + 2];

            readFrame[0] = writeFrame[0] = SlaveAddress;
            readFrame[1] = writeFrame[1] = FunctionCode;

            Buffer.BlockCopy(frame, 2, readFrame, 2, 4);
            Buffer.BlockCopy(frame, 6, writeFrame, 2, frame.Length - 6);

            _readRequest  = ModbusMessageFactory.CreateModbusMessage <ReadHoldingInputRegistersRequest>(readFrame);
            _writeRequest = ModbusMessageFactory.CreateModbusMessage <WriteMultipleRegistersRequest>(writeFrame);
        }
        public ReadWriteMultipleRegistersRequest(
            byte slaveAddress,
            ushort startReadAddress,
            ushort numberOfPointsToRead,
            ushort startWriteAddress,
            RegisterCollection writeData)
            : base(slaveAddress, Modbus.ReadWriteMultipleRegisters)
        {
            _readRequest = new ReadHoldingInputRegistersRequest(
                Modbus.ReadHoldingRegisters,
                slaveAddress,
                startReadAddress,
                numberOfPointsToRead);

            _writeRequest = new WriteMultipleRegistersRequest(
                slaveAddress,
                startWriteAddress,
                writeData);
        }
        public void UnicastMessage_AcknowlegeSlaveException()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock<ModbusTransport>();

            // set the wait to retry property to a small value so the test completes quickly
            transport.WaitToRetryMilliseconds = 5;

            transport.Write(null);
            LastCall.IgnoreArguments();

            // return a slave exception a greater number of times than number of retries to make sure we aren't just retrying
            Expect.Call(transport.ReadResponse<ReadHoldingInputRegistersResponse>())
                .Return(new SlaveExceptionResponse(1, Modbus.ReadHoldingRegisters + Modbus.ExceptionOffset,
                    Modbus.Acknowledge))
                .Repeat.Times(transport.Retries + 1);

            Expect.Call(transport.ReadResponse<ReadHoldingInputRegistersResponse>())
                .Return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1)));

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

            mocks.ReplayAll();

            ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(
                Modbus.ReadHoldingRegisters, 1, 1, 1);
            ReadHoldingInputRegistersResponse expectedResponse =
                new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1));
            ReadHoldingInputRegistersResponse response =
                transport.UnicastMessage<ReadHoldingInputRegistersResponse>(request);
            Assert.AreEqual(expectedResponse.MessageFrame, response.MessageFrame);

            mocks.VerifyAll();
        }
 public void CreateReadHoldingInputRegistersRequestMaxSize()
 {
     ReadHoldingInputRegistersRequest response = new ReadHoldingInputRegistersRequest(
         Modbus.ReadHoldingRegisters, 1, 2, Modbus.MaximumRegisterRequestResponseSize);
     Assert.Equal(Modbus.MaximumRegisterRequestResponseSize, response.NumberOfPoints);
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="frame"></param>
        protected override void InitializeUnique(byte[] frame)
        {
            if (frame.Length < MinimumFrameSize + frame[10])
                throw new FormatException("Message frame does not contain enough bytes.");

            byte[] readFrame = new byte[2 + 4];
            byte[] writeFrame = new byte[frame.Length - 6 + 2];

            readFrame[0] = writeFrame[0] = SlaveAddress;
            readFrame[1] = writeFrame[1] = FunctionCode;

            Buffer.BlockCopy(frame, 2, readFrame, 2, 4);
            Buffer.BlockCopy(frame, 6, writeFrame, 2, frame.Length - 6);

            _readRequest = ModbusMessageFactory.CreateModbusMessage<ReadHoldingInputRegistersRequest>(readFrame);
            _writeRequest = ModbusMessageFactory.CreateModbusMessage<WriteMultipleRegistersRequest>(writeFrame);
        }
Exemplo n.º 13
0
        public void UnicastMessage_ReReadsIfValidateResponseIsFalse()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock<ModbusTransport>();
            transport.WaitToRetryMilliseconds = 5;

            transport.Write(null);
            LastCall.IgnoreArguments();

            Expect.Call(transport.ReadResponse<ReadHoldingInputRegistersResponse>())
                .Return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1)))
                .Repeat.Times(2)
                .Message("ReadResponse should be called twice, one for the retry");

            Expect.Call(transport.OnValidateResponse(null, null))
                .IgnoreArguments()
                .Return(false)
                .Repeat.Times(1);
            Expect.Call(transport.OnValidateResponse(null, null))
                .IgnoreArguments()
                .Return(true)
                .Repeat.Times(1);

            mocks.ReplayAll();
            ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, 1, 1, 1);
            ReadHoldingInputRegistersResponse response = transport.UnicastMessage<ReadHoldingInputRegistersResponse>(request);

            mocks.VerifyAll();
        }
        public void UnicastMessage_ReReads_IfShouldRetryReturnTrue()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock<ModbusTransport>();

            transport.RetryOnOldResponseThreshold = 3;
            transport.Stub(x => x.Write(null)).IgnoreArguments();

            Expect.Call(transport.ReadResponse<ReadHoldingInputRegistersResponse>())
                .Return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1)))
                .Repeat.Times(2)
                .Message("ReadResponse should be called twice, one for the retry");

            transport.Expect(x => x.OnValidateResponse(null, null)).IgnoreArguments();

            Expect.Call(transport.OnShouldRetryResponse(null, null))
                .IgnoreArguments()
                .Return(true)
                .Repeat.Times(1);
            Expect.Call(transport.OnShouldRetryResponse(null, null))
                .IgnoreArguments()
                .Return(false)
                .Repeat.Times(1);

            mocks.ReplayAll();
            ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, 1, 1, 1);
            ReadHoldingInputRegistersResponse response = transport.UnicastMessage<ReadHoldingInputRegistersResponse>(request);

            mocks.VerifyAll();
        }