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
IsConnected
Gets the connection status of the underlying TCP client.
public bool IsConnected { get; }
Property Value
ReadTimeout
Gets or sets the read timeout in milliseconds. Default is Infinite.
public int ReadTimeout { get; set; }
Property Value
WriteTimeout
Gets or sets the write timeout in milliseconds. Default is Infinite.
public int WriteTimeout { get; set; }
Property Value
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
ModbusEndiannessSpecifies 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
IPAddressThe 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
IPAddressThe IP address of the end unit. Example: IPAddress.Parse("192.168.0.1").
endianness
ModbusEndiannessSpecifies 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
IPEndPointThe 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
IPEndPointThe IP address and port of the end unit.
endianness
ModbusEndiannessSpecifies the endianness of the data exchanged with the Modbus server.
Connect(string)
Connect to the specified remoteEndpoint
.
public void Connect(string remoteEndpoint)
Parameters
remoteEndpoint
stringThe 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
stringThe 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
ModbusEndiannessSpecifies 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
TcpClientThe externally managed TcpClient.
endianness
ModbusEndiannessSpecifies 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
byteThe unit identifier.
functionCode
ModbusFunctionCodeThe function code.
extendFrame
Action<ExtendedBinaryWriter>An action to be called to extend the prepared Modbus frame with function code specific data.
Returns
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
byteThe unit identifier.
functionCode
ModbusFunctionCodeThe function code.
extendFrame
Action<ExtendedBinaryWriter>An action to be called to extend the prepared Modbus frame with function code specific data.
cancellationToken
CancellationTokenThe token to monitor for cancellation requests. The default value is None.