Пример #1
0
        /// <summary>
        /// Gets the digital input values for the specified <see cref="DIBit"/>s. A read from the controller will only be performed if no previous
        /// read was performed within the specified timeout.
        /// </summary>
        /// <param name="bits">The DI bits.</param>
        /// <param name="minFresh">The minimum amount of time before data is considered stale and a re-read is required.</param>
        /// <returns>
        /// The values.
        /// </returns>
        public static IEnumerable <int> GetValues(this IEnumerable <DIBit> bits, TimeSpan minFresh)
        {
            var devGroups = bits.GroupBy(b => b.Device);

            foreach (var devGroup in devGroups)
            {
                IIODevice    dev         = (IIODevice)devGroup.Key;
                List <DIBit> orderedBits = devGroup.OrderBy(b => b.Channel).ToList();

                bool allFresh = true;
                foreach (DIBit bit in orderedBits)
                {
                    if (DateTime.Now - bit.LastRead > minFresh)
                    {
                        allFresh = false;
                        break;
                    }
                }

                if (!allFresh)
                {
                    int[] values = dev.GetDIBits(orderedBits.Select(b => b.Channel).ToArray());

                    DateTime readTime = DateTime.Now;

                    for (int i = 0; i < values.Length; i++)
                    {
                        orderedBits[i].Update(readTime, values[i]);
                    }
                }
            }

            return(bits.Select(b => b.GetLast()));
        }
Пример #2
0
        /// <summary>
        /// Gets the digital input values for the specified DI bits.
        /// </summary>
        /// <param name="bits">The DI bits.</param>
        /// <returns>The values.</returns>
        public static IEnumerable <int> GetValues(this IEnumerable <DIBit> bits)
        {
            var devGroups = bits.GroupBy(b => b.Device);

            foreach (var devGroup in devGroups)
            {
                IIODevice    dev         = (IIODevice)devGroup.Key;
                List <DIBit> orderedBits = devGroup.OrderBy(b => b.Channel).ToList();

                int[] values = dev.GetDIBits(orderedBits.Select(b => b.Channel).ToArray());

                DateTime readTime = DateTime.Now;

                for (int i = 0; i < values.Length; i++)
                {
                    orderedBits[i].Update(readTime, values[i]);
                }
            }

            return(bits.Select(b => b.GetLast()));
        }