Skip to main content

Class ModbusPolledDevice

Represents an abstract base class for Modbus devices where register values are polled

Assembly: Meadow.Modbus.dll​
View Source​
Declaration
public abstract class ModbusPolledDevice

Properties​

BusAddress​

Gets or sets the device's address on the bus

View Source​
Declaration
protected byte BusAddress { get; set; }

Fields​

DefaultRefreshPeriod​

Gets the default refresh period for polling.

View Source​
Declaration
public static readonly TimeSpan DefaultRefreshPeriod

Methods​

StartPolling()​

Starts polling the Modbus device.

View Source​
Declaration
public virtual void StartPolling()

StopPolling()​

Stops polling the Modbus device.

View Source​
Declaration
public virtual void StopPolling()

WriteHoldingRegister(ushort, params ushort[])​

Writes one or more values to the holding registers of the Modbus device.

View Source​
Declaration
protected Task WriteHoldingRegister(ushort startRegister, params ushort[] data)
Returns​

System.Threading.Tasks.Task: A task representing the asynchronous write operation.

Parameters​
TypeNameDescription
System.UInt16startRegisterThe starting register address.
System.UInt16[]dataThe values to be written to the holding registers.

ReadHoldingRegisters(ushort, int)​

Reads one or more values from the holding registers of the Modbus device.

View Source​
Declaration
protected Task<ushort[]> ReadHoldingRegisters(ushort startRegister, int count)
Returns​

System.Threading.Tasks.Task<System.UInt16[]>: A task representing the asynchronous write operation.

Parameters​
TypeNameDescription
System.UInt16startRegisterThe starting register address.
System.Int32countThe number of registers to read.

ReadCoil(ushort)​

Reads a value from a coil register of the Modbus device.

View Source​
Declaration
protected Task<bool> ReadCoil(ushort register)
Returns​

System.Threading.Tasks.Task<System.Boolean>: A task representing the asynchronous write operation.

Parameters​
TypeNameDescription
System.UInt16registerThe coil register address.

WriteCoil(ushort, bool)​

Writes a value to a coil register of the Modbus device.

View Source​
Declaration
protected Task WriteCoil(ushort register, bool value)
Returns​

System.Threading.Tasks.Task: A task representing the asynchronous write operation.

Parameters​
TypeNameDescription
System.UInt16registerThe coil register address.
System.BooleanvalueThe value to be written to the coil registers.

MapHoldingRegistersToProperty(ushort, int, string, double?, double?, SourceFormat)​

Maps a range of holding registers to a property of the Modbus device.

View Source​
Declaration
protected void MapHoldingRegistersToProperty(ushort startRegister, int registerCount, string propertyName, double? scale = null, double? offset = null, ModbusPolledDevice.SourceFormat sourceFormat = SourceFormat.LittleEndianInteger)
Parameters​
TypeNameDescription
System.UInt16startRegisterThe starting register address.
System.Int32registerCountThe number of registers to map.
System.StringpropertyNameThe name of the property to map the registers to.
System.Nullable<System.Double>scaleThe optional scale factor to apply to the register values.
System.Nullable<System.Double>offsetThe optional offset to apply to the register values.
Meadow.Modbus.ModbusPolledDevice.SourceFormatsourceFormatThe format of the source registers

MapInputRegistersToProperty(ushort, int, string, double?, double?, SourceFormat)​

Maps a range of input registers to a property of the Modbus device.

View Source​
Declaration
protected void MapInputRegistersToProperty(ushort startRegister, int registerCount, string propertyName, double? scale = null, double? offset = null, ModbusPolledDevice.SourceFormat sourceFormat = SourceFormat.LittleEndianInteger)
Parameters​
TypeNameDescription
System.UInt16startRegisterThe starting register address.
System.Int32registerCountThe number of registers to map.
System.StringpropertyNameThe name of the property to map the registers to.
System.Nullable<System.Double>scaleThe optional scale factor to apply to the register values.
System.Nullable<System.Double>offsetThe optional offset to apply to the register values.
Meadow.Modbus.ModbusPolledDevice.SourceFormatsourceFormatThe format of the source registers

MapHoldingRegistersToField(ushort, int, string, double?, double?, SourceFormat)​

Maps a range of holding registers to a field of the Modbus device.

View Source​
Declaration
protected void MapHoldingRegistersToField(ushort startRegister, int registerCount, string fieldName, double? scale = null, double? offset = null, ModbusPolledDevice.SourceFormat sourceFormat = SourceFormat.LittleEndianInteger)
Parameters​
TypeNameDescription
System.UInt16startRegisterThe starting register address.
System.Int32registerCountThe number of registers to map.
System.StringfieldNameThe name of the field to map the registers to.
System.Nullable<System.Double>scaleThe optional scale factor to apply to the register values.
System.Nullable<System.Double>offsetThe optional offset to apply to the register values.
Meadow.Modbus.ModbusPolledDevice.SourceFormatsourceFormatThe format of the source registers

MapHoldingRegistersToProperty(ushort, int, string, Func<ushort[], object>)​

Maps a range of holding registers to a property of the Modbus device.

View Source​
Declaration
protected void MapHoldingRegistersToProperty(ushort startRegister, int registerCount, string propertyName, Func<ushort[], object> conversionFunction)
Parameters​
TypeNameDescription
System.UInt16startRegisterThe starting register address.
System.Int32registerCountThe number of registers to map.
System.StringpropertyNameThe name of the property to map the registers to.
System.Func<System.UInt16[],System.Object>conversionFunctionThe custom conversion function to transform raw register values to the property type.

MapHoldingRegistersToField(ushort, int, string, Func<ushort[], object>)​

Maps a range of holding registers to a class field

View Source​
Declaration
protected void MapHoldingRegistersToField(ushort startRegister, int registerCount, string fieldName, Func<ushort[], object> conversionFunction)
Parameters​
TypeNameDescription
System.UInt16startRegisterThe starting register address.
System.Int32registerCountThe number of registers to map.
System.StringfieldNameThe name of the field to map the registers to.
System.Func<System.UInt16[],System.Object>conversionFunctionThe custom conversion function to transform raw register values to the field type.

MapInputRegistersToField(ushort, int, string, Func<ushort[], object>)​

Maps a range of input registers to a class field

View Source​
Declaration
protected void MapInputRegistersToField(ushort startRegister, int registerCount, string fieldName, Func<ushort[], object> conversionFunction)
Parameters​
TypeNameDescription
System.UInt16startRegisterThe starting register address.
System.Int32registerCountThe number of registers to map.
System.StringfieldNameThe name of the field to map the registers to.
System.Func<System.UInt16[],System.Object>conversionFunctionThe custom conversion function to transform raw register values to the field type.

GetValueByFormat(ushort[], SourceFormat)​

Retrieves the value from the specified register array based on the provided data format.

View Source​
Declaration
protected object GetValueByFormat(ushort[] data, ModbusPolledDevice.SourceFormat format)
Returns​

System.Object: The extracted value in its raw form.

Parameters​
TypeNameDescription
System.UInt16[]dataThe array of ushort register values.
Meadow.Modbus.ModbusPolledDevice.SourceFormatformatThe source data format indicating how to interpret the values in the data array.

Events​

CommTimeout​

Raised when communication with the target device times out

View Source​
Declaration
public event EventHandler? CommTimeout
Event Type​

System.EventHandler

CommError​

Raised when a communication error with the target device occurs

View Source​
Declaration
public event EventHandler<Exception>? CommError
Event Type​

System.EventHandler<System.Exception>

DataUpdated​

Raised when data has been read from the device

View Source​
Declaration
public event EventHandler? DataUpdated
Event Type​

System.EventHandler