Skip to main content

ModbusRtuInterface Class

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

ModbusRtuInterface is a Modbus RTU implemention of the IModbusInterface interface to be used with ModbusMaster or ModbusDevice.

public class ModbusRtuInterface : IModbusInterface

Constructors

ModbusRtuInterface(UartController serial, int baudRate, int dataBits, UartStopBitCount stopBits, UartParity parity, short maxDataLength)

public ModbusRtuInterface(UartController serial, int baudRate, int dataBits, UartStopBitCount stopBits, UartParity parity, short maxDataLength = 252)

Creates a new Modbus RTU interface using an existing and fully initialized UartController. This interface can be used with ModbusMaster or ModbusDevice.

ParameterTypeDescription
serialUartControllerFully initialized UartController.
baudRateint
dataBitsint
stopBitsUartStopBitCount
parityUartParity
maxDataLengthshortMaximum number of data bytes

Properties

UartController

public UartController UartController { get; }

Gets the serial port which is used.

MaxDataLength

public short MaxDataLength { get; set; }

Gets the maximum data length (not including address, function code, ...) of e telegram.

MaxTelegramLength

public short MaxTelegramLength { get; set; }

Gets the maximum length of a Modbus telegram.

IsDataAvailable

public bool IsDataAvailable { get; }

Gets if there is currently dataavailable on the interface.

IsConnectionOk

public bool IsConnectionOk { get; }

Gets if the connection is ok

Methods

CreateTelegram(byte addr, byte fkt, short dataLength, byte[] buffer, out short telegramLength, out short dataPos, bool isResponse, ref object telegramContext)

public void CreateTelegram(byte addr, byte fkt, short dataLength, byte[] buffer, out short telegramLength, out short dataPos, bool isResponse, ref object telegramContext)

Creates a new telegram for a modbus request or response. All data except the function code specific user data is written into the given buffer.

ParameterTypeDescription
addrbyteDevice address. 0 = Breadcast, 1..247 are valid device addresses.
fktbyteFunction code. ModbusFunctionCode
dataLengthshortNumber of bytes for function code sspecific user data.
bufferbyte[]Buffer to write data into. The buffer must be at least MaxTelegramLength - MaxDataLength + dataLength bytes long.
telegramLengthout shortReturns the total length of the telegram in bytes.
dataPosout shortReturns the offset of the function code specific user data in buffer.
isResponsebooltrue if this is a response telegram; false if this is a request telegram.
telegramContextref objectIf isResponse == false, this parameter returns the interface implementation specific data which must be passed to the ParseTelegram method of the received response. If isResponse == true, this parameter must be called with the telegramContext parameter returned by ParseTelegram of the request telegram.

PrepareWrite()

public void PrepareWrite()

No description available.

PrepareRead()

public void PrepareRead()

No description available.

SendTelegram(byte[] buffer, short telegramLength)

public void SendTelegram(byte[] buffer, short telegramLength)

Sends the given telegram. If necessary additional information like a checksum can be inserted here.

ParameterTypeDescription
bufferbyte[]Buffer containing the data.
telegramLengthshortLength of the telegram in bytes.

ReceiveTelegram(byte[] buffer, short desiredDataLength, int timeout, out short telegramLength)

public bool ReceiveTelegram(byte[] buffer, short desiredDataLength, int timeout, out short telegramLength)

Waits and receives a telegram.

ParameterTypeDescription
bufferbyte[]Buffer to write data into.
desiredDataLengthshortDesired length of the function code specific data in bytes. -1 if length is unknown.
timeoutintTimeout in milliseconds to wait for the telegram.
telegramLengthout shortReturns the total length of the telegram in bytes.

Returns bool — Returns true if the telegram was received successfully; false on timeout.

ParseTelegram(byte[] buffer, short telegramLength, bool isResponse, ref object telegramContext, out byte address, out byte fkt, out short dataPos, out short dataLength)

public bool ParseTelegram(byte[] buffer, short telegramLength, bool isResponse, ref object telegramContext, out byte address, out byte fkt, out short dataPos, out short dataLength)

Parses a telegram received by ReceiveTelegram.

ParameterTypeDescription
bufferbyte[]Buffer containing the data.
telegramLengthshortTotal length of the telegram in bytes.
isResponsebooltrue if the telegram is a response telegram; false if the telegram is a request telegram.
telegramContextref objectIf isResponse == true: pass the telegramContext returned by CreateTelegram from the request. If isResponse == false: returns the telegramContext from the received request. It must pe passed to the CreateTelegram method for the response.
addressout byteReturns the device address.
fktout byteReturns the function code.
dataPosout shortReturns the offset in buffer of the function code specific data.
dataLengthout shortReturns the length of the function code specific data.

Returns bool — Returns true if this is the matching response according to the telegramContext; else false. If isResponse == false this method should return always true.

ClearInputBuffer()

public void ClearInputBuffer()

Removes all data from the input interface.

Fields

isOpen

public bool isOpen

No description available.