using System; using System.Diagnostics.CodeAnalysis; using System.IO.Ports; using System.Net.Sockets; using Modbus.IO; namespace Modbus.Device { /// /// Modbus IP master device. /// [SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")] public class ModbusIpMaster : ModbusMaster { private ModbusIpMaster(ModbusTransport transport) : base(transport) { } /// /// Modbus IP master factory method. /// [SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")] public static ModbusIpMaster CreateIp(TcpClient tcpClient) { if (tcpClient == null) throw new ArgumentNullException("tcpClient"); return CreateIp(new TcpClientAdapter(tcpClient)); } /// /// Modbus IP master factory method. /// [SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")] public static ModbusIpMaster CreateIp(UdpClient udpClient) { if (udpClient == null) throw new ArgumentNullException("udpClient"); if (!udpClient.Client.Connected) throw new InvalidOperationException(Resources.UdpClientNotConnected); return CreateIp(new UdpClientAdapter(udpClient)); } /// /// Modbus IP master factory method. /// [SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")] public static ModbusIpMaster CreateIp(SerialPort serialPort) { if (serialPort == null) throw new ArgumentNullException("serialPort"); return CreateIp(new SerialPortAdapter(serialPort)); } /// /// Modbus IP master factory method. /// [SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Breaking change.")] public static ModbusIpMaster CreateIp(IStreamResource streamResource) { if (streamResource == null) throw new ArgumentNullException("streamResource"); return new ModbusIpMaster(new ModbusIpTransport(streamResource)); } /// /// Read from 1 to 2000 contiguous coils status. /// /// Address to begin reading. /// Number of coils to read. /// Coils status public bool[] ReadCoils(ushort startAddress, ushort numberOfPoints) { return base.ReadCoils(Modbus.DefaultIpSlaveUnitId, startAddress, numberOfPoints); } /// /// Read from 1 to 2000 contiguous discrete input status. /// /// Address to begin reading. /// Number of discrete inputs to read. /// Discrete inputs status public bool[] ReadInputs(ushort startAddress, ushort numberOfPoints) { return base.ReadInputs(Modbus.DefaultIpSlaveUnitId, startAddress, numberOfPoints); } /// /// Read contiguous block of holding registers. /// /// Address to begin reading. /// Number of holding registers to read. /// Holding registers status public ushort[] ReadHoldingRegisters(ushort startAddress, ushort numberOfPoints) { return base.ReadHoldingRegisters(Modbus.DefaultIpSlaveUnitId, startAddress, numberOfPoints); } /// /// Read contiguous block of input registers. /// /// Address to begin reading. /// Number of holding registers to read. /// Input registers status public ushort[] ReadInputRegisters(ushort startAddress, ushort numberOfPoints) { return base.ReadInputRegisters(Modbus.DefaultIpSlaveUnitId, startAddress, numberOfPoints); } /// /// Write a single coil value. /// /// Address to write value to. /// Value to write. public void WriteSingleCoil(ushort coilAddress, bool value) { base.WriteSingleCoil(Modbus.DefaultIpSlaveUnitId, coilAddress, value); } /// /// Write a single holding register. /// /// Value to write. /// Value to write. public void WriteSingleRegister(ushort registerAddress, ushort value) { base.WriteSingleRegister(Modbus.DefaultIpSlaveUnitId, registerAddress, value); } /// /// Write a block of 1 to 123 contiguous registers. /// /// Address to begin writing values. /// Values to write. public void WriteMultipleRegisters(ushort startAddress, ushort[] data) { base.WriteMultipleRegisters(Modbus.DefaultIpSlaveUnitId, startAddress, data); } /// /// Force each coil in a sequence of coils to a provided value. /// /// Address to begin writing values. /// Values to write. public void WriteMultipleCoils(ushort startAddress, bool[] data) { base.WriteMultipleCoils(Modbus.DefaultIpSlaveUnitId, startAddress, data); } /// /// Performs a combination of one read operation and one write operation in a single MODBUS transaction. /// The write operation is performed before the read. /// Message uses default TCP slave id of 0. /// /// Address to begin reading (Holding registers are addressed starting at 0). /// Number of registers to read. /// Address to begin writing (Holding registers are addressed starting at 0). /// Register values to write. public ushort[] ReadWriteMultipleRegisters(ushort startReadAddress, ushort numberOfPointsToRead, ushort startWriteAddress, ushort[] writeData) { return base.ReadWriteMultipleRegisters(Modbus.DefaultIpSlaveUnitId, startReadAddress, numberOfPointsToRead, startWriteAddress, writeData); } } }