using System;
using AMWD.Protocols.Modbus.Common.Contracts;
using AMWD.Protocols.Modbus.Common.Protocols;
namespace AMWD.Protocols.Modbus.Tcp
{
///
/// Default implementation of a Modbus TCP client.
///
public class ModbusTcpClient : ModbusClientBase
{
///
/// Initializes a new instance of the class with a hostname and port number.
///
/// The DNS name of the remote host to which the connection is intended to.
/// The port number of the remote host to which the connection is intended to.
public ModbusTcpClient(string hostname, int port = 502)
: this(new ModbusTcpConnection { Hostname = hostname, Port = port })
{ }
///
/// Initializes a new instance of the class with a specific .
///
/// The responsible for invoking the requests.
public ModbusTcpClient(IModbusConnection connection)
: this(connection, true)
{ }
///
/// Initializes a new instance of the class with a specific .
///
/// The responsible for invoking the requests.
///
/// if the connection should be disposed of by Dispose(),
/// otherwise if you inted to reuse the connection.
///
public ModbusTcpClient(IModbusConnection connection, bool disposeConnection)
: base(connection, disposeConnection)
{
Protocol = new TcpProtocol();
}
///
public override IModbusProtocol Protocol { get; set; }
///
public string Hostname
{
get
{
if (connection is ModbusTcpConnection tcpConnection)
return tcpConnection.Hostname;
return default;
}
set
{
if (connection is ModbusTcpConnection tcpConnection)
tcpConnection.Hostname = value;
}
}
///
public int Port
{
get
{
if (connection is ModbusTcpConnection tcpConnection)
return tcpConnection.Port;
return default;
}
set
{
if (connection is ModbusTcpConnection tcpConnection)
tcpConnection.Port = value;
}
}
///
public TimeSpan ReadTimeout
{
get
{
if (connection is ModbusTcpConnection tcpConnection)
return tcpConnection.ReadTimeout;
return default;
}
set
{
if (connection is ModbusTcpConnection tcpConnection)
tcpConnection.ReadTimeout = value;
}
}
///
public TimeSpan WriteTimeout
{
get
{
if (connection is ModbusTcpConnection tcpConnection)
return tcpConnection.WriteTimeout;
return default;
}
set
{
if (connection is ModbusTcpConnection tcpConnection)
tcpConnection.WriteTimeout = value;
}
}
///
public TimeSpan ReconnectTimeout
{
get
{
if (connection is ModbusTcpConnection tcpConnection)
return tcpConnection.ConnectTimeout;
return default;
}
set
{
if (connection is ModbusTcpConnection tcpConnection)
tcpConnection.ConnectTimeout = value;
}
}
///
public TimeSpan IdleTimeout
{
get
{
if (connection is ModbusTcpConnection tcpConnection)
return tcpConnection.IdleTimeout;
return default;
}
set
{
if (connection is ModbusTcpConnection tcpConnection)
tcpConnection.IdleTimeout = value;
}
}
}
}