Class ModbusClient
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
portSerialPortThe serial port.
modeSerialModeThe 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
portNamestringThe name of the serial port.
baudRateintThe baud rate. Default is 9600.
parityParityThe parity. Default is None.
dataBitsintThe number of data bits. Default is 8.
stopBitsStopBitsThe number of stop bits. Default is One.
modeSerialModeThe 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
deviceIdbyteThe device ID.
functionFunctionCodeThe Modbus function code.
databyte[]The raw data of the request.
Returns
ReadCoils(byte, ushort, ushort)
Reads the values of coils from the Modbus device.
public bool[] ReadCoils(byte deviceId, ushort startAddress, ushort count)
Parameters
deviceIdbyteThe device ID.
startAddressushortThe starting address of the coils.
countushortThe number of coils to read.
Returns
- bool[]
An array of boolean values representing the state of the coils or a null if the read operation fails.
Exceptions
- ArgumentOutOfRangeException
Thrown when
deviceId,startAddress, orcountis out of range.
ReadDiscreteInputs(byte, ushort, ushort)
Reads the values of discrete inputs from the Modbus device.
public bool[] ReadDiscreteInputs(byte deviceId, ushort startAddress, ushort count)
Parameters
deviceIdbyteThe device ID.
startAddressushortThe starting address of the inputs.
countushortThe number of inputs to read.
Returns
- bool[]
An array of boolean values representing the state of the discrete inputs or a null if the read operation fails.
Exceptions
- ArgumentOutOfRangeException
Thrown when
deviceId,startAddress, orcountis out of range.
ReadHoldingRegisters(byte, ushort, ushort)
Reads the values of holding registers from the Modbus device.
public short[] ReadHoldingRegisters(byte deviceId, ushort startAddress, ushort count)
Parameters
deviceIdbyteThe device ID.
startAddressushortThe starting address of the registers.
countushortThe number of registers to read.
Returns
- short[]
An array of short values representing the values of the holding registers or a null if the read operation fails.
Exceptions
- ArgumentOutOfRangeException
Thrown when
deviceId,startAddress, orcountis out of range.
ReadInputRegisters(byte, ushort, ushort)
Reads the values of input registers from the Modbus device.
public short[] ReadInputRegisters(byte deviceId, ushort startAddress, ushort count)
Parameters
deviceIdbyteThe device ID.
startAddressushortThe starting address of the registers.
countushortThe number of registers to read.
Returns
- short[]
An array of short values representing the values of the holding registers or a null if the read operation fails.
Exceptions
- ArgumentOutOfRangeException
Thrown when
deviceId,startAddress, orcountis out of range.
WriteMultipleCoils(byte, ushort, bool[])
Writes multiple coil values to the Modbus device.
public bool WriteMultipleCoils(byte deviceId, ushort startAddress, bool[] values)
Parameters
deviceIdbyteThe device ID.
startAddressushortThe starting address of the coils to write.
valuesbool[]An array of boolean values representing the state of the coils to write.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
deviceIdorstartAddressis out of range, or the length of thevaluesarray is zero.
WriteMultipleRegisters(byte, ushort, ushort[])
Writes multiple register values to the Modbus device.
public bool WriteMultipleRegisters(byte deviceId, ushort startAddress, ushort[] values)
Parameters
deviceIdbyteThe device ID.
startAddressushortThe starting address of the registers to write.
valuesushort[]An array of ushort values representing the values to write to the registers.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
deviceIdorstartAddressis out of range, or the length of thevaluesarray is zero.
WriteSingleCoil(byte, ushort, bool)
Writes a single coil value to the Modbus device.
public bool WriteSingleCoil(byte deviceId, ushort startAddress, bool value)
Parameters
deviceIdbyteThe device ID.
startAddressushortThe address of the coil to write.
valueboolThe value to write to the coil.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
deviceIdorstartAddressis out of range.
WriteSingleRegister(byte, ushort, short)
Writes a single register value to the Modbus device.
public bool WriteSingleRegister(byte deviceId, ushort startAddress, short value)
Parameters
deviceIdbyteThe device ID.
startAddressushortThe address of the register to write.
valueshortThe value to write to the register.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
deviceIdorstartAddressis out of range.