Table of Contents

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 bool

A 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

bool

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

bool

IsAsynchronous

Gets the operation mode.

public bool IsAsynchronous { get; }

Property Value

bool

Lock

Gets the lock object. For synchronous operation only.

public object Lock { get; }

Property Value

object

MaxCoilAddress

Gets the maximum coil address.

public ushort MaxCoilAddress { get; }

Property Value

ushort

MaxDiscreteInputAddress

Gets the maximum discrete input address.

public ushort MaxDiscreteInputAddress { get; }

Property Value

ushort

MaxHoldingRegisterAddress

Gets the maximum holding register address.

public ushort MaxHoldingRegisterAddress { get; }

Property Value

ushort

MaxInputRegisterAddress

Gets the maximum input register address.

public ushort MaxInputRegisterAddress { get; }

Property Value

ushort

RequestValidator

Gets or sets a method that validates each client request.

public Func<byte, ModbusFunctionCode, ushort, ushort, ModbusExceptionCode>? RequestValidator { get; set; }

Property Value

Func<byte, ModbusFunctionCode, ushort, ushort, ModbusExceptionCode>

UnitIdentifiers

Gets list of identifiers of the currently active units.

public IReadOnlyList<byte> UnitIdentifiers { get; }

Property Value

IReadOnlyList<byte>

Methods

AddUnit(byte)

Dynamically adds a new unit to the server.

public void AddUnit(byte unitIdentifer)

Parameters

unitIdentifer byte

The identifier of the unit to add.

ClearBuffers(byte)

Clears all buffer contents.

public void ClearBuffers(byte unitIdentifier = 0)

Parameters

unitIdentifier byte

The 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 bool

A 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 byte

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).

Returns

Span<byte>

GetCoilBuffer<T>(byte)

Gets the coil buffer as type T.

public Span<T> GetCoilBuffer<T>(byte unitIdentifier = 0) where T : unmanaged

Parameters

unitIdentifier byte

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).

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 byte

The 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

Span<byte>

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 byte

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).

Returns

Span<byte>

GetDiscreteInputBuffer<T>(byte)

Gets the discrete input buffer as type T.

public Span<T> GetDiscreteInputBuffer<T>(byte unitIdentifier = 0) where T : unmanaged

Parameters

unitIdentifier byte

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).

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 byte

The 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

Span<byte>

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 byte

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).

Returns

Span<byte>

GetHoldingRegisterBuffer<T>(byte)

Gets the holding register buffer as type T.

public Span<T> GetHoldingRegisterBuffer<T>(byte unitIdentifier = 0) where T : unmanaged

Parameters

unitIdentifier byte

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).

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 byte

The 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

Span<short>

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 byte

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).

Returns

Span<byte>

GetInputRegisterBuffer<T>(byte)

Gets the input register buffer as type T.

public Span<T> GetInputRegisterBuffer<T>(byte unitIdentifier = 0) where T : unmanaged

Parameters

unitIdentifier byte

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).

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 byte

The 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

Span<short>

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 byte

The 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

EventHandler<CoilsChangedEventArgs>

RegistersChanged

Occurs after one or more registers changed.

public event EventHandler<RegistersChangedEventArgs>? RegistersChanged

Event Type

EventHandler<RegistersChangedEventArgs>