Table of Contents

Class ModbusClient

Namespace
Iot.Device.Modbus.Client
Assembly
Iot.Device.Modbus.dll

Represents a Modbus client that communicates with a Modbus device over a serial port.

public class ModbusClient : Port
Inheritance
ModbusClient
Inherited Members

Constructors

ModbusClient(SerialPort, SerialMode)

Initializes a new instance of the ModbusClient class with the specified serial port.

public ModbusClient(SerialPort port, SerialMode mode = SerialMode.RS485)

Parameters

port SerialPort

The serial port.

mode SerialMode

The mode of serial port, default is RS485.

ModbusClient(string, int, Parity, int, StopBits, SerialMode)

Initializes a new instance of the ModbusClient class with the specified port settings.

public ModbusClient(string portName, int baudRate = 9600, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One, SerialMode mode = SerialMode.RS485)

Parameters

portName string

The name of the serial port.

baudRate int

The baud rate. Default is 9600.

parity Parity

The parity. Default is None.

dataBits int

The number of data bits. Default is 8.

stopBits StopBits

The number of stop bits. Default is One.

mode SerialMode

The mode of serial port, default is RS485.

Methods

Raw(byte, FunctionCode, byte[])

Sends a raw Modbus request to the device and returns the raw response.

public byte[] Raw(byte deviceId, FunctionCode function, byte[] data)

Parameters

deviceId byte

The device ID.

function FunctionCode

The Modbus function code.

data byte[]

The raw data of the request.

Returns

byte[]

The raw data of the response.

ReadCoils(byte, ushort, ushort)

Reads the values of coils from the Modbus device.

public bool[] ReadCoils(byte deviceId, ushort startAddress, ushort count)

Parameters

deviceId byte

The device ID.

startAddress ushort

The starting address of the coils.

count ushort

The number of coils to read.

Returns

bool[]

An array of boolean values representing the state of the coils.

ReadDiscreteInputs(byte, ushort, ushort)

Reads the values of discrete inputs from the Modbus device.

public bool[] ReadDiscreteInputs(byte deviceId, ushort startAddress, ushort count)

Parameters

deviceId byte

The device ID.

startAddress ushort

The starting address of the inputs.

count ushort

The number of inputs to read.

Returns

bool[]

An array of boolean values representing the state of the discrete inputs.

ReadHoldingRegisters(byte, ushort, ushort)

Reads the values of holding registers from the Modbus device.

public short[] ReadHoldingRegisters(byte deviceId, ushort startAddress, ushort count)

Parameters

deviceId byte

The device ID.

startAddress ushort

The starting address of the registers.

count ushort

The number of registers to read.

Returns

short[]

An array of ushort values representing the values of the holding registers.

ReadInputRegisters(byte, ushort, ushort)

Reads the values of input registers from the Modbus device.

public short[] ReadInputRegisters(byte deviceId, ushort startAddress, ushort count)

Parameters

deviceId byte

The device ID.

startAddress ushort

The starting address of the registers.

count ushort

The number of registers to read.

Returns

short[]

An array of ushort values representing the values of the input registers.

WriteMultipleCoils(byte, ushort, bool[])

Writes multiple coil values to the Modbus device.

public bool WriteMultipleCoils(byte deviceId, ushort startAddress, bool[] values)

Parameters

deviceId byte

The device ID.

startAddress ushort

The starting address of the coils to write.

values bool[]

An array of boolean values representing the state of the coils to write.

Returns

bool

True if the write operation is successful, false otherwise.

WriteMultipleRegisters(byte, ushort, ushort[])

Writes multiple register values to the Modbus device.

public bool WriteMultipleRegisters(byte deviceId, ushort startAddress, ushort[] values)

Parameters

deviceId byte

The device ID.

startAddress ushort

The starting address of the registers to write.

values ushort[]

An array of ushort values representing the values to write to the registers.

Returns

bool

True if the write operation is successful, false otherwise.

WriteSingleCoil(byte, ushort, bool)

Writes a single coil value to the Modbus device.

public bool WriteSingleCoil(byte deviceId, ushort startAddress, bool value)

Parameters

deviceId byte

The device ID.

startAddress ushort

The address of the coil to write.

value bool

The value to write to the coil.

Returns

bool

True if the write operation is successful, false otherwise.

WriteSingleRegister(byte, ushort, short)

Writes a single register value to the Modbus device.

public bool WriteSingleRegister(byte deviceId, ushort startAddress, short value)

Parameters

deviceId byte

The device ID.

startAddress ushort

The address of the register to write.

value short

The value to write to the register.

Returns

bool

True if the write operation is successful, false otherwise.