Table of Contents

Class ModbusTcpClient

Namespace
FluentModbus
Assembly
FluentModbus.dll

A Modbus TCP client.

public class ModbusTcpClient : ModbusClient, IDisposable
Inheritance
ModbusTcpClient
Implements
Inherited Members

Constructors

ModbusTcpClient()

Creates a new Modbus TCP client for communication with Modbus TCP servers or bridges, routers and gateways for communication with serial line end units.

public ModbusTcpClient()

Properties

ConnectTimeout

Gets or sets the connect timeout in milliseconds. Default is 1000 ms.

public int ConnectTimeout { get; set; }

Property Value

int

IsConnected

Gets the connection status of the underlying TCP client.

public bool IsConnected { get; }

Property Value

bool

ReadTimeout

Gets or sets the read timeout in milliseconds. Default is Infinite.

public int ReadTimeout { get; set; }

Property Value

int

WriteTimeout

Gets or sets the write timeout in milliseconds. Default is Infinite.

public int WriteTimeout { get; set; }

Property Value

int

Methods

Connect()

Connect to localhost at port 502 with LittleEndian as default byte layout.

public void Connect()

Connect(ModbusEndianness)

Connect to localhost at port 502.

public void Connect(ModbusEndianness endianness)

Parameters

endianness ModbusEndianness

Specifies the endianness of the data exchanged with the Modbus server.

Connect(IPAddress)

Connect to the specified remoteIpAddress at port 502.

public void Connect(IPAddress remoteIpAddress)

Parameters

remoteIpAddress IPAddress

The IP address of the end unit with LittleEndian as default byte layout. Example: IPAddress.Parse("192.168.0.1").

Connect(IPAddress, ModbusEndianness)

Connect to the specified remoteIpAddress at port 502.

public void Connect(IPAddress remoteIpAddress, ModbusEndianness endianness)

Parameters

remoteIpAddress IPAddress

The IP address of the end unit. Example: IPAddress.Parse("192.168.0.1").

endianness ModbusEndianness

Specifies the endianness of the data exchanged with the Modbus server.

Connect(IPEndPoint)

Connect to the specified remoteEndpoint with LittleEndian as default byte layout.

public void Connect(IPEndPoint remoteEndpoint)

Parameters

remoteEndpoint IPEndPoint

The IP address and port of the end unit.

Connect(IPEndPoint, ModbusEndianness)

Connect to the specified remoteEndpoint.

public void Connect(IPEndPoint remoteEndpoint, ModbusEndianness endianness)

Parameters

remoteEndpoint IPEndPoint

The IP address and port of the end unit.

endianness ModbusEndianness

Specifies the endianness of the data exchanged with the Modbus server.

Connect(string)

Connect to the specified remoteEndpoint.

public void Connect(string remoteEndpoint)

Parameters

remoteEndpoint string

The IP address and optional port of the end unit with LittleEndian as default byte layout. Examples: "192.168.0.1", "192.168.0.1:502", "::1", "[::1]:502". The default port is 502.

Connect(string, ModbusEndianness)

Connect to the specified remoteEndpoint.

public void Connect(string remoteEndpoint, ModbusEndianness endianness)

Parameters

remoteEndpoint string

The IP address and optional port of the end unit. Examples: "192.168.0.1", "192.168.0.1:502", "::1", "[::1]:502". The default port is 502.

endianness ModbusEndianness

Specifies the endianness of the data exchanged with the Modbus server.

Disconnect()

Disconnect from the end unit.

public void Disconnect()

Dispose()

Diposes the current instance.

public void Dispose()

Dispose(bool)

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

Initialize(TcpClient, ModbusEndianness)

Initialize the Modbus TCP client with an externally managed TcpClient.

public void Initialize(TcpClient tcpClient, ModbusEndianness endianness)

Parameters

tcpClient TcpClient

The externally managed TcpClient.

endianness ModbusEndianness

Specifies the endianness of the data exchanged with the Modbus server.

TransceiveFrame(byte, ModbusFunctionCode, Action<ExtendedBinaryWriter>)

Sends the requested modbus message and waits for the response.

protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctionCode functionCode, Action<ExtendedBinaryWriter> extendFrame)

Parameters

unitIdentifier byte

The unit identifier.

functionCode ModbusFunctionCode

The function code.

extendFrame Action<ExtendedBinaryWriter>

An action to be called to extend the prepared Modbus frame with function code specific data.

Returns

Span<byte>

TransceiveFrameAsync(byte, ModbusFunctionCode, Action<ExtendedBinaryWriter>, CancellationToken)

Sends the requested modbus message and waits for the response.

protected override Task<Memory<byte>> TransceiveFrameAsync(byte unitIdentifier, ModbusFunctionCode functionCode, Action<ExtendedBinaryWriter> extendFrame, CancellationToken cancellationToken = default)

Parameters

unitIdentifier byte

The unit identifier.

functionCode ModbusFunctionCode

The function code.

extendFrame Action<ExtendedBinaryWriter>

An action to be called to extend the prepared Modbus frame with function code specific data.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

Task<Memory<byte>>