private static async Task <int> Read(ReadOptions readOptions, ILoggerFactory loggerFactory) { var client = new Dacs7Client(readOptions.Address, PlcConnectionType.Pg, 5000, loggerFactory) { MaxAmQCalled = (ushort)readOptions.MaxJobs, MaxAmQCalling = (ushort)readOptions.MaxJobs }; var logger = loggerFactory?.CreateLogger("Dacs7Cli.Read"); try { await client.ConnectAsync(); if (readOptions.RegisterItems) { await client.RegisterAsync(readOptions.Tags); } var swTotal = new Stopwatch(); var tasks = new List <Task <IEnumerable <DataValue> > >(); for (var i = 0; i < readOptions.Loops; i++) { try { tasks.Add(client.ReadAsync(readOptions.Tags)); } catch (Exception ex) { logger?.LogError($"Exception in loop {ex.Message}."); } } await Task.WhenAll(tasks.ToArray()).ConfigureAwait(false); } catch (Exception ex) { logger?.LogError($"An error occurred in Read: {ex.Message} - {ex.InnerException?.Message}"); return(1); } finally { if (readOptions.RegisterItems) { await client.UnregisterAsync(readOptions.Tags); } await client.DisconnectAsync(); } return(0); }
private static async Task <int> Read(ReadOptions readOptions, ILoggerFactory loggerFactory) { var client = new Dacs7Client(readOptions.Address, PlcConnectionType.Pg, 5000, loggerFactory) { MaxAmQCalled = (ushort)readOptions.MaxJobs, MaxAmQCalling = (ushort)readOptions.MaxJobs }; var logger = loggerFactory?.CreateLogger("Dacs7Cli.Read"); try { await client.ConnectAsync(); if (readOptions.RegisterItems) { await client.RegisterAsync(readOptions.Tags); } var swTotal = new Stopwatch(); for (var i = 0; i < readOptions.Loops; i++) { if (i > 0 && readOptions.Wait > 0) { await Task.Delay(readOptions.Wait); } try { var sw = new Stopwatch(); sw.Start(); swTotal.Start(); var results = await client.ReadAsync(readOptions.Tags); swTotal.Stop(); sw.Stop(); logger?.LogDebug($"ReadTime: {sw.Elapsed}"); var resultEnumerator = results.GetEnumerator(); foreach (var item in readOptions.Tags) { if (resultEnumerator.MoveNext()) { var current = resultEnumerator.Current; logger?.LogInformation($"Read: {item}={current.Data} - {GetValue(current.Value)}"); } } } catch (Exception ex) { logger?.LogError($"Exception in loop {ex.Message}."); } } if (readOptions.Loops > 0) { logger?.LogInformation($"Average read time over loops is {(ElapsedNanoSeconds(swTotal.ElapsedTicks) / readOptions.Loops)}ns"); await Task.Delay(readOptions.Wait); } } catch (Exception ex) { logger?.LogError($"An error occured in Read: {ex.Message} - {ex.InnerException?.Message}"); return(1); } finally { if (readOptions.RegisterItems) { await client.UnregisterAsync(readOptions.Tags); } await client.DisconnectAsync(); } return(0); }
private static async Task <int> Read(ReadOptions readOptions) { var client = new Dacs7Client(readOptions.Address); try { long msTotal = 0; await client.ConnectAsync(); if (readOptions.RegisterItems) { await client.RegisterAsync(readOptions.Tags); } for (int i = 0; i < readOptions.Loops; i++) { if (i > 0 && readOptions.Wait > 0) { await Task.Delay(readOptions.Wait); } try { var sw = new Stopwatch(); sw.Start(); var results = await client.ReadAsync(readOptions.Tags); sw.Stop(); msTotal += sw.ElapsedMilliseconds; _logger?.LogDebug($"ReadTime: {sw.Elapsed}"); var resultEnumerator = results.GetEnumerator(); foreach (var item in readOptions.Tags) { if (resultEnumerator.MoveNext()) { _logger?.LogInformation($"Read: {item}={resultEnumerator.Current}"); } } } catch (Exception ex) { _logger?.LogError($"Exception in loop {ex.Message}."); } } if (readOptions.Loops > 0) { _logger?.LogInformation($"Average read time over loops is {msTotal / readOptions.Loops}ms"); await Task.Delay(readOptions.Wait); } } catch (Exception ex) { _logger?.LogError($"An error occured in Read: {ex.Message}"); return(1); } finally { if (readOptions.RegisterItems) { await client.UnregisterAsync(readOptions.Tags); } await client.DisconnectAsync(); } return(0); }