Class ModbusPolledDevice
Represents an abstract base class for Modbus devices where register values are polled
Assembly: Meadow.Modbus.dll
View Source
public abstract class ModbusPolledDevice
Properties
BusAddress
Gets or sets the device's address on the bus
View Source
protected byte BusAddress { get; set; }
Fields
DefaultRefreshPeriod
Gets the default refresh period for polling.
View Source
public static readonly TimeSpan DefaultRefreshPeriod
Methods
StartPolling()
Starts polling the Modbus device.
View Source
public virtual void StartPolling()
StopPolling()
Stops polling the Modbus device.
View Source
public virtual void StopPolling()
WriteHoldingRegister(ushort, params ushort[])
Writes one or more values to the holding registers of the Modbus device.
View Source
protected Task WriteHoldingRegister(ushort startRegister, params ushort[] data)
Returns
System.Threading.Tasks.Task: A task representing the asynchronous write operation.
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | startRegister | The starting register address. |
System.UInt16[] | data | The 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
protected Task<ushort[]> ReadHoldingRegisters(ushort startRegister, int count)
Returns
System.Threading.Tasks.Task<System.UInt16[]>: A task representing the asynchronous write operation.
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | startRegister | The starting register address. |
System.Int32 | count | The number of registers to read. |
ReadCoil(ushort)
Reads a value from a coil register of the Modbus device.
View Source
protected Task<bool> ReadCoil(ushort register)
Returns
System.Threading.Tasks.Task<System.Boolean>: A task representing the asynchronous write operation.
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | register | The coil register address. |
WriteCoil(ushort, bool)
Writes a value to a coil register of the Modbus device.
View Source
protected Task WriteCoil(ushort register, bool value)
Returns
System.Threading.Tasks.Task: A task representing the asynchronous write operation.
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | register | The coil register address. |
System.Boolean | value | The 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
protected void MapHoldingRegistersToProperty(ushort startRegister, int registerCount, string propertyName, double? scale = null, double? offset = null, ModbusPolledDevice.SourceFormat sourceFormat = SourceFormat.LittleEndianInteger)
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | startRegister | The starting register address. |
System.Int32 | registerCount | The number of registers to map. |
System.String | propertyName | The name of the property to map the registers to. |
System.Nullable<System.Double> | scale | The optional scale factor to apply to the register values. |
System.Nullable<System.Double> | offset | The optional offset to apply to the register values. |
| Meadow.Modbus.ModbusPolledDevice.SourceFormat | sourceFormat | The 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
protected void MapInputRegistersToProperty(ushort startRegister, int registerCount, string propertyName, double? scale = null, double? offset = null, ModbusPolledDevice.SourceFormat sourceFormat = SourceFormat.LittleEndianInteger)
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | startRegister | The starting register address. |
System.Int32 | registerCount | The number of registers to map. |
System.String | propertyName | The name of the property to map the registers to. |
System.Nullable<System.Double> | scale | The optional scale factor to apply to the register values. |
System.Nullable<System.Double> | offset | The optional offset to apply to the register values. |
| Meadow.Modbus.ModbusPolledDevice.SourceFormat | sourceFormat | The 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
protected void MapHoldingRegistersToField(ushort startRegister, int registerCount, string fieldName, double? scale = null, double? offset = null, ModbusPolledDevice.SourceFormat sourceFormat = SourceFormat.LittleEndianInteger)
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | startRegister | The starting register address. |
System.Int32 | registerCount | The number of registers to map. |
System.String | fieldName | The name of the field to map the registers to. |
System.Nullable<System.Double> | scale | The optional scale factor to apply to the register values. |
System.Nullable<System.Double> | offset | The optional offset to apply to the register values. |
| Meadow.Modbus.ModbusPolledDevice.SourceFormat | sourceFormat | The 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
protected void MapHoldingRegistersToProperty(ushort startRegister, int registerCount, string propertyName, Func<ushort[], object> conversionFunction)
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | startRegister | The starting register address. |
System.Int32 | registerCount | The number of registers to map. |
System.String | propertyName | The name of the property to map the registers to. |
System.Func<System.UInt16[],System.Object> | conversionFunction | The 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
protected void MapHoldingRegistersToField(ushort startRegister, int registerCount, string fieldName, Func<ushort[], object> conversionFunction)
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | startRegister | The starting register address. |
System.Int32 | registerCount | The number of registers to map. |
System.String | fieldName | The name of the field to map the registers to. |
System.Func<System.UInt16[],System.Object> | conversionFunction | The 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
protected void MapInputRegistersToField(ushort startRegister, int registerCount, string fieldName, Func<ushort[], object> conversionFunction)
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16 | startRegister | The starting register address. |
System.Int32 | registerCount | The number of registers to map. |
System.String | fieldName | The name of the field to map the registers to. |
System.Func<System.UInt16[],System.Object> | conversionFunction | The 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
protected object GetValueByFormat(ushort[] data, ModbusPolledDevice.SourceFormat format)
Returns
System.Object: The extracted value in its raw form.
Parameters
| Type | Name | Description |
|---|---|---|
System.UInt16[] | data | The array of ushort register values. |
| Meadow.Modbus.ModbusPolledDevice.SourceFormat | format | The 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
public event EventHandler? CommTimeout
Event Type
System.EventHandler
CommError
Raised when a communication error with the target device occurs
View Source
public event EventHandler<Exception>? CommError
Event Type
System.EventHandler<System.Exception>
DataUpdated
Raised when data has been read from the device
View Source
public event EventHandler? DataUpdated
Event Type
System.EventHandler