using System;
using System.Net;
using AMWD.Protocols.Modbus.Common;
using AMWD.Protocols.Modbus.Common.Events;
using AMWD.Protocols.Modbus.Common.Utils;
namespace AMWD.Protocols.Modbus.Tcp
{
///
/// Implements a Modbus TCP server proxying all requests to a virtual Modbus client.
///
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class ModbusTcpServer : ModbusTcpProxy
{
///
/// Initializes a new instance of the class.
///
/// The to listen on.
public ModbusTcpServer(IPAddress listenAddress)
: base(new VirtualModbusClient(), listenAddress)
{
TypedClient.CoilWritten += (sender, e) => CoilWritten?.Invoke(this, e);
TypedClient.RegisterWritten += (sender, e) => RegisterWritten?.Invoke(this, e);
}
#region Events
///
/// Indicates that a -value received through a remote client has been written.
///
public event EventHandler CoilWritten;
///
/// Indicates that a -value received from a remote client has been written.
///
public event EventHandler RegisterWritten;
#endregion Events
#region Properties
internal VirtualModbusClient TypedClient
=> Client as VirtualModbusClient;
#endregion Properties
#region Device Handling
///
public bool AddDevice(byte unitId)
=> TypedClient.AddDevice(unitId);
///
public bool RemoveDevice(byte unitId)
=> TypedClient.RemoveDevice(unitId);
#endregion Device Handling
#region Entity Handling
///
public Coil GetCoil(byte unitId, ushort address)
=> TypedClient.GetCoil(unitId, address);
///
public void SetCoil(byte unitId, Coil coil)
=> TypedClient.SetCoil(unitId, coil);
///
public DiscreteInput GetDiscreteInput(byte unitId, ushort address)
=> TypedClient.GetDiscreteInput(unitId, address);
///
public void SetDiscreteInput(byte unitId, DiscreteInput discreteInput)
=> TypedClient.SetDiscreteInput(unitId, discreteInput);
///
public HoldingRegister GetHoldingRegister(byte unitId, ushort address)
=> TypedClient.GetHoldingRegister(unitId, address);
///
public void SetHoldingRegister(byte unitId, HoldingRegister holdingRegister)
=> TypedClient.SetHoldingRegister(unitId, holdingRegister);
///
public InputRegister GetInputRegister(byte unitId, ushort address)
=> TypedClient.GetInputRegister(unitId, address);
///
public void SetInputRegister(byte unitId, InputRegister inputRegister)
=> TypedClient.SetInputRegister(unitId, inputRegister);
#endregion Entity Handling
}
}