Skip to main content

ModbusDevice Class

Namespace: GHIElectronics.TinyCLR.Devices.Modbus · Assembly: GHIElectronics.TinyCLR.Devices.Modbus

ModbusDevice is a base class for any modbus device.

public abstract class ModbusDevice

Constructors

ModbusDevice(byte deviceAddress, object syncObject)

protected ModbusDevice(byte deviceAddress, object syncObject = null)

Creates a new ModebusDevice without assigned interfaces.

ParameterTypeDescription
deviceAddressbyteDevice address. Must be between 1 and 247 for RTU and should be 248 for TCP.
syncObjectobjectOptional object for communication interface synchronization.

ModbusDevice(IModbusInterface intf, byte deviceAddress, object syncObject)

protected ModbusDevice(IModbusInterface intf, byte deviceAddress, object syncObject = null) : this(deviceAddress, syncObject)

Creates a new ModebusDevice with one initial interface.

ParameterTypeDescription
intfIModbusInterfaceInitial interface.
deviceAddressbyteDevice address. Must be between 1 and 247 for RTU and should be 248 for TCP.
syncObjectobjectOptional object for communication interface synchronization.

Properties

Buffer

protected byte[] Buffer { get; }

Gets the internal buffer

IsRunning

public bool IsRunning { get; set; }

Gets if the interface message polling is running.

Buffer1

public byte[] Buffer1 { get; set; }

No description available.

Buffer2

public byte[] Buffer2 { get; set; }

No description available.

Methods

AddInterface(IModbusInterface intf)

public void AddInterface(IModbusInterface intf)

Adds an additional interface to be polled for incoming messages.

ParameterTypeDescription
intfIModbusInterfaceInterface to add.

RemoveInterface(IModbusInterface intf)

public void RemoveInterface(IModbusInterface intf)

Removes an interface.

ParameterTypeDescription
intfIModbusInterfaceInterface to remove.

Start()

public void Start()

Start the interface message polling.

Stop()

public void Stop()

Stop the interface message polling.

OnMessageReceived(IModbusInterface modbusInterface, byte deviceAddress, ModbusFunctionCode functionCode)

protected virtual void OnMessageReceived(IModbusInterface modbusInterface, byte deviceAddress, ModbusFunctionCode functionCode)

Is called when ever a modbus message was received, no matter if it was for this device or not.

ParameterTypeDescription
modbusInterfaceIModbusInterfaceInterface by which the message was received
deviceAddressbyteAddress to which device the message was sent
functionCodeModbusFunctionCodeFunction code

OnHandleTelegram(IModbusInterface intf, byte deviceAddress, bool isBroadcast, short telegramLength, object telegramContext, ModbusFunctionCode fc, short dataPos, short dataLength)

protected virtual void OnHandleTelegram(IModbusInterface intf, byte deviceAddress, bool isBroadcast, short telegramLength, object telegramContext, ModbusFunctionCode fc, short dataPos, short dataLength)

Handles a received message.

ParameterTypeDescription
intfIModbusInterfaceInterface by which the message was received.
deviceAddressbyteAddress of the target device
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no response is sent.
telegramLengthshortLength of the message in bytes.
telegramContextobjectInterface specific message context.
fcModbusFunctionCodeFunction code.
dataPosshortIndex of function code specific data.
dataLengthshortLength of the function code specific data in bytes.

OnReadCoils(bool isBroadcast, ushort startAddress, ushort coilCount, byte[] coils)

protected virtual ModbusErrorCode OnReadCoils(bool isBroadcast, ushort startAddress, ushort coilCount, byte[] coils)

Is called when a ReadCoils request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
startAddressushortStart address: 0x0000 .. 0xFFFF
coilCountushortNumber of coils to read: 1 .. 2000
coilsbyte[]Byte array which must receive the coils. The coils are written as single bits into the array starting with coil 1 at the lsb.

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

OnReadDiscreteInputs(bool isBroadcast, ushort startAddress, ushort inputCount, byte[] inputs)

protected virtual ModbusErrorCode OnReadDiscreteInputs(bool isBroadcast, ushort startAddress, ushort inputCount, byte[] inputs)

Is called when a ReadDiscreteInputs request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
startAddressushortStart address: 0x0000 .. 0xFFFF
inputCountushortNumber of inputs to read: 1 .. 2000
inputsbyte[]Byte array which must receive the inputs. The inputs are written as single bits into the array starting with input 1 at the lsb.

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

OnReadHoldingRegisters(bool isBroadcast, ushort startAddress, ushort[] registers)

protected virtual ModbusErrorCode OnReadHoldingRegisters(bool isBroadcast, ushort startAddress, ushort[] registers)

Is called when a ReadHoldingRegisters request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
startAddressushortStart address: 0x0000 .. 0xFFFF
registersushort[]Array in which the read register values must be written.

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

OnReadInputRegisters(bool isBroadcast, ushort startAddress, ushort[] registers)

protected virtual ModbusErrorCode OnReadInputRegisters(bool isBroadcast, ushort startAddress, ushort[] registers)

Is called when a ReadInputRegisters request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
startAddressushortStart address: 0x0000 .. 0xFFFF
registersushort[]Array in which the read register values must be written.

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

OnWriteSingleCoil(bool isBroadcast, ushort address, bool value)

protected virtual ModbusErrorCode OnWriteSingleCoil(bool isBroadcast, ushort address, bool value)

Is called when a WriteSingleCoil request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
addressushortAddress of the coil: 0x0000 .. 0xFFFF
valueboolValue to write.

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

OnWriteSingleRegister(bool isBroadcast, ushort address, ushort value)

protected virtual ModbusErrorCode OnWriteSingleRegister(bool isBroadcast, ushort address, ushort value)

Is called when a WriteSingleRegister request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
addressushortAddress of the register: 0x0000 .. 0xFFFF
valueushortRegister value to write

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

OnWriteMultipleCoils(bool isBroadcast, ushort startAddress, ushort outputCount, byte[] values)

protected virtual ModbusErrorCode OnWriteMultipleCoils(bool isBroadcast, ushort startAddress, ushort outputCount, byte[] values)

Is called when a WriteMultipleCoils request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
startAddressushortStart address: 0x0000 .. 0xFFFF
outputCountushortNumber of couils (bist) to write.
valuesbyte[]Values to write. Each bit is a value, starting at the lsb.

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

OnWriteMultipleRegisters(bool isBroadcast, ushort startAddress, ushort[] registers)

protected virtual ModbusErrorCode OnWriteMultipleRegisters(bool isBroadcast, ushort startAddress, ushort[] registers)

Is called when a WriteMultipleRegisters request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
startAddressushortStart address: 0x0000 .. 0xFFFF
registersushort[]Registers to write

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

OnReadWriteMultipleRegisters(bool isBroadcast, ushort writeStartAddress, ushort[] writeRegisters, ushort readStartAddress, ushort[] readRegisters)

protected virtual ModbusErrorCode OnReadWriteMultipleRegisters(bool isBroadcast, ushort writeStartAddress, ushort[] writeRegisters, ushort readStartAddress, ushort[] readRegisters)

Is called when a ReadWriteMultipleRegisters request comes in.

ParameterTypeDescription
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
writeStartAddressushortStart address of writeRegisters: 0x0000 .. 0xFFFF
writeRegistersushort[]Registers to write
readStartAddressushortStart address readRegisters: 0x0000 .. 0xFFFF
readRegistersushort[]Array to write the read registers into.

Returns ModbusErrorCode — Returns ModbusErrorCode.NoError on success or any other ModbusErrorCode on errors.

ReadDeviceIdentification(IModbusInterface intf, bool isBroadcast, object telegramContext, ModbusFunctionCode fc, short dataPos, short dataLength)

protected virtual void ReadDeviceIdentification(IModbusInterface intf, bool isBroadcast, object telegramContext, ModbusFunctionCode fc, short dataPos, short dataLength)

Is called when the device identification of this devuice is requested.

ParameterTypeDescription
intfIModbusInterfaceInterface from wich the requst was received
isBroadcastbooltrue if request is a broadcast
telegramContextobjectConext of the telegram
fcModbusFunctionCodeFunction code
dataPosshortPosittion (offset) of the data in the buffer
dataLengthshortLength of the data in the buffer

OnGetDeviceIdentification(ModbusObjectId objectId)

protected abstract string OnGetDeviceIdentification(ModbusObjectId objectId)

Is called when the value of the object id is needed for a ReadDeviceIdentification request.

ParameterTypeDescription
objectIdModbusObjectIdObject id.

Returns string — Returns the value of the requested object id.

GetConformityLevel()

protected abstract ModbusConformityLevel GetConformityLevel()

Is called when the maximum conformity level is needed for a ReadDeviceIdentification request.

Returns ModbusConformityLevel — Returns the maximum conformity level which is provided by this device.

OnCustomTelegram(IModbusInterface intf, bool isBroadcast, byte[] buffer, short telegramLength, object telegramContext, ModbusFunctionCode fc, short dataPos, short dataLength)

protected virtual bool OnCustomTelegram(IModbusInterface intf, bool isBroadcast, byte[] buffer, short telegramLength, object telegramContext, ModbusFunctionCode fc, short dataPos, short dataLength)

OnCustomTelegram is called for any function code which is not explicitly handeled by a On"FunctionCode" methos.

ParameterTypeDescription
intfIModbusInterfaceInterface which sent the request.
isBroadcastbooltrue if the request is a broadcast
bufferbyte[]Buffer containing the message.
telegramLengthshortTotal length of message in bytes.
telegramContextobjectInterface specific message context.
fcModbusFunctionCodeFunction code.
dataPosshortIndex of the function code specific data.
dataLengthshortLength of the function code specific data in bytes.

Returns bool

SendErrorResult(IModbusInterface intf, bool isBroadcast, byte deviceAddress, object telegramContext, ModbusFunctionCode fc, ModbusErrorCode modbusErrorCode)

protected virtual void SendErrorResult(IModbusInterface intf, bool isBroadcast, byte deviceAddress, object telegramContext, ModbusFunctionCode fc, ModbusErrorCode modbusErrorCode)

Sends a error result message.

ParameterTypeDescription
intfIModbusInterfaceInterface to send message to.
isBroadcastbooltrue if the message is a broadcast. For broadcast messages no reponse is sent.
deviceAddressbyteDevice address for response
telegramContextobjectInterface specific telegram context.
fcModbusFunctionCodeFunction code. The msg is automatically set.
modbusErrorCodeModbusErrorCodeModbus error code to send.