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
port
SerialPortThe serial port.
mode
SerialModeThe 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
stringThe name of the serial port.
baudRate
intThe baud rate. Default is 9600.
parity
ParityThe parity. Default is None.
dataBits
intThe number of data bits. Default is 8.
stopBits
StopBitsThe number of stop bits. Default is One.
mode
SerialModeThe 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
byteThe device ID.
function
FunctionCodeThe 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
byteThe device ID.
startAddress
ushortThe starting address of the coils.
count
ushortThe 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
byteThe device ID.
startAddress
ushortThe starting address of the inputs.
count
ushortThe 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
byteThe device ID.
startAddress
ushortThe starting address of the registers.
count
ushortThe 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
byteThe device ID.
startAddress
ushortThe starting address of the registers.
count
ushortThe 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
byteThe device ID.
startAddress
ushortThe 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
byteThe device ID.
startAddress
ushortThe 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
byteThe device ID.
startAddress
ushortThe address of the coil to write.
value
boolThe 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
byteThe device ID.
startAddress
ushortThe address of the register to write.
value
shortThe value to write to the register.
Returns
- bool
True if the write operation is successful, false otherwise.