Exemplo n.º 1
0
        public async Task ReadMultibleData()
        {
            var client = new Dacs7Client(Address);

            try
            {
                await client.ConnectAsync();

                var results = (await client.ReadAsync(ReadItemSpecification.Create <byte[]>("DB1114", 0, 1000),
                                                      ReadItemSpecification.Create <byte[]>("DB1114", 2200, 100),
                                                      ReadItemSpecification.Create <byte[]>("DB1114", 1000, 1000),
                                                      ReadItemSpecification.Create <byte[]>("DB1114", 200, 100))).ToArray();


                Assert.Equal(4, results.Count());
                Assert.Equal(1000, results[0].Data.Length);
                Assert.Equal(100, results[1].Data.Length);
                Assert.Equal(1000, results[2].Data.Length);
                Assert.Equal(100, results[3].Data.Length);
            }
            finally
            {
                await client.DisconnectAsync();
            }
        }
Exemplo n.º 2
0
        public async Task ReadAsync()
        {
            var results = new List <Task <IEnumerable <DataValue> > >();

            for (int i = 0; i < Loops; i++)
            {
                results.Add(_client.ReadAsync(_item));
            }
            await Task.WhenAll(results.ToArray()).ConfigureAwait(false);
        }
Exemplo n.º 3
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();
                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);
        }
Exemplo n.º 4
0
        public async Task <List <ReadResultItem> > ReadAsync(List <ReadRequestItem> readItems)
        {
            var reads      = readItems.Select(ri => ri.ToTag());
            var readResult = await _client.ReadAsync(reads).ConfigureAwait(false);

            var enumerator = readItems.GetEnumerator();
            var result     = new List <ReadResultItem>();

            foreach (var item in readResult)
            {
                if (!enumerator.MoveNext())
                {
                    break;
                }
                result.Add(new ReadResultItem(enumerator.Current, item.ReturnCode, item.Data));
            }
            return(result);
        }
Exemplo n.º 5
0
        public async Task ReadWriteBigDBData()
        {
            var client = new Dacs7Client(Address);

            try
            {
                await client.ConnectAsync();

                var results0 = new Memory <byte>(Enumerable.Repeat((byte)0x25, 1000).ToArray());
                var results1 = await client.WriteAsync(WriteItemSpecification.Create("DB1114", 0, results0));

                var results2 = (await client.ReadAsync(ReadItemSpecification.Create <byte[]>("DB1114", 0, 1000)));
                Assert.True(results0.Span.SequenceEqual(results2.FirstOrDefault().Data.Span));
            }
            finally
            {
                await client.DisconnectAsync();
            }
        }
Exemplo n.º 6
0
        public async Task ExecuteClientAsync()
        {
            var client = new Dacs7Client(PlcTestServer.Address, PlcTestServer.ConnectionType, PlcTestServer.Timeout);

            for (var i = 0; i < 5; i++)
            {
                try
                {
                    await client.ConnectAsync();

                    var results = (await client.ReadAsync(ReadItem.Create <bool>("DB3", 0))).ToArray();

                    Assert.Single(results);
                    Assert.True(results[0].IsSuccessReturnCode);
                }
                finally
                {
                    await client.DisconnectAsync();
                }
            }
        }
Exemplo n.º 7
0
        private async Task Papper_OnRead(IEnumerable <DataPack> reads)
        {
            try
            {
                if (!reads.Any())
                {
                    return;
                }


                var readAddresses = reads.Select(w => ReadItem.Create <byte[]>(w.Selector, (ushort)w.Offset, (ushort)w.Length)).ToList();
                var results       = await _client.ReadAsync(readAddresses).ConfigureAwait(false);


                reads.AsParallel().Select((item, index) =>
                {
                    if (results != null)
                    {
                        var result = results.ElementAt(index);
                        item.ApplyData(result.Data);
                        item.ExecutionResult = result.ReturnCode == ItemResponseRetValue.Success ? ExecutionResult.Ok : ExecutionResult.Error;
                    }
                    else
                    {
                        item.ExecutionResult = ExecutionResult.Error;
                    }
                    return(true);
                }).ToList();
            }
            catch (Exception ex)
            {
                reads.AsParallel().Select((item, index) =>
                {
                    item.ExecutionResult = ExecutionResult.Error;
                    return(true);
                }).ToList();
            }
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Reads data from the plc.
 /// </summary>
 /// <param name="values">a list of tags with the following syntax Area.Offset,DataType[,length]</param>
 /// <returns>returns a enumerable with the read values</returns>
 public static Task <IEnumerable <DataValue> > ReadAsync(this Dacs7Client client, IEnumerable <string> values) => client.ReadAsync(client.CreateNodeIdCollection(values));
Exemplo n.º 11
0
 /// <summary>
 /// Reads data from the plc.
 /// </summary>
 /// <param name="values">a list of <see cref="ReadItem"/></param>
 /// <returns>returns a enumerable with the read values</returns>
 public static Task <IEnumerable <DataValue> > ReadAsync(this Dacs7Client client, params ReadItem[] values) => client.ReadAsync(values as IEnumerable <ReadItem>);
Exemplo n.º 12
0
 /// <summary>
 /// Reads data from the plc.
 /// </summary>
 /// <param name="values">a list of tags with the following syntax Area.Offset,DataType[,length]</param>
 /// <returns>returns a enumerable with the read values</returns>
 public static Task <IEnumerable <DataValue> > ReadAsync(this Dacs7Client client, params string[] values) => client.ReadAsync(values as IEnumerable <string>);
Exemplo n.º 13
0
 public async Task ReadAsync()
 {
     var results = await _client.ReadAsync(_item);
 }