ModbusMaster Class
Namespace: GHIElectronics.TinyCLR.Devices.Modbus · Assembly: GHIElectronics.TinyCLR.Devices.Modbus
ModbusMaster provides the function code implementaions for a modbus master.
public class ModbusMaster
Constructors
ModbusMaster(IModbusInterface intf, object syncObject)
public ModbusMaster(IModbusInterface intf, object syncObject = null)
Creates a modbus master using the given interface
| Parameter | Type | Description |
|---|---|---|
intf | IModbusInterface | Interface to use for the master |
syncObject | object | Object to use for multi threaded communication locking. |
Methods
SendReceive(byte deviceAddress, ModbusFunctionCode fc, int timeout, short telegramLength, short desiredDataLength, object telegramContext, ref short dataPos)
protected virtual short SendReceive(byte deviceAddress, ModbusFunctionCode fc, int timeout, short telegramLength, short desiredDataLength, object telegramContext, ref short dataPos)
Sends a modbus telegram over the interface and waits for a response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Modbus device address. |
fc | ModbusFunctionCode | Function code. |
timeout | int | Timeout in Milli seconds. |
telegramLength | short | Total length of the telegram in bytes |
desiredDataLength | short | Length of the desired telegram data (without fc, cs, ...) of the response in bytes. -1 for unknown. |
telegramContext | object | Interface specific context of the telegram |
dataPos | ref short | Index of the response data in the buffer. |
Returns short
ReadCoils(byte deviceAddress, ushort startAddress, ushort coilCount, int timeout)
public virtual byte[] ReadCoils(byte deviceAddress, ushort startAddress, ushort coilCount, int timeout = 2000)
Sends a Read Coils (0x01) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
startAddress | ushort | Start address: 0x0000 .. 0xFFFF |
coilCount | ushort | >Number of coils to read: 1 .. 2000 |
timeout | int | Timeout for response in Milli seconds. |
Returns byte[] — Returns a byte array which contains the coils. The coils are written as single bits into the array starting with coil 1 at the lsb.
ReadDiscreteInputs(byte deviceAddress, ushort startAddress, ushort inputCount, int timeout)
public virtual byte[] ReadDiscreteInputs(byte deviceAddress, ushort startAddress, ushort inputCount, int timeout = 2000)
Sends a Read Discrete Inputs (0x02) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
startAddress | ushort | Start address: 0x0000 .. 0xFFFF |
inputCount | ushort | >Number of coils to read: 1 .. 2000 |
timeout | int | Timeout for response in milli seconds. |
Returns byte[] — Returns a byte array which contains the inputs. The inputs are written as single bits into the array starting with coil 1 at the lsb.
ReadHoldingRegisters(byte deviceAddress, ushort startAddress, ushort registerCount, int timeout)
public virtual ushort[] ReadHoldingRegisters(byte deviceAddress, ushort startAddress, ushort registerCount, int timeout = 2000)
Sends a Read Holding Registers (0x03) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
startAddress | ushort | Start address: 0x0000 .. 0xFFFF |
registerCount | ushort | Number of the registers to read. |
timeout | int | Timeout for response in milli seconds. |
Returns ushort[] — Returns an array of 16 bit register values.
ReadInputRegisters(byte deviceAddress, ushort startAddress, ushort registerCount, int timeout)
public virtual ushort[] ReadInputRegisters(byte deviceAddress, ushort startAddress, ushort registerCount, int timeout = 2000)
Sends a Read Input Registers (0x04) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
startAddress | ushort | Start address: 0x0000 .. 0xFFFF |
registerCount | ushort | Number of the registers to read. |
timeout | int | Timeout for response in milli seconds. |
Returns ushort[] — Returns an array of 16 bit register values.
WriteSingleCoil(byte deviceAddress, ushort address, bool value, int timeout)
public virtual void WriteSingleCoil(byte deviceAddress, ushort address, bool value, int timeout = 2000)
Sends a Write Single Coils (0x05) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
address | ushort | Address of the coil: 0x0000 .. 0xFFFF |
value | bool | true or false |
timeout | int | Timeout for response in milli seconds. |
WriteSingleCoil(byte deviceAddress, ushort address, ushort value, int timeout)
public virtual void WriteSingleCoil(byte deviceAddress, ushort address, ushort value, int timeout = 2000)
Sends a Write Single Coils (0x05) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
address | ushort | Address of the coil: 0x0000 .. 0xFFFF |
value | ushort | 0xFF00 for true or 0x0000 for false |
timeout | int | Timeout for response in milli seconds. |
WriteSingleRegister(byte deviceAddress, ushort address, ushort value, int timeout)
public virtual void WriteSingleRegister(byte deviceAddress, ushort address, ushort value, int timeout = 2000)
Sends a Write Single Register (0x06) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
address | ushort | Address of the register: 0x0000 .. 0xFFFF |
value | ushort | Register value to write |
timeout | int | Timeout for response in milli seconds. |
ReadExceptionStatus(byte deviceAddress, int timeout)
public virtual byte ReadExceptionStatus(byte deviceAddress, int timeout = 2000)
Sends a Read Exception Status (0x07) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
timeout | int | Timeout for response in milli seconds. |
Returns byte — Returns 8 bit encoded exception statuses of the device.
Diagnostics(byte deviceAddress, ushort subFunction, ushort[] data, int timeout)
public virtual ushort[] Diagnostics(byte deviceAddress, ushort subFunction, ushort[] data, int timeout = 2000)
Sends a Diagnostics (0x08) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
subFunction | ushort | Sub function code. |
data | ushort[] | Data |
timeout | int | Timeout for response in milli seconds. |
Returns ushort[] — Returns a unsigned short array with diagnostic data.
GetCommEventCounter(byte deviceAddress, out ushort status, out ushort eventCount, int timeout)
public virtual void GetCommEventCounter(byte deviceAddress, out ushort status, out ushort eventCount, int timeout = 2000)
Sends a Get Comm Event Counter (0x0B) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
status | out ushort | Receives the status word of the device. |
eventCount | out ushort | Receives the event counter of the device. |
timeout | int | Timeout for response in milli seconds. |
GetCommEventLog(byte deviceAddress, out ushort status, out ushort eventCount, out ushort messageCount, out byte[] events, int timeout)
public virtual void GetCommEventLog(byte deviceAddress, out ushort status, out ushort eventCount, out ushort messageCount, out byte[] events, int timeout = 2000)
Sends a Get Comm Event Log (0x0C) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
status | out ushort | Receives the status word of the device. |
eventCount | out ushort | Receives the event counter of the device. |
messageCount | out ushort | Receives the event message count. |
events | out byte[] | Receives 0 - 64 bytes, with each byte corresponding to the status of one MODBUS send or receive operation for the remote device. Byte 0 is the most recent. |
timeout | int | Timeout for response in milli seconds. |
WriteMultipleCoils(byte deviceAddress, ushort startAddress, ushort coilCount, byte[] coils, int timeout)
public virtual void WriteMultipleCoils(byte deviceAddress, ushort startAddress, ushort coilCount, byte[] coils, int timeout = 2000)
Sends a Write Multiple Coils (0x0F) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
startAddress | ushort | Address of the 1st coil: 0x0000 .. 0xFFFF |
coilCount | ushort | Number of coils to write. |
coils | byte[] | Byte array with bit coded coil values. |
timeout | int | Timeout for response in milli seconds. |
WriteMultipleRegisters(byte deviceAddress, ushort startAddress, ushort[] registers, int timeout)
public virtual void WriteMultipleRegisters(byte deviceAddress, ushort startAddress, ushort[] registers, int timeout = 2000)
Sends a Write Multiple Coils (0x10) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
startAddress | ushort | Address of the 1st register: 0x0000 .. 0xFFFF |
registers | ushort[] | Register values to write |
timeout | int | Timeout for response in milli seconds. |
WriteMultipleRegisters(byte deviceAddress, ushort startAddress, ushort[] registers, int offset, int count, int timeout)
public virtual void WriteMultipleRegisters(byte deviceAddress, ushort startAddress, ushort[] registers, int offset, int count, int timeout = 2000)
Sends a Write Multiple Coils (0x10) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
startAddress | ushort | Address of the 1st register: 0x0000 .. 0xFFFF |
registers | ushort[] | Register values to write |
offset | int | Offset in register of the 1st register to write |
count | int | Number of registers to write |
timeout | int | Timeout for response in milli seconds. |
ReadWriteMultipleRegisters(byte deviceAddress, ushort writeStartAddress, ushort[] writeRegisters, ushort readStartAddress, ushort readCount, int timeout)
public virtual ushort[] ReadWriteMultipleRegisters(byte deviceAddress, ushort writeStartAddress, ushort[] writeRegisters, ushort readStartAddress, ushort readCount, int timeout = 2000)
Sends a Read Write Multiple Coils (0x17) telegram to a device and waits for the response
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
writeStartAddress | ushort | Address of the 1st write register: 0x0000 .. 0xFFFF |
writeRegisters | ushort[] | Register values to write |
readStartAddress | ushort | Address of the 1st read register: 0x0000 .. 0xFFFF |
readCount | ushort | Number of registers to read |
timeout | int | Timeout for response in milli seconds. |
Returns ushort[] — Returns a unsigned short array with the register values.
ReadDeviceIdentification(byte deviceAddress, ModbusConformityLevel deviceIdCode, int timeout)
public virtual DeviceIdentification[] ReadDeviceIdentification(byte deviceAddress, ModbusConformityLevel deviceIdCode, int timeout = 2000)
Reads the Device Information by using the function code (0x2b / 0x0e). The methods sends as many messages as needed to read the whole data.
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
deviceIdCode | ModbusConformityLevel | Device id code to read. |
timeout | int | Timeout for response in milli seconds. |
Returns DeviceIdentification[] — Returns an array with all device information.
ReadDeviceIdentification(byte deviceAddress, ModbusConformityLevel deviceIdCode, ref ModbusObjectId objectId, out bool moreFollows, int timeout)
public virtual DeviceIdentification[] ReadDeviceIdentification(byte deviceAddress, ModbusConformityLevel deviceIdCode, ref ModbusObjectId objectId, out bool moreFollows, int timeout = 2000)
Reads the Device Information by using the function code (0x2b / 0x0e)
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
deviceIdCode | ModbusConformityLevel | Device id code to read. |
objectId | ref ModbusObjectId | Object id to start at. Receives the next object id to read from if moreFollows is set to true. |
moreFollows | out bool | Receives true if there is more information to read. |
timeout | int | Timeout for response in milli seconds. |
Returns DeviceIdentification[] — Returns an array with all device information.
ReadSpecificDeviceIdentification(byte deviceAddress, ModbusObjectId objectId, int timeout)
public virtual DeviceIdentification ReadSpecificDeviceIdentification(byte deviceAddress, ModbusObjectId objectId, int timeout = 4000)
Reads one specific Device Information by using the function code (0x2b / 0x0e)
| Parameter | Type | Description |
|---|---|---|
deviceAddress | byte | Address of the modbus device. |
objectId | ModbusObjectId | Object id to read. |
timeout | int | Timeout for response in milli seconds. |
Returns DeviceIdentification — Returns the requested device information.