using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Net; using Modbus.Utility; namespace Modbus.Data { /// /// Collection of 16 bit registers. /// public class RegisterCollection : Collection, IModbusMessageDataCollection { /// /// Initializes a new instance of the class. /// public RegisterCollection() { } /// /// Initializes a new instance of the class. /// public RegisterCollection(byte[] bytes) : this((IList) ModbusUtility.NetworkBytesToHostUInt16(bytes)) { } /// /// Initializes a new instance of the class. /// public RegisterCollection(params ushort[] registers) : this((IList) registers) { } /// /// Initializes a new instance of the class. /// public RegisterCollection(IList registers) : base(registers.IsReadOnly ? new List(registers) : registers) { } /// /// Gets the network bytes. /// public byte[] NetworkBytes { get { List bytes = new List(); foreach (ushort register in this) bytes.AddRange(BitConverter.GetBytes((ushort) IPAddress.HostToNetworkOrder((short) register))); return bytes.ToArray(); } } /// /// Gets the byte count. /// public byte ByteCount { get { return (byte) (Count * 2); } } /// /// Returns a that represents the current . /// /// /// A that represents the current . /// public override string ToString() { return String.Concat("{", String.Join(", ", CollectionUtility.ToArray(SequenceUtility.ToList(this, delegate(ushort v) { return v.ToString(); }))), "}"); } } }