Class ModbusClientBase
Base class for a Modbus client.
Assembly: Meadow.Modbus.dll
View Source
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
public bool IsDisposed { get; }
IsConnected
Gets or sets a value indicating whether the client is connected to the Modbus device.
View Source
public virtual bool IsConnected { get; protected set; }
Timeout
Gets or sets the Timeout value for reading / writing to a Modbus device.
View Source
public TimeSpan Timeout { get; protected set; }
Methods
GenerateReportMessage(byte)
Generates the message for requesting a device report its identifier
View Source
protected abstract byte[] GenerateReportMessage(byte modbusAddress)
Returns
System.Byte[]
Parameters
| Type | Name |
|---|---|
System.Byte | modbusAddress |
GenerateWriteMessage(byte, ModbusFunction, ushort, byte[])
Generates the message for writing data to a Modbus device.
View Source
protected abstract byte[] GenerateWriteMessage(byte modbusAddress, ModbusFunction function, ushort register, byte[] data)
Returns
System.Byte[]
Parameters
| Type | Name |
|---|---|
System.Byte | modbusAddress |
| Meadow.Modbus.ModbusFunction | function |
System.UInt16 | register |
System.Byte[] | data |
GenerateReadMessage(byte, ModbusFunction, ushort, int)
Generates the message for reading data from a Modbus device.
View Source
protected abstract byte[] GenerateReadMessage(byte modbusAddress, ModbusFunction function, ushort startRegister, int registerCount)
Returns
System.Byte[]
Parameters
| Type | Name |
|---|---|
System.Byte | modbusAddress |
| Meadow.Modbus.ModbusFunction | function |
System.UInt16 | startRegister |
System.Int32 | registerCount |
DeliverMessage(byte[])
Delivers the Modbus message to the device.
View Source
protected abstract Task DeliverMessage(byte[] message)
Returns
System.Threading.Tasks.Task
Parameters
| Type | Name |
|---|---|
System.Byte[] | message |
ReadResult(ModbusFunction, byte)
Reads the result of the Modbus function.
View Source
protected abstract Task<byte[]> ReadResult(ModbusFunction function, byte expectedAddress)
Returns
System.Threading.Tasks.Task<System.Byte[]>
Parameters
| Type | Name |
|---|---|
| Meadow.Modbus.ModbusFunction | function |
System.Byte | expectedAddress |
Connect()
Connects to the Modbus device.
View Source
public abstract Task Connect()
Returns
System.Threading.Tasks.Task
Disconnect()
Disconnects from the Modbus device.
View Source
public abstract void Disconnect()
DisposeManagedResources()
Releases the managed resources used by the Modbus client.
View Source
protected virtual void DisposeManagedResources()
DisposeUnmanagedResources()
Releases the unmanaged resources used by the Modbus client.
View Source
protected virtual void DisposeUnmanagedResources()
Dispose(bool)
Disposes of the Modbus client.
View Source
protected virtual void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
System.Boolean | disposing | True if called from user code; false if called from the finalizer. |
Dispose()
Disposes of the Modbus client.
View Source
public void Dispose()
WriteHoldingRegister(byte, ushort, ushort, bool)
Writes a single value to a holding register on the Modbus device.
View Source
public Task WriteHoldingRegister(byte modbusAddress, ushort register, ushort value, bool addressIsLogical = false)
Returns
System.Threading.Tasks.Task
Parameters
| Type | Name | Description |
|---|---|---|
System.Byte | modbusAddress | The Modbus device address. |
System.UInt16 | register | The register number to write to. |
System.UInt16 | value | The value to write to the register. |
System.Boolean | addressIsLogical | If 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
public Task WriteHoldingRegisters(byte modbusAddress, ushort startRegister, IEnumerable<ushort> values, bool addressIsLogical = false)
Returns
System.Threading.Tasks.Task
Parameters
| Type | Name | Description |
|---|---|---|
System.Byte | modbusAddress | The Modbus device address. |
System.UInt16 | startRegister | The starting register number to write to. |
System.Collections.Generic.IEnumerable<System.UInt16> | values | The values to write to the registers. |
System.Boolean | addressIsLogical | If 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
public Task<float[]> ReadHoldingRegistersFloat(byte modbusAddress, ushort startRegister, int floatCount)
Returns
System.Threading.Tasks.Task<System.Single[]>: An array of floating point values.
Parameters
| Type | Name | Description |
|---|---|---|
System.Byte | modbusAddress | The Modbus device address. |
System.UInt16 | startRegister | The starting register number to read from. |
System.Int32 | floatCount | The number of floating point values to read. |
ReadHoldingRegisters(byte, ushort, int, bool)
Reads holding registers from the Modbus device.
View Source
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
| Type | Name | Description |
|---|---|---|
System.Byte | modbusAddress | The Modbus device address. |
System.UInt16 | startRegister | The starting register number to read from. |
System.Int32 | registerCount | The number of registers to read. |
System.Boolean | addressIsLogical | If the address is a Modbus logical address, 40001 will be subtracted to get the register address. |
ReadDeviceId(byte)
View Source
public Task<byte[]> ReadDeviceId(byte modbusAddress)
Returns
System.Threading.Tasks.Task<System.Byte[]>
Parameters
| Type | Name |
|---|---|
System.Byte | modbusAddress |
ReadInputRegisters(byte, ushort, int, bool)
Reads input registers from the Modbus device.
View Source
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
| Type | Name | Description |
|---|---|---|
System.Byte | modbusAddress | The Modbus device address. |
System.UInt16 | startRegister | The starting register number to read from. |
System.Int32 | registerCount | The number of registers to read. |
System.Boolean | addressIsLogical | If 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
public Task WriteCoil(byte modbusAddress, ushort register, bool value)
Returns
System.Threading.Tasks.Task: A System.Threading.Tasks.Task representing the asynchronous operation.
Parameters
| Type | Name | Description |
|---|---|---|
System.Byte | modbusAddress | The Modbus address of the device. |
System.UInt16 | register | The register to write to. |
System.Boolean | value | The value to write. |
WriteMultipleCoils(byte, ushort, IEnumerable<bool>)
Writes multiple coil values to the given registers on a device.
View Source
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
| Type | Name | Description |
|---|---|---|
System.Byte | modbusAddress | The Modbus address of the device. |
System.UInt16 | startRegister | The first register to begin writing. |
System.Collections.Generic.IEnumerable<System.Boolean> | values | The coil values to write. |
ReadCoils(byte, ushort, int)
Reads the requested number of coils from a device.
View Source
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
| Type | Name | Description |
|---|---|---|
System.Byte | modbusAddress | The Modbus address of the device. |
System.UInt16 | startCoil | The first coil to begin reading. |
System.Int32 | coilCount | The number of coils to read. |
Events
Disconnected
Event triggered when the client is disconnected.
View Source
public event EventHandler Disconnected
Event Type
System.EventHandler
Connected
Event triggered when the client is connected.
View Source
public event EventHandler Connected
Event Type
System.EventHandler
Implements
- Meadow.Modbus.IModbusBusClient
System.IDisposable