.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]


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 ConvertRegistersToDouble(int[] registers)

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


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

Converts two ModbusRegisters to Double – 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 double value


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