.NET ModbusClient – Methods

Namespace:  EasyModbus
Class:  ModbusClient


Constructor  ModbusClient(string ipAddress, int port)

Constructor for Modbus TCP and Modbus UDP connection.
string ipAddress: Ip- Address of the Modbus TCP Server
int port: Port of the Modbus TCP Server listening (Standard is 502)


Constructor  ModbusClient( string serialPort)

Constructor for serial connection (Modbus RTU).
String serialPort: Name of the serial port to be used (e.g. “COM1”)


Constructor  ModbusClient( )

Parameterless constructor. Needed parameters has to be changed using properties.


void Connect()

Connects to the Modbus Server (Modbus Master for RTU) with the previous selected properties.


void Connect (string ipAddress, int port)

Connects to the specified Modbus TCP Server
string ipAddress: Ip- Address of the Modbus TCP Server
int port: Port of the Modbus TCP Server listening (Standard is 502)


 bool[] ReadDiscreteInputs(int startingAddress, int quantity)

Read Discrete Inputs from Master device (Function code 2)
startingAddress: First discrete input to be read
quantity: Numer of discrete Inputs to be read
returns: Boolean Array [0..quantity-1] which contains the discrete Inputs


bool[] ReadCoils(int startingAddress, int quantity)

Read Coils from Master device (Function code 1)
startingAddress:  First coil to be read
quantity: Numer of coils to be read
returns:  Boolean Array [0..quantity-1] which contains the coils


int[] ReadHoldingRegisters(int startingAddress, int quantity)

Read Holding Registers from Master device (Function code 3)
startingAddress: First holding register to be read
quantity:  Number of holding registers to be read
returns:  Int Array [0..quantity-1] which contains the holding registers


int[] ReadInputRegisters(int startingAddress, int quantity)

Read Input Registers from Master device (Function code 4)
startingAddress :  First input register to be read
quantity:  Number of input registers to be read
returns:  Int Array [0..quantity-1] which contains the input registers


void WriteSingleCoil(int startingAddress, bool value)

Write single Coil to Master device (Function code 5)
startingAddress: Coil to be written
value:  Coil Value to be written


void WriteSingleRegister(int startingAddress, int value)

Write single Register to Master device (Function code 6)
startingAddress:  Register to be written
value: Register Value to be written


WriteMultipleCoils(int startingAddress, bool[] values)

Write multiple coils to Master device (Function code 15)
startingAddress :  First coil to be written
values:  Coil Values [0..quantity-1] to be written


WriteMultipleRegisters(int startingAddress, int[] values)

Write multiple registers to Master device (Function code 16)
startingAddress: First register to be written
values:  Register Values [0..quantity-1] to be written


int[] ReadWriteMultipleRegisters(int startingAddressRead, int quantityRead, int startingAddressWrite, int[] values)

Read/Write Multiple Registers (Function code 23)
startingAddressRead:  First input register to read
quantityRead:  Number of input registers to read
startingAddressWrite:  First input register to write
values: Values to write
returns:  Int Array which contains the holding registers [0..quantityRead-1]


public bool[] ReadDiscreteInputs(int startingAddress, int quantity, string mqttBrokerAddress)

Read Discrete Inputs from Server device (FC2) and publishes the values to a MQTT-Broker.
The Topic will be easymodbusclient/discreteinputs/’address’ e.g. easymodbusclient/discreteinputs/0 for address “0”.
Note that the Address that will be publishes is “0”-Based. The Root topic can be changed using the Parameter
By default we are using the Standard-Port 1883. This Port can be changed using the Property “MqttBrokerPort”
A Username and Passowrd can be provided using the Properties “MqttUserName” and “MqttPassword”
‘MqttRootTopic’ Default is ‘easymodbusclient’


public bool[] ReadCoils(int startingAddress, int quantity, string mqttBrokerAddress)
Read coils from Server device (FC1) and publishes the values to a MQTT-Broker.
The Topic will be easymodbusclient/coils/’address’ e.g. easymodbusclient/coils/0 for address “0”.
Note that the Address that will be publishes is “0”-Based. The Root topic can be changed using the Parameter
‘MqttRootTopic’ Default is ‘easymodbusclient’
By default we are using the Standard-Port 1883. This Port can be changed using the Property “MqttBrokerPort”
A Username and Passowrd can be provided using the Properties “MqttUserName” and “MqttPassword”


public int[] ReadHoldingRegisters(int startingAddress, int quantity, string mqttBrokerAddress)

Read Holding Registers from Server device (FC3) and publishes the values to a MQTT-Broker.
The Topic will be easymodbusclient/holdingregisters/’address’ e.g. easymodbusclient/holdingregisters/0 for address “0”.
Note that the Address that will be publishes is “0”-Based. The Root topic can be changed using the Parameter
‘MqttRootTopic’ Default is ‘easymodbusclient’
By default we are using the Standard-Port 1883. This Port can be changed using the Property “MqttBrokerPort”
A Username and Passowrd can be provided using the Properties “MqttUserName” and “MqttPassword”


public int[] ReadInputRegisters(int startingAddress, int quantity, string mqttBrokerAddress)

Read Input Registers from Server device (FC4) and publishes the values to a MQTT-Broker.
The Topic will be easymodbusclient/inputregisters/’address’ e.g. easymodbusclient/inputregisters/0 for address “0”.
Note that the Address that will be publishes is “0”-Based. The Root topic can be changed using the Parameter
‘MqttRootTopic’ Default is ‘easymodbusclient’
By default we are using the Standard-Port 1883. This Port can be changed using the Property “MqttBrokerPort”
A Username and Passowrd can be provided using the Properties “MqttUserName” and “MqttPassword”


void Disconnect()

Close connection to Master Device


static float ConvertRegistersToFloat(int[] registers)

Converts two ModbusRegisters to Float – Example: EasyModbus.ModbusClient.ConvertRegistersToFloat(modbusClient.ReadHoldingRegisters(19,2))
registers: Two Register values (int array [0..1]) received from Modbus
returns: Connected float value


static float ConvertRegistersToFloat(int[] registers, RegisterOrder registerOrder)

Converts two ModbusRegisters to Float, Registers can by swapped
registers: Two Register values (int array [0..1]) received from Modbus
registerOrder:  Desired Word Order (Low Register first or High Register first)
returns: Connected float value


static Int32 ConvertRegistersToInt(int[] registers)

Converts two ModbusRegisters to 32 Bit Integer value
registers: Two Register values (int array [0..1]) received from Modbus
returns:  Connected 32 Bit Integer value


static Int32 ConvertRegistersToInt(int[] registers, RegisterOrder registerOrder)

Converts two ModbusRegisters to 32 Bit Integer value – Registers can be swapped
registers: Two Register values (int array [0..1]) received from Modbus
registerOrder:  Desired Word Order (Low Register first or High Register first
returns:  Connected 32 Bit Integer value


 

public static Int64 ConvertRegistersToLong(int[] registers)

Convert four 16 Bit Registers to 64 Bit Integer value Register Order “LowHigh”: Reg0: Low Word…..Reg3: High Word, “HighLow”: Reg0: High Word…..Reg3: Low Word
registers: Four Register values received from Modbus
returns: 64 bit value


 

public static Int64 ConvertRegistersToLong(int[] registers, RegisterOrder registerOrder)

Convert four 16 Bit Registers to 64 Bit Integer value – Registers can be swapped
registers: four Register values received from Modbus
registerOrder: Desired Word Order (Low Register first or High Register first
returns: Connected 64 Bit Integer value


public static double ConvertRegistersToDouble(int[] registers)

Convert four 16 Bit Registers to 64 Bit double prec. value Register Order “LowHigh”: Reg0: Low Word…..Reg3: High Word, “HighLow”: Reg0: High Word…..Reg3: Low Word
registers: four Register values received from Modbus
returns: 64 bit value


 

public static double ConvertRegistersToDouble(int[] registers, RegisterOrder registerOrder)

Convert four 16 Bit Registers to 64 Bit double prec. value – Registers can be swapped
registers: four Register values received from Modbus
registerOrder: Desired Word Order (Low Register first or High Register first
returns: Connected double prec. float value


 

public static int[] ConvertFloatToTwoRegisters(float floatValue)

Converts float to two ModbusRegisters – Example:  modbusClient.WriteMultipleRegisters(24, EasyModbus.ModbusClient.ConvertFloatToTwoRegisters((float)1.22));
floatValue: Float value which has to be converted into two registers
returns: Register values


 

public static int[] ConvertFloatToTwoRegisters(float floatValue, RegisterOrder registerOrder)

Converts float to two ModbusRegisters Registers – Registers can be swapped
floatValue: Float value which has to be converted into two registers
registerOrder: Desired Word Order (Low Register first or High Register first
returns: Register values


 

public static int[] ConvertIntToTwoRegisters(Int32 intValue)

Converts 32 Bit Value to two ModbusRegisters
intValue: Int value which has to be converted into two registers
returns: Register values


 

public static int[] ConvertDoubleToTwoRegisters(Int32 intValue, RegisterOrder registerOrder)

Converts 32 Bit Value to two ModbusRegisters Registers – Registers can be swapped
intValue: Double value which has to be converted into two registers
registerOrder: Desired Word Order (Low Register first or High Register first
returns: Register values


 

public static int[] ConvertLongToTwoRegisters(Int64 longValue)

Converts 64 Bit Value to four ModbusRegisters
longValue: long value which has to be converted into four registers
returns:
Register values


 

public static int[] ConvertLongToTwoRegisters(Int64 longValue, RegisterOrder registerOrder)

Converts 64 Bit Value to four ModbusRegisters – Registers can be swapped
longValue: long value which has to be converted into four registers
registerOrder: Desired Word Order (Low Register first or High Register first
Register values


 

public static int[] ConvertDoubleToTwoRegisters(double doubleValue)

Converts 64 Bit double prec Value to four ModbusRegisters
doubleValue: double value which has to be converted into four registers
returns: Register values


 

public static int[] ConvertDoubleToTwoRegisters(double doubleValue, RegisterOrder registerOrder)

Converts 64 Bit double prec. Value to four ModbusRegisters – Registers can be swapped
doubleValue: double value which has to be converted into four registers
registerOrder: Desired Word Order (Low Register first or High Register first
returns: Register values


public static string ConvertRegistersToString(int[] registers, int offset, int stringLength)

Converts 16 – Bit Register values to String
registers – Register array received via Modbus
offset – First Register containing the String to convert
stringLength – number of characters in String (must be even)
returns: Converted String


public static int[] ConvertStringToRegisters(string stringToConvert)

Converts a String to 16 – Bit Registers
registers – Register array received via Modbus
returns: Converted String


public bool Available(int timeout)

Checks if the Server IP-Address is available
timeout: Timeout to wait for response