internal static ReadHoldingInputRegistersResponse ReadRegisters(ReadHoldingInputRegistersRequest request, DataStore dataStore, ModbusDataCollection<ushort> dataSource) { RegisterCollection data = DataStore.ReadData<RegisterCollection, ushort>(dataStore, dataSource, request.StartAddress, request.NumberOfPoints, dataStore.SyncRoot); var response = new ReadHoldingInputRegistersResponse(request.FunctionCode, request.SlaveAddress, data); return response; }
/// <summary> /// Reads contiguous block of holding registers. /// </summary> /// <param name="slaveAddress">Address of device to read values from.</param> /// <param name="startAddress">Address to begin reading.</param> /// <param name="numberOfPoints">Number of holding registers to read.</param> /// <returns>Holding registers status.</returns> public ushort[] ReadHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints) { ValidateNumberOfPoints("numberOfPoints", numberOfPoints, 125); var request = new ReadHoldingInputRegistersRequest( Modbus.ReadHoldingRegisters, slaveAddress, startAddress, numberOfPoints); return(PerformReadRegisters(request)); }
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.Equal(expectedRequest.StartAddress, request.StartAddress); Assert.Equal(expectedRequest.NumberOfPoints, request.NumberOfPoints); }
/// <summary> /// Asynchronously reads contiguous block of input registers. /// </summary> /// <param name="slaveAddress">Address of device to read values from.</param> /// <param name="startAddress">Address to begin reading.</param> /// <param name="numberOfPoints">Number of holding registers to read.</param> /// <returns>A task that represents the asynchronous read operation.</returns> public Task <ushort[]> ReadInputRegistersAsync(byte slaveAddress, ushort startAddress, ushort numberOfPoints) { ValidateNumberOfPoints("numberOfPoints", numberOfPoints, 125); var request = new ReadHoldingInputRegistersRequest( ModbusFunctionCodes.ReadInputRegisters, slaveAddress, startAddress, numberOfPoints); return(PerformReadRegistersAsync(request)); }
public Function12RequestRead(byte functionCode, ushort startAddress, ushort numOfPoints, byte devicenumber, byte moduleNumber) : base(devicenumber, functionCode) { List <byte> customBytes = new List <byte>(); var res = 0xF0 | moduleNumber; customBytes.Add(Convert.ToByte(res)); ReadHoldingInputRegistersRequest innerRequest = new ReadHoldingInputRegistersRequest(Modbus.ReadInputRegisters, devicenumber, startAddress, numOfPoints); byte[] crcforinnerRequest = ModbusUtility.CalculateCrc(innerRequest.MessageFrame); customBytes.AddRange(innerRequest.MessageFrame); customBytes.AddRange(crcforinnerRequest); MessageImpl.CustomBytesInRequest = customBytes.ToArray(); }
public void UnicastMessage_SlaveDeviceBusySlaveException() { var mock = new Mock <ModbusTransport>() { CallBase = true }; var transport = mock.Object; int writeCallsCount = 0; int readResponseCallsCount = 0; // set the wait to retry property to a small value so the test completes quickly transport.WaitToRetryMilliseconds = 5; mock.Setup(t => t.Write(It.IsNotNull <IModbusMessage>())) .Callback(() => ++ writeCallsCount); // return a slave exception a greater number of times than number of retries to make sure we aren't just retrying mock.Setup(t => t.ReadResponse <ReadHoldingInputRegistersResponse>()) .Returns(() => { if (readResponseCallsCount == 0) { return(new SlaveExceptionResponse(1, Modbus.ReadHoldingRegisters + Modbus.ExceptionOffset, Modbus.SlaveDeviceBusy)); } else { return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1))); } }) .Callback(() => ++ readResponseCallsCount); mock.Setup(t => t.OnValidateResponse(It.IsNotNull <IModbusMessage>(), It.IsNotNull <IModbusMessage>())); var request = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, 1, 1, 1); var expectedResponse = new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1)); var response = transport.UnicastMessage <ReadHoldingInputRegistersResponse>(request); Assert.Equal(2, writeCallsCount); Assert.Equal(2, readResponseCallsCount); Assert.Equal(expectedResponse.MessageFrame, response.MessageFrame); mock.VerifyAll(); }
internal static ReadHoldingInputRegistersResponse ReadRegisters( ReadHoldingInputRegistersRequest request, DataStore dataStore, ModbusDataCollection <ushort> dataSource) { RegisterCollection data = DataStore.ReadData <RegisterCollection, ushort>( dataStore, dataSource, request.StartAddress, request.NumberOfPoints, dataStore.SyncRoot); ReadHoldingInputRegistersResponse response = new( request.FunctionCode, request.SlaveAddress, data); return(response); }
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_SlaveDeviceBusySlaveExceptionDoesNotFailAfterExceedingRetries() { 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() .Repeat.Times(transport.Retries + 1); // 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.SlaveDeviceBusy)) .Repeat.Times(transport.Retries); 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.Equal(expectedResponse.MessageFrame, response.MessageFrame); mocks.VerifyAll(); }
public void UnicastMessage_AcknowlegeSlaveException() { var mock = new Mock <ModbusTransport>(new ModbusFactory(), NullModbusLogger.Instance) { CallBase = true }; ModbusTransport transport = mock.Object; int callsCount = 0; // set the wait to retry property to a small value so the test completes quickly transport.WaitToRetryMilliseconds = 5; mock.Setup(t => t.Write(It.IsNotNull <IModbusMessage>())); // return a slave exception a greater number of times than number of retries to make sure we aren't just retrying mock.Setup(t => t.ReadResponse <ReadHoldingInputRegistersResponse>()) .Returns(() => { if (callsCount < transport.Retries + 1) { ++callsCount; return(new SlaveExceptionResponse(1, ModbusFunctionCodes.ReadHoldingRegisters + Modbus.ExceptionOffset, SlaveExceptionCodes.Acknowledge)); } return(new ReadHoldingInputRegistersResponse(ModbusFunctionCodes.ReadHoldingRegisters, 1, new RegisterCollection(1))); }); mock.Setup(t => t.OnValidateResponse(It.IsNotNull <IModbusMessage>(), It.IsNotNull <IModbusMessage>())); var request = new ReadHoldingInputRegistersRequest(ModbusFunctionCodes.ReadHoldingRegisters, 1, 1, 1); var expectedResponse = new ReadHoldingInputRegistersResponse(ModbusFunctionCodes.ReadHoldingRegisters, 1, new RegisterCollection(1)); ReadHoldingInputRegistersResponse response = transport.UnicastMessage <ReadHoldingInputRegistersResponse>(request); Assert.Equal(transport.Retries + 1, callsCount); Assert.Equal(expectedResponse.MessageFrame, response.MessageFrame); mock.VerifyAll(); }
/// <summary> /// Asynchronously reads contiguous block of holding registers. /// </summary> /// <param name="slaveAddress">Address of device to read values from.</param> /// <param name="startAddress">Address to begin reading.</param> /// <param name="numberOfPoints">Number of holding registers to read.</param> /// <returns>A task that represents the asynchronous read operation</returns> public Task <ushort[]> ReadHoldingRegistersAsync(byte slaveAddress, ushort startAddress, ushort numberOfPoints, byte registerSize) { ValidateNumberOfPoints("numberOfPoints", numberOfPoints, 125); ReadHoldingInputRegistersRequest request; switch (registerSize) { case 4: request = new ReadHoldingInputRegistersRequest32( Modbus.ReadHoldingRegisters, slaveAddress, startAddress, numberOfPoints); return(PerformReadRegistersAsync(request)); default: request = new ReadHoldingInputRegistersRequest( Modbus.ReadHoldingRegisters, slaveAddress, startAddress, numberOfPoints); return(PerformReadRegistersAsync(request)); } }
private Task <ushort[]> PerformReadRegistersAsync(ReadHoldingInputRegistersRequest request) { return(Task.Factory.StartNew(() => PerformReadRegisters(request))); }
public void CreateModbusMessageReadHoldingRegistersRequestWithInvalidFrameSize() { ReadHoldingInputRegistersRequest request = ModbusMessageFactory.CreateModbusMessage <ReadHoldingInputRegistersRequest>(new byte[] { 11, Modbus.ReadHoldingRegisters, 0, 0, 5 }); }
public void ToString_ReadInputRegistersRequest() { ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(Modbus.ReadInputRegisters, 5, 1, 10); Assert.AreEqual("Read 10 input registers starting at address 1.", request.ToString()); }
public void CreateReadHoldingInputRegistersRequestMaxSize() { ReadHoldingInputRegistersRequest response = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, 1, 2, Modbus.MaximumRegisterRequestResponseSize); Assert.AreEqual(Modbus.MaximumRegisterRequestResponseSize, response.NumberOfPoints); }