using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using Modbus.Utility; namespace Modbus.Data { /// /// Collection of discrete values. /// public class DiscreteCollection : Collection, IModbusMessageDataCollection { /// /// Initializes a new instance of the class. /// public DiscreteCollection () { } /// /// Initializes a new instance of the class. /// public DiscreteCollection(params bool[] bits) : this((IList)bits) { } /// /// Initializes a new instance of the class. /// public DiscreteCollection(params byte[] bytes) : this((IList) CollectionUtility.ToBoolArray(new BitArray(bytes))) { } /// /// Initializes a new instance of the class. /// public DiscreteCollection(IList bits) : base(bits.IsReadOnly ? new List(bits) : bits) { } /// /// Gets the network bytes. /// public byte[] NetworkBytes { get { bool[] bits = new bool[Count]; CopyTo(bits, 0); BitArray bitArray = new BitArray(bits); byte[] bytes = new byte[ByteCount]; bitArray.CopyTo(bytes, 0); return bytes; } } /// /// Gets the byte count. /// public byte ByteCount { get { return (byte) ((Count + 7) / 8); } } /// /// 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(bool discrete) { return discrete ? "1" : "0"; }))), "}"); } } }