Class ModbusServer
- Namespace
- FluentModbus
- Assembly
- FluentModbus.dll
Base class for a Modbus server.
public abstract class ModbusServer : IDisposable
- Inheritance
-
ModbusServer
- Implements
- Derived
Constructors
ModbusServer(bool)
Initializes a new instance of the ModbusServer.
protected ModbusServer(bool isAsynchronous)
Parameters
isAsynchronous
boolA boolean which indicates if the server responds to client requests asynchronously (immediately) or synchronously (regularly at fixed events).
Properties
AlwaysRaiseChangedEvent
Trigger the RegistersChanged or CoilsChanged event even when value has not been updated. Default: false.
public bool AlwaysRaiseChangedEvent { get; set; }
Property Value
EnableRaisingEvents
Gets or sets whether the events should be raised when register or coil data changes. Default: false.
public bool EnableRaisingEvents { get; set; }
Property Value
IsAsynchronous
Gets the operation mode.
public bool IsAsynchronous { get; }
Property Value
Lock
Gets the lock object. For synchronous operation only.
public object Lock { get; }
Property Value
MaxCoilAddress
Gets the maximum coil address.
public ushort MaxCoilAddress { get; }
Property Value
MaxDiscreteInputAddress
Gets the maximum discrete input address.
public ushort MaxDiscreteInputAddress { get; }
Property Value
MaxHoldingRegisterAddress
Gets the maximum holding register address.
public ushort MaxHoldingRegisterAddress { get; }
Property Value
MaxInputRegisterAddress
Gets the maximum input register address.
public ushort MaxInputRegisterAddress { get; }
Property Value
RequestValidator
Gets or sets a method that validates each client request.
public Func<byte, ModbusFunctionCode, ushort, ushort, ModbusExceptionCode>? RequestValidator { get; set; }
Property Value
UnitIdentifiers
Gets list of identifiers of the currently active units.
public IReadOnlyList<byte> UnitIdentifiers { get; }
Property Value
Methods
AddUnit(byte)
Dynamically adds a new unit to the server.
public void AddUnit(byte unitIdentifer)
Parameters
unitIdentifer
byteThe identifier of the unit to add.
ClearBuffers(byte)
Clears all buffer contents.
public void ClearBuffers(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe unit identifier. A value of 0 means that the default unit identifier is used (for single-unit mode only).
Dispose()
Disposes the buffers.
public void Dispose()
Dispose(bool)
Disposes the ModbusServer and frees all managed and unmanaged resources.
protected virtual void Dispose(bool disposing)
Parameters
disposing
boolA value indicating if the finalizer or the dispose method triggered the dispose process.
~ModbusServer()
Disposes the ModbusServer and frees all managed and unmanaged resources.
protected ~ModbusServer()
GetCoilBuffer(byte)
Low level API. Use the generic version for easy access. This method gets the coil buffer as byte array.
public Span<byte> GetCoilBuffer(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe 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).
Returns
GetCoilBuffer<T>(byte)
Gets the coil buffer as type T
.
public Span<T> GetCoilBuffer<T>(byte unitIdentifier = 0) where T : unmanaged
Parameters
unitIdentifier
byteThe 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).
Returns
- Span<T>
Type Parameters
T
The type of the returned array.
GetCoils(byte)
Gets the coils as byte array.
public Span<byte> GetCoils(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe unit identifier of the coils to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).
Returns
GetDiscreteInputBuffer(byte)
Low level API. Use the generic version for easy access. This method gets the discrete input buffer as byte array.
public Span<byte> GetDiscreteInputBuffer(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe 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).
Returns
GetDiscreteInputBuffer<T>(byte)
Gets the discrete input buffer as type T
.
public Span<T> GetDiscreteInputBuffer<T>(byte unitIdentifier = 0) where T : unmanaged
Parameters
unitIdentifier
byteThe 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).
Returns
- Span<T>
Type Parameters
T
The type of the returned array.
GetDiscreteInputs(byte)
Gets the discrete inputs as byte array.
public Span<byte> GetDiscreteInputs(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe unit identifier of the discrete inputs to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).
Returns
GetHoldingRegisterBuffer(byte)
Low level API. Use the generic version for easy access. This method gets the holding register buffer as byte array.
public Span<byte> GetHoldingRegisterBuffer(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe 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).
Returns
GetHoldingRegisterBuffer<T>(byte)
Gets the holding register buffer as type T
.
public Span<T> GetHoldingRegisterBuffer<T>(byte unitIdentifier = 0) where T : unmanaged
Parameters
unitIdentifier
byteThe 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).
Returns
- Span<T>
Type Parameters
T
The type of the returned array.
GetHoldingRegisters(byte)
Gets the holding register as ushort array.
public Span<short> GetHoldingRegisters(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe unit identifier of the holding registers to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).
Returns
GetInputRegisterBuffer(byte)
Low level API. Use the generic version for easy access. This method gets the input register buffer as byte array.
public Span<byte> GetInputRegisterBuffer(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe 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).
Returns
GetInputRegisterBuffer<T>(byte)
Gets the input register buffer as type T
.
public Span<T> GetInputRegisterBuffer<T>(byte unitIdentifier = 0) where T : unmanaged
Parameters
unitIdentifier
byteThe 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).
Returns
- Span<T>
Type Parameters
T
The type of the returned array.
GetInputRegisters(byte)
Gets the input register as ushort array.
public Span<short> GetInputRegisters(byte unitIdentifier = 0)
Parameters
unitIdentifier
byteThe unit identifier of the input registers to return. A value of 0 means that the default unit identifier is used (for single-unit mode only).
Returns
ProcessRequests()
Process incoming requests.
protected abstract void ProcessRequests()
RemoveUnit(byte)
Dynamically removes an existing unit from the server.
public void RemoveUnit(byte unitIdentifer)
Parameters
unitIdentifer
byteThe identifier of the unit to remove.
StartProcessing()
Starts the server operation.
protected virtual void StartProcessing()
Stop()
Stops the server operation and cleans up all resources.
public virtual void Stop()
StopProcessing()
Stops the server operation.
protected virtual void StopProcessing()
Update()
Serve all available client requests. For synchronous operation only.
public void Update()
Events
CoilsChanged
Occurs after one or more coils changed.
public event EventHandler<CoilsChangedEventArgs>? CoilsChanged
Event Type
RegistersChanged
Occurs after one or more registers changed.
public event EventHandler<RegistersChangedEventArgs>? RegistersChanged