Class ModbusServer
A Modbus device: a unit address and the four tables it serves, coils, discrete inputs, holding registers, and input registers, each holding only the addresses it was given.
public sealed class ModbusServer : IDisposable
- Inheritance
-
ModbusServer
- Implements
- Inherited Members
Examples
using var meter = new ModbusServer(17);
meter.SetHoldingRegisters(107, [2301, 418, 0]);
Remarks
Answer(ReadOnlySpan<byte>) takes one RTU frame and returns the frame the device sends back, following the Modbus over Serial Line specification: a frame that fails its CRC, is for another unit, or is a broadcast gets no answer, and a broadcast write is still carried out. A request the device cannot serve is answered with the exception the application protocol gives: an unknown function, then a quantity or value out of range, then an address the device does not have.
Put a device on a ModbusLine to poll it with a ModbusClient. The line shares the device, so this object still reads and changes its tables.
Constructors
ModbusServer(byte)
Makes a device at a unit address, with every table empty.
public ModbusServer(byte unit)
Parameters
unitbyteIts address, 1 to 247.
Exceptions
- PamojaException
The unit is 0, the broadcast address, or 248 to 255, which the specification reserves.
Properties
Served
How many requests the device has carried out, broadcasts included and refusals not.
public int Served { get; }
Property Value
Unit
The device's unit address.
public byte Unit { get; }
Property Value
Methods
Answer(ReadOnlySpan<byte>)
Answers one RTU frame, as the device on the line does.
public byte[]? Answer(ReadOnlySpan<byte> frame)
Parameters
frameReadOnlySpan<byte>The frame as it came off the line, CRC included.
Returns
- byte[]
The frame to send back, or null when the device stays silent: the frame failed its CRC, is for another unit, or is a broadcast, whose write the device still carries out.
Coil(ushort)
Reads a coil.
public bool? Coil(ushort address)
Parameters
addressushortThe coil's address.
Returns
- bool?
Its state, or null when the device has no coil there.
DiscreteInput(ushort)
Reads a discrete input.
public bool? DiscreteInput(ushort address)
Parameters
addressushortThe input's address.
Returns
- bool?
Its state, or null when the device has no input there.
Dispose()
Releases this handle to the device. A line it is on keeps its own share.
public void Dispose()
HoldingRegister(ushort)
Reads a holding register.
public ushort? HoldingRegister(ushort address)
Parameters
addressushortThe register's address.
Returns
- ushort?
Its value, or null when the device has no register there.
InputRegister(ushort)
Reads an input register.
public ushort? InputRegister(ushort address)
Parameters
addressushortThe register's address.
Returns
- ushort?
Its value, or null when the device has no register there.
SetCoils(ushort, ReadOnlySpan<bool>)
Sets coils from an address on, adding any the device did not have.
public void SetCoils(ushort start, ReadOnlySpan<bool> values)
Parameters
startushortThe first coil's address.
valuesReadOnlySpan<bool>Their states, in address order.
SetDiscreteInputs(ushort, ReadOnlySpan<bool>)
Sets discrete inputs from an address on, adding any the device did not have.
public void SetDiscreteInputs(ushort start, ReadOnlySpan<bool> values)
Parameters
startushortThe first input's address.
valuesReadOnlySpan<bool>Their states, in address order.
SetHoldingRegisters(ushort, ReadOnlySpan<ushort>)
Sets holding registers from an address on, adding any the device did not have.
public void SetHoldingRegisters(ushort start, ReadOnlySpan<ushort> values)
Parameters
startushortThe first register's address.
valuesReadOnlySpan<ushort>Their values, in address order.
SetInputRegisters(ushort, ReadOnlySpan<ushort>)
Sets input registers from an address on, adding any the device did not have.
public void SetInputRegisters(ushort start, ReadOnlySpan<ushort> values)
Parameters
startushortThe first register's address.
valuesReadOnlySpan<ushort>Their values, in address order.