Skip to main content

Class ModbusClientBase

Base class for a Modbus client.

Assembly: Meadow.Modbus.dll​
View Source​
Declaration
public abstract class ModbusClientBase : IModbusBusClient, IDisposable

Derived:
Meadow.Modbus.ModbusRtuClient, Meadow.Modbus.ModbusTcpClient

Implements:
Meadow.Modbus.IModbusBusClient, System.IDisposable

Properties​

IsDisposed​

Gets a value indicating whether the client is disposed.

View Source​
Declaration
public bool IsDisposed { get; }

IsConnected​

Gets or sets a value indicating whether the client is connected to the Modbus device.

View Source​
Declaration
public virtual bool IsConnected { get; protected set; }

Timeout​

Gets or sets the Timeout value for reading / writing to a Modbus device.

View Source​
Declaration
public TimeSpan Timeout { get; protected set; }

Methods​

GenerateReportMessage(byte)​

Generates the message for requesting a device report its identifier

View Source​
Declaration
protected abstract byte[] GenerateReportMessage(byte modbusAddress)
Returns​

System.Byte[]

Parameters​
TypeName
System.BytemodbusAddress

GenerateWriteMessage(byte, ModbusFunction, ushort, byte[])​

Generates the message for writing data to a Modbus device.

View Source​
Declaration
protected abstract byte[] GenerateWriteMessage(byte modbusAddress, ModbusFunction function, ushort register, byte[] data)
Returns​

System.Byte[]

Parameters​
TypeName
System.BytemodbusAddress
Meadow.Modbus.ModbusFunctionfunction
System.UInt16register
System.Byte[]data

GenerateReadMessage(byte, ModbusFunction, ushort, int)​

Generates the message for reading data from a Modbus device.

View Source​
Declaration
protected abstract byte[] GenerateReadMessage(byte modbusAddress, ModbusFunction function, ushort startRegister, int registerCount)
Returns​

System.Byte[]

Parameters​
TypeName
System.BytemodbusAddress
Meadow.Modbus.ModbusFunctionfunction
System.UInt16startRegister
System.Int32registerCount

DeliverMessage(byte[])​

Delivers the Modbus message to the device.

View Source​
Declaration
protected abstract Task DeliverMessage(byte[] message)
Returns​

System.Threading.Tasks.Task

Parameters​
TypeName
System.Byte[]message

ReadResult(ModbusFunction, byte)​

Reads the result of the Modbus function.

View Source​
Declaration
protected abstract Task<byte[]> ReadResult(ModbusFunction function, byte expectedAddress)
Returns​

System.Threading.Tasks.Task<System.Byte[]>

Parameters​
TypeName
Meadow.Modbus.ModbusFunctionfunction
System.ByteexpectedAddress

Connect()​

Connects to the Modbus device.

View Source​
Declaration
public abstract Task Connect()
Returns​

System.Threading.Tasks.Task

Disconnect()​

Disconnects from the Modbus device.

View Source​
Declaration
public abstract void Disconnect()

DisposeManagedResources()​

Releases the managed resources used by the Modbus client.

View Source​
Declaration
protected virtual void DisposeManagedResources()

DisposeUnmanagedResources()​

Releases the unmanaged resources used by the Modbus client.

View Source​
Declaration
protected virtual void DisposeUnmanagedResources()

Dispose(bool)​

Disposes of the Modbus client.

View Source​
Declaration
protected virtual void Dispose(bool disposing)
Parameters​
TypeNameDescription
System.BooleandisposingTrue if called from user code; false if called from the finalizer.

Dispose()​

Disposes of the Modbus client.

View Source​
Declaration
public void Dispose()

WriteHoldingRegister(byte, ushort, ushort, bool)​

Writes a single value to a holding register on the Modbus device.

View Source​
Declaration
public Task WriteHoldingRegister(byte modbusAddress, ushort register, ushort value, bool addressIsLogical = false)
Returns​

System.Threading.Tasks.Task

Parameters​
TypeNameDescription
System.BytemodbusAddressThe Modbus device address.
System.UInt16registerThe register number to write to.
System.UInt16valueThe value to write to the register.
System.BooleanaddressIsLogicalIf the address is a Modbus logical address, 40001 will be subtracted to get the register address.

WriteHoldingRegisters(byte, ushort, IEnumerable<ushort>, bool)​

Writes multiple values to consecutive holding registers on the Modbus device.

View Source​
Declaration
public Task WriteHoldingRegisters(byte modbusAddress, ushort startRegister, IEnumerable<ushort> values, bool addressIsLogical = false)
Returns​

System.Threading.Tasks.Task

Parameters​
TypeNameDescription
System.BytemodbusAddressThe Modbus device address.
System.UInt16startRegisterThe starting register number to write to.
System.Collections.Generic.IEnumerable<System.UInt16>valuesThe values to write to the registers.
System.BooleanaddressIsLogicalIf the address is a Modbus logical address, 40001 will be subtracted to get the register address.

ReadHoldingRegistersFloat(byte, ushort, int)​

Reads floating point values from holding registers on the Modbus device.

View Source​
Declaration
public Task<float[]> ReadHoldingRegistersFloat(byte modbusAddress, ushort startRegister, int floatCount)
Returns​

System.Threading.Tasks.Task<System.Single[]>: An array of floating point values.

Parameters​
TypeNameDescription
System.BytemodbusAddressThe Modbus device address.
System.UInt16startRegisterThe starting register number to read from.
System.Int32floatCountThe number of floating point values to read.

ReadHoldingRegisters(byte, ushort, int, bool)​

Reads holding registers from the Modbus device.

View Source​
Declaration
public Task<ushort[]> ReadHoldingRegisters(byte modbusAddress, ushort startRegister, int registerCount, bool addressIsLogical = false)
Returns​

System.Threading.Tasks.Task<System.UInt16[]>: An array of ushort values representing the registers.

Parameters​
TypeNameDescription
System.BytemodbusAddressThe Modbus device address.
System.UInt16startRegisterThe starting register number to read from.
System.Int32registerCountThe number of registers to read.
System.BooleanaddressIsLogicalIf the address is a Modbus logical address, 40001 will be subtracted to get the register address.

ReadDeviceId(byte)​

View Source​
Declaration
public Task<byte[]> ReadDeviceId(byte modbusAddress)
Returns​

System.Threading.Tasks.Task<System.Byte[]>

Parameters​
TypeName
System.BytemodbusAddress

ReadInputRegisters(byte, ushort, int, bool)​

Reads input registers from the Modbus device.

View Source​
Declaration
public Task<ushort[]> ReadInputRegisters(byte modbusAddress, ushort startRegister, int registerCount, bool addressIsLogical = false)
Returns​

System.Threading.Tasks.Task<System.UInt16[]>: An array of ushort values representing the registers.

Parameters​
TypeNameDescription
System.BytemodbusAddressThe Modbus device address.
System.UInt16startRegisterThe starting register number to read from.
System.Int32registerCountThe number of registers to read.
System.BooleanaddressIsLogicalIf the address is a Modbus logical address, 30001 will be subtracted to get the register address.

WriteCoil(byte, ushort, bool)​

Writes a coil value to the given register on a device.

View Source​
Declaration
public Task WriteCoil(byte modbusAddress, ushort register, bool value)
Returns​

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

Parameters​
TypeNameDescription
System.BytemodbusAddressThe Modbus address of the device.
System.UInt16registerThe register to write to.
System.BooleanvalueThe value to write.

WriteMultipleCoils(byte, ushort, IEnumerable<bool>)​

Writes multiple coil values to the given registers on a device.

View Source​
Declaration
public Task WriteMultipleCoils(byte modbusAddress, ushort startRegister, IEnumerable<bool> values)
Returns​

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

Parameters​
TypeNameDescription
System.BytemodbusAddressThe Modbus address of the device.
System.UInt16startRegisterThe first register to begin writing.
System.Collections.Generic.IEnumerable<System.Boolean>valuesThe coil values to write.

ReadCoils(byte, ushort, int)​

Reads the requested number of coils from a device.

View Source​
Declaration
public Task<bool[]> ReadCoils(byte modbusAddress, ushort startCoil, int coilCount)
Returns​

System.Threading.Tasks.Task<System.Boolean[]>: An array of coil values read from the device.

Parameters​
TypeNameDescription
System.BytemodbusAddressThe Modbus address of the device.
System.UInt16startCoilThe first coil to begin reading.
System.Int32coilCountThe number of coils to read.

Events​

Disconnected​

Event triggered when the client is disconnected.

View Source​
Declaration
public event EventHandler Disconnected
Event Type​

System.EventHandler

Connected​

Event triggered when the client is connected.

View Source​
Declaration
public event EventHandler Connected
Event Type​

System.EventHandler

Implements​