Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/FluentModbus/Server/ModbusServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ public Span<T> GetInputRegisterBuffer<T>(byte unitIdentifier = 0) where T : unma
/// </summary>
/// <param name="unitIdentifier">The unit identifier of the input register buffer to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).</param>
public Span<byte> GetInputRegisterBuffer(byte unitIdentifier = 0)
{
return GetInputRegisterMemory(unitIdentifier).Span;
}

/// <summary>
/// Gets the input register.
/// </summary>
/// <param name="unitIdentifier">The unit identifier of the input register buffer to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).</param>
public Memory<byte> GetInputRegisterMemory(byte unitIdentifier = 0)
{
return Find(unitIdentifier, _inputRegisterBufferMap);
}
Expand Down Expand Up @@ -260,6 +269,15 @@ public Span<T> GetHoldingRegisterBuffer<T>(byte unitIdentifier = 0) where T : un
/// </summary>
/// <param name="unitIdentifier">The unit identifier of the holding register buffer to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).</param>
public Span<byte> GetHoldingRegisterBuffer(byte unitIdentifier = 0)
{
return GetHoldingRegisterMemory(unitIdentifier).Span;
}

/// <summary>
/// Gets the holding register buffer.
/// </summary>
/// <param name="unitIdentifier">The unit identifier of the holding register buffer to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).</param>
public Memory<byte> GetHoldingRegisterMemory(byte unitIdentifier = 0)
{
return Find(unitIdentifier, _holdingRegisterBufferMap);
}
Expand Down Expand Up @@ -288,6 +306,15 @@ public Span<T> GetCoilBuffer<T>(byte unitIdentifier = 0) where T : unmanaged
/// </summary>
/// <param name="unitIdentifier">The unit identifier of the coil buffer to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).</param>
public Span<byte> GetCoilBuffer(byte unitIdentifier = 0)
{
return GetCoilMemory(unitIdentifier).Span;
}

/// <summary>
/// Gets the coil buffer.
/// </summary>
/// <param name="unitIdentifier">The unit identifier of the coil buffer to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).</param>
public Memory<byte> GetCoilMemory(byte unitIdentifier = 0)
{
return Find(unitIdentifier, _coilBufferMap);
}
Expand Down Expand Up @@ -316,6 +343,15 @@ public Span<T> GetDiscreteInputBuffer<T>(byte unitIdentifier = 0) where T : unma
/// </summary>
/// <param name="unitIdentifier">The unit identifier of the discrete input buffer to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).</param>
public Span<byte> GetDiscreteInputBuffer(byte unitIdentifier = 0)
{
return GetDiscreteInputMemory(unitIdentifier).Span;
}

/// <summary>
/// Gets the discrete input buffer.
/// </summary>
/// <param name="unitIdentifier">The unit identifier of the discrete input buffer to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).</param>
public Memory<byte> GetDiscreteInputMemory(byte unitIdentifier = 0)
{
return Find(unitIdentifier, _discreteInputBufferMap);
}
Expand Down Expand Up @@ -449,7 +485,7 @@ public void RemoveUnit(byte unitIdentifer)
}
}

private Span<byte> Find(byte unitIdentifier, Dictionary<byte, byte[]> map)
private Memory<byte> Find(byte unitIdentifier, Dictionary<byte, byte[]> map)
{
if (!map.TryGetValue(unitIdentifier, out var buffer))
throw new KeyNotFoundException(ErrorMessage.ModbusServer_UnitIdentifierNotFound);
Expand All @@ -459,10 +495,10 @@ private Span<byte> Find(byte unitIdentifier, Dictionary<byte, byte[]> map)

internal void OnRegistersChanged(byte unitIdentifier, int[] registers)
{
RegistersChanged?.Invoke(this, new RegistersChangedEventArgs()
{
RegistersChanged?.Invoke(this, new RegistersChangedEventArgs()
{
UnitIdentifier = unitIdentifier,
Registers = registers
Registers = registers
});
}

Expand Down