/// <summary> /// Validates the write result of the <paramref name="plcItems"/> by reading and comparing their <see cref="IPlcItem.Value"/> with the target value. /// </summary> /// <param name="plc"> The extended <see cref="IPlcItem"/> instance. </param> /// <param name="plcItems"> The <see cref="IPlcItem"/>s whose write result should be validated. </param> /// <param name="cancellationToken"> An optional <see cref="CancellationToken"/> for cancelling the write operation. </param> /// <returns> An awaitable <see cref="Task"/> yielding <c>True</c> on success, otherwise <c>False</c>. </returns> /// <exception cref="ReadOrWritePlcException"> Thrown if an exception occurred while writing or while validating. </exception> private static async Task <bool> ValidateWriteResultAsync(this IPlc plc, ICollection <IPlcItem> plcItems, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); // Create clones of the items, for later comparison of the target against the actual value. var clonedItems = plcItems .Select ( plcItem => { var clonedItem = plcItem.Clone(); clonedItem.Value.SetAllBitsTo(false); return(clonedItem); } ) .ToArray() ; await plc.ReadItemsAsync(clonedItems, cancellationToken); var targetValues = plcItems.Select(plcItem => plcItem.Value); var actualValues = clonedItems.Select(clonedItem => clonedItem.Value); return(targetValues.SequenceEqual(actualValues)); }
/// <summary> /// Reads the value of the <paramref name="plcItem"/> from the plc. /// </summary> /// <param name="plc"> The extended <see cref="IPlcItem"/> instance. </param> /// <param name="plcItem"> The <see cref="IPlcItem"/> to read. </param> /// <param name="cancellationToken"> An optional <see cref="CancellationToken"/> for cancelling the read operation. </param> /// <returns> An awaitable task containing the result as <see cref="Byte"/> array. </returns> /// <exception cref="ReadPlcException"> Thrown if an exception occurred while reading. </exception> public static async Task <BitCollection> ReadItemAsync(this IPlc plc, IPlcItem plcItem, CancellationToken cancellationToken = default) { await plc.ReadItemsAsync(new[] { plcItem }, cancellationToken); return(plcItem.Value); }