Skip to main content

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

ParameterTypeDescription
intfIModbusInterfaceInterface to use for the master
syncObjectobjectObject 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

ParameterTypeDescription
deviceAddressbyteModbus device address.
fcModbusFunctionCodeFunction code.
timeoutintTimeout in Milli seconds.
telegramLengthshortTotal length of the telegram in bytes
desiredDataLengthshortLength of the desired telegram data (without fc, cs, ...) of the response in bytes. -1 for unknown.
telegramContextobjectInterface specific context of the telegram
dataPosref shortIndex 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
startAddressushortStart address: 0x0000 .. 0xFFFF
coilCountushort>Number of coils to read: 1 .. 2000
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
startAddressushortStart address: 0x0000 .. 0xFFFF
inputCountushort>Number of coils to read: 1 .. 2000
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
startAddressushortStart address: 0x0000 .. 0xFFFF
registerCountushortNumber of the registers to read.
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
startAddressushortStart address: 0x0000 .. 0xFFFF
registerCountushortNumber of the registers to read.
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
addressushortAddress of the coil: 0x0000 .. 0xFFFF
valuebooltrue or false
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
addressushortAddress of the coil: 0x0000 .. 0xFFFF
valueushort0xFF00 for true or 0x0000 for false
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
addressushortAddress of the register: 0x0000 .. 0xFFFF
valueushortRegister value to write
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
subFunctionushortSub function code.
dataushort[]Data
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
statusout ushortReceives the status word of the device.
eventCountout ushortReceives the event counter of the device.
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
statusout ushortReceives the status word of the device.
eventCountout ushortReceives the event counter of the device.
messageCountout ushortReceives the event message count.
eventsout 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.
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
startAddressushortAddress of the 1st coil: 0x0000 .. 0xFFFF
coilCountushortNumber of coils to write.
coilsbyte[]Byte array with bit coded coil values.
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
startAddressushortAddress of the 1st register: 0x0000 .. 0xFFFF
registersushort[]Register values to write
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
startAddressushortAddress of the 1st register: 0x0000 .. 0xFFFF
registersushort[]Register values to write
offsetintOffset in register of the 1st register to write
countintNumber of registers to write
timeoutintTimeout 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

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
writeStartAddressushortAddress of the 1st write register: 0x0000 .. 0xFFFF
writeRegistersushort[]Register values to write
readStartAddressushortAddress of the 1st read register: 0x0000 .. 0xFFFF
readCountushortNumber of registers to read
timeoutintTimeout 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.

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
deviceIdCodeModbusConformityLevelDevice id code to read.
timeoutintTimeout 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)

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
deviceIdCodeModbusConformityLevelDevice id code to read.
objectIdref ModbusObjectIdObject id to start at. Receives the next object id to read from if moreFollows is set to true.
moreFollowsout boolReceives true if there is more information to read.
timeoutintTimeout 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)

ParameterTypeDescription
deviceAddressbyteAddress of the modbus device.
objectIdModbusObjectIdObject id to read.
timeoutintTimeout for response in milli seconds.

Returns DeviceIdentification — Returns the requested device information.