public async Task MonitorDifferentIntervals() { var monitoredChangesOfFirstItem = 0; var monitoredChangesOfSecondItem = 0; var firstMonitorItemIdentifier = "Monitored item #01"; uint firstMonitorItemInterval = 50; var secondMonitorItemIdentifier = "Monitored item #02"; uint secondMonitorItemInterval = 200; var changes = 100; var secondChanges = changes / ((secondMonitorItemInterval / firstMonitorItemInterval) / 2); var monitoredPlc = new MockPlc().MakeMonitorable ( new Dictionary <string, uint> { { firstMonitorItemIdentifier, firstMonitorItemInterval } , { secondMonitorItemIdentifier, secondMonitorItemInterval } } ); // Create items that must be monitored. var firstMonitoredItem = new BytePlcItem(dataBlock: Data.Datablock, position: Data.StartOfModifiableBytes, identifier: firstMonitorItemIdentifier); var secondMonitoredItem = firstMonitoredItem.Clone(secondMonitorItemIdentifier); // Create the item that is used to manipulate the value. IPlcItem writeItem = firstMonitoredItem.Clone("ChangeItem"); // Set a callback for changes to the items. firstMonitoredItem.ValueChanged += (sender, args) => { monitoredChangesOfFirstItem++; }; secondMonitoredItem.ValueChanged += (sender, args) => { monitoredChangesOfSecondItem++; }; try { // Connect to the plc. monitoredPlc.Connect(); // Start monitoring those items. monitoredPlc.MonitorItem(firstMonitoredItem); monitoredPlc.MonitorItem(secondMonitoredItem); monitoredPlc.Start(); // Manipulate the monitored value. for (byte i = 1; i <= changes; i++) { writeItem.Value.TransferValuesFrom(new[] { i }); await monitoredPlc.WriteItemAsync(writeItem); await Task.Delay((int)firstMonitorItemInterval * 2); // This must be at least the double amount of the polling interval. } // Stop monitoring. monitoredPlc.Stop(); // Check if all changes where registered. Assert.AreEqual(changes, monitoredChangesOfFirstItem); Assert.True(monitoredChangesOfSecondItem >= secondChanges); } finally { monitoredPlc.Dispose(); } }
public async Task MonitorChanges() { var changes = 100; var target = (changes * (changes + 1)) / 2; // Gaußsche Summenformel var monitoredChanges = 0; var collectedValue = 0; var monitorItemIdentifier = "MonitoredItem"; uint monitorItemInterval = 50; var monitoredPlc = new MockPlc().MakeMonitorable ( new Dictionary <string, uint> { { monitorItemIdentifier, monitorItemInterval } } ); // Create the item that must be monitored. var monitorItem = new BytePlcItem(dataBlock: Data.Datablock, position: Data.StartOfModifiableBytes, identifier: monitorItemIdentifier); // Create the item that is used to manipulate the value. IPlcItem writeItem = monitorItem.Clone("ChangeItem"); // Set a callback for changes to the items. monitorItem.ValueChanged += (sender, args) => { monitoredChanges++; collectedValue += args.NewValue; }; try { // Connect to the plc. monitoredPlc.Connect(); // Start monitoring those items. monitoredPlc.MonitorItem(monitorItem); monitoredPlc.Start(); // Manipulate the monitored value. for (byte i = 1; i <= changes; i++) { writeItem.Value.TransferValuesFrom(new[] { i }); await monitoredPlc.WriteItemAsync(writeItem); await Task.Delay((int)monitorItemInterval * 2); // This must be at least the double amount of the polling interval. } // Stop monitoring. monitoredPlc.Stop(); // Further manipulate the value to check if this is not monitored. writeItem.Value.TransferValuesFrom(new[] { byte.MinValue }); writeItem.Value.TransferValuesFrom(new[] { byte.MaxValue }); // Check if all changes where registered. Assert.AreEqual(changes, monitoredChanges); Assert.AreEqual(target, collectedValue); Assert.AreEqual(changes, monitorItem.Value); } finally { monitoredPlc.Dispose(); } }