public async Task ReadWriteSingleWords()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                const string datablock = "DB1";
                var writeResults       = (await client.WriteAsync(WriteItem.Create(datablock, 10002, (ushort)0),
                                                                  WriteItem.Create(datablock, 10004, (short)0))).ToArray();


                var results = (await client.ReadAsync(ReadItem.Create <ushort>(datablock, 10002),
                                                      ReadItem.Create <short>(datablock, 10004))).ToArray();


                Assert.Equal(2, results.Count());
                Assert.Equal(typeof(ushort), results[0].Type);
                Assert.Equal((ushort)0, (ushort)results[0].Value);
                Assert.Equal(typeof(short), results[1].Type);
                Assert.Equal((short)0, (short)results[1].Value);

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, 10002, (ushort)15),
                                                        WriteItem.Create(datablock, 10004, (short)25))).ToArray();

                results = (await client.ReadAsync(ReadItem.Create <ushort>(datablock, 10002),
                                                  ReadItem.Create <short>(datablock, 10004))).ToArray();

                Assert.Equal(2, results.Count());
                Assert.Equal(typeof(ushort), results[0].Type);
                Assert.Equal((ushort)15, (ushort)results[0].Value);
                Assert.Equal(typeof(short), results[1].Type);
                Assert.Equal((short)25, (short)results[1].Value);

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, 10002, (ushort)0),
                                                        WriteItem.Create(datablock, 10004, (short)0))).ToArray();
            });
        }
        public async Task ReadWriteString()
        {
            const string datablock = "DB962";
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                var data0 = new List <WriteItem> {
                    WriteItem.Create <string>(datablock, 58, 14, "XXXXXXXXXXXXXX")
                };

                var writeResults1 = await client.WriteAsync(data0);
                Assert.Equal(data0.Count, writeResults1.Count());
                Assert.True(writeResults1.All(x => x == ItemResponseRetValue.Success));


                var data1 = new List <ReadItem> {
                    ReadItem.Create <string>(datablock, 58, 14)
                };

                var readResults1 = await client.ReadAsync(data1);

                var data = new List <WriteItem> {
                    WriteItem.Create <string>(datablock, 58, 14, "1234567890ABCD")
                };

                var writeResults2 = await client.WriteAsync(data);
                Assert.Equal(data.Count, writeResults2.Count());
                Assert.True(writeResults2.All(x => x == ItemResponseRetValue.Success));


                var readResults2 = await client.ReadAsync(data1);


                Assert.False(readResults1.FirstOrDefault().Data.Span.SequenceEqual(readResults2.FirstOrDefault().Data.Span));
            }, 240);
        }
        public async Task ReadWriteMultibleDInts()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                const string datablock = "DB1";
                var writeDataDefault   = new int[] { 0, 0 };
                const int startAddress = 10038;
                var writeResults       = (await client.WriteAsync(WriteItem.Create(datablock, startAddress, writeDataDefault))).ToArray();


                var results = (await client.ReadAsync(ReadItem.Create <int>(datablock, startAddress, 2))).ToArray();


                Assert.Single(results);
                Assert.Equal(typeof(int[]), results[0].Type);

                var resultValueDefault = results[0].GetValue <int[]>();

                Assert.True(resultValueDefault.SequenceEqual(writeDataDefault));

                var writeData = new int[] { 22, 21 };
                writeResults  = (await client.WriteAsync(WriteItem.Create(datablock, startAddress, writeData))).ToArray();

                results = (await client.ReadAsync(ReadItem.Create <int>(datablock, startAddress, 2))).ToArray();

                Assert.Single(results);
                Assert.Equal(typeof(int[]), results[0].Type);

                var resultValue = results[0].GetValue <int[]>();

                Assert.True(resultValue.SequenceEqual(writeData));

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, startAddress, writeDataDefault))).ToArray();
            });
        }
        public async Task ReadWriteSingleStrings()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                const string datablock = "DB1";
                var writeResults       = (await client.WriteAsync(WriteItem.Create(datablock, 10046, ""),
                                                                  WriteItem.Create(datablock, 10068, "                    "))).ToArray();


                var results = (await client.ReadAsync(ReadItem.Create <string>(datablock, 10046, 20),
                                                      ReadItem.Create <string>(datablock, 10068, 20))).ToArray();


                Assert.Equal(2, results.Count());
                Assert.Equal(typeof(string), results[0].Type);
                Assert.Equal("", (string)results[0].Value);
                Assert.Equal(typeof(string), results[1].Type);
                Assert.Equal("                    ", (string)results[1].Value);

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, 10046, "Test1"),
                                                        WriteItem.Create(datablock, 10068, "Test2               "))).ToArray();

                results = (await client.ReadAsync(ReadItem.Create <string>(datablock, 10046, 20),
                                                  ReadItem.Create <string>(datablock, 10068, 20))).ToArray();

                Assert.Equal(2, results.Count());
                Assert.Equal(typeof(string), results[0].Type);
                Assert.Equal("Test1", (string)results[0].Value);
                Assert.Equal(typeof(string), results[1].Type);
                Assert.Equal("Test2               ", (string)results[1].Value);

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, 10046, ""),
                                                        WriteItem.Create(datablock, 10068, "                    "))).ToArray();
            });
        }
        public async Task ReadWriteMultibleCharsStringLongerThanPDUSize()
        {
            const string datablock = "DB141";
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                var data = new List <ReadItem> {
                    ReadItem.Create <char[]>(datablock, 28, 14),
                    ReadItem.Create <char[]>(datablock, 46, 10),
                    ReadItem.Create <string>(datablock, 106, 10),
                    ReadItem.Create <char[]>(datablock, 124, 10),
                    ReadItem.Create <char[]>(datablock, 134, 10),
                    ReadItem.Create <char[]>(datablock, 60, 8),
                    ReadItem.Create <char[]>(datablock, 86, 8),
                    ReadItem.Create <char[]>(datablock, 94, 8),
                    ReadItem.Create <char[]>(datablock, 116, 8),
                    ReadItem.Create <char[]>(datablock, 0, 8),
                    ReadItem.Create <char[]>(datablock, 8, 8),
                    ReadItem.Create <char[]>(datablock, 16, 8),
                    ReadItem.Create <char[]>(datablock, 76, 6),
                    ReadItem.Create <char[]>(datablock, 24, 4),
                    ReadItem.Create <char[]>(datablock, 42, 4),
                    ReadItem.Create <char[]>(datablock, 56, 4)
                };

                var results = (await client.ReadAsync(data)).ToArray();

                Assert.Equal(data.Count, results.Length);
                Assert.True(results.All(x => x.IsSuccessReturnCode));

                var writeResults = await client.WriteAsync(data.Select((r, i) => r.From(results[i].Data)).ToArray());

                Assert.Equal(data.Count, writeResults.Count());
                Assert.True(writeResults.All(x => x == ItemResponseRetValue.Success));
            }, 240);
        }
Beispiel #6
0
        private DataValue CreateTestValue <T>(T value)
        {
            ushort countItems = 1;

            if (value is IList l)
            {
                countItems = (ushort)l.Count;
            }
            else if (value is string s)
            {
                countItems = (ushort)s.Length;
            }


            var ri       = ReadItem.Create <T>("DB1", 0, countItems);
            var itemData = ri.ConvertDataToMemory(value);

            Memory <byte> buffer = new byte[4 + itemData.Length];

            buffer.Span[0] = 255;
            buffer.Span[1] = 3;
            BinaryPrimitives.WriteUInt16BigEndian(buffer.Span.Slice(2, 2), (ushort)itemData.Length);
            itemData.CopyTo(buffer.Slice(4));
            return(new DataValue(ri, S7DataItemSpecification.TranslateFromMemory(buffer)));
        }
        public async Task ReadWriteSingles()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                const string datablock = "DB1";
                var writeResults       = (await client.WriteAsync(WriteItem.Create(datablock, 10014, (float)0.0))).ToArray();


                var results = (await client.ReadAsync(ReadItem.Create <float>(datablock, 10014))).ToArray();


                Assert.Single(results);
                Assert.Equal(typeof(float), results[0].Type);
                Assert.Equal((float)0.0, (float)results[0].Value);

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, 10014, (float)0.5))).ToArray();

                results = (await client.ReadAsync(ReadItem.Create <float>(datablock, 10014))).ToArray();

                Assert.Single(results);
                Assert.Equal(typeof(float), results[0].Type);
                Assert.Equal((float)0.5, (float)results[0].Value);

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, 10014, (float)0.0))).ToArray();
            });
        }
        public async Task ReadWriteDataAndCheckIfTheResultsDontChangeBecauseOfBufferReusing()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                const string datablock = "DB4";
                const ushort offset    = 2500;
                var resultsDefault0    = new Memory <byte>(Enumerable.Repeat((byte)0x00, 1000).ToArray());
                var resultsDefault1    = await client.WriteAsync(WriteItem.Create(datablock, offset, resultsDefault0));
                var resultsDefault2    = (await client.ReadAsync(ReadItem.Create <byte[]>(datablock, offset, 1000)));

                var first = resultsDefault2.FirstOrDefault();
                var copy1 = new byte[first.Data.Length];
                first.Data.CopyTo(copy1);

                Assert.True(resultsDefault0.Span.SequenceEqual(first.Data.Span), "1");
                Assert.True(resultsDefault0.Span.SequenceEqual(copy1), "2");

                var results0 = new Memory <byte>(Enumerable.Repeat((byte)0x25, 1000).ToArray());
                var results1 = await client.WriteAsync(WriteItem.Create(datablock, offset, results0));
                var results2 = (await client.ReadAsync(ReadItem.Create <byte[]>(datablock, offset, 1000)));

                var second = results2.FirstOrDefault();
                var copy2  = new byte[second.Data.Length];
                second.Data.CopyTo(copy2);


                resultsDefault1 = await client.WriteAsync(WriteItem.Create(datablock, offset, resultsDefault0));
                Assert.True(results0.Span.SequenceEqual(results2.FirstOrDefault().Data.Span), "3");
                Assert.True(results0.Span.SequenceEqual(copy2), "4");


                var results00 = new Memory <byte>(Enumerable.Repeat((byte)0x01, 1000).ToArray());
                var results01 = await client.WriteAsync(WriteItem.Create(datablock, offset, results00));
                var results02 = (await client.ReadAsync(ReadItem.Create <byte[]>(datablock, offset, 1000)));

                var third = results02.FirstOrDefault();
                var copy3 = new byte[third.Data.Length];
                third.Data.CopyTo(copy3);

                resultsDefault1 = await client.WriteAsync(WriteItem.Create(datablock, offset, resultsDefault0));
                Assert.True(results00.Span.SequenceEqual(results02.FirstOrDefault().Data.Span), "5");
                Assert.True(results00.Span.SequenceEqual(copy3), "6");


                Assert.True(resultsDefault0.Span.SequenceEqual(first.Data.Span), "7");
                Assert.True(resultsDefault0.Span.SequenceEqual(copy1), "8");
                Assert.True(first.Data.Span.SequenceEqual(copy1), "9");

                Assert.True(results0.Span.SequenceEqual(second.Data.Span), "10");
                Assert.True(results0.Span.SequenceEqual(copy2), "11");
                Assert.True(second.Data.Span.SequenceEqual(copy2), "12");

                Assert.True(results00.Span.SequenceEqual(third.Data.Span), "13");
                Assert.True(results00.Span.SequenceEqual(copy3), "14");
                Assert.True(third.Data.Span.SequenceEqual(copy3), "15");
            });
        }
        public void TestReadItemInvalidNumberOfBitsThrowsException()
        {
            Assert.Throws <Dacs7TypeNotSupportedException>(() => ReadItem.CreateFromTag("DB3.10000,x0,2"));

            Assert.Throws <Dacs7TypeNotSupportedException>(() => ReadItem.Create <bool[]>("DB1", 0, 2));
            Assert.Throws <Dacs7TypeNotSupportedException>(() => ReadItem.Create <bool>("DB1", 0, 2));

            Assert.Throws <Dacs7TypeNotSupportedException>(() => ReadItem.Create <bool[]>("DB1", 0));
            Assert.Throws <Dacs7TypeNotSupportedException>(() => ReadItem.Create <bool>("DB1", 0, 2));
        }
        public async Task ReadMultibleByteArrayData()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                const string datablock = "DB1";
                var results            = (await client.ReadAsync(ReadItem.Create <byte[]>(datablock, 0, 1000),
                                                                 ReadItem.Create <byte[]>(datablock, 2200, 100),
                                                                 ReadItem.Create <byte[]>(datablock, 1000, 1000),
                                                                 ReadItem.Create <byte[]>(datablock, 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);
            });
        }
        public async Task ReadWriteBigDBData()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                const string datablock = "DB1";
                const ushort offset    = 2500;
                var resultsDefault0    = new Memory <byte>(Enumerable.Repeat((byte)0x00, 1000).ToArray());
                var resultsDefault1    = await client.WriteAsync(WriteItem.Create(datablock, offset, resultsDefault0));
                var resultsDefault2    = (await client.ReadAsync(ReadItem.Create <byte[]>(datablock, offset, 1000)));

                var results0 = new Memory <byte>(Enumerable.Repeat((byte)0x25, 1000).ToArray());
                var results1 = await client.WriteAsync(WriteItem.Create(datablock, offset, results0));
                var results2 = (await client.ReadAsync(ReadItem.Create <byte[]>(datablock, offset, 1000)));


                resultsDefault1 = await client.WriteAsync(WriteItem.Create(datablock, offset, resultsDefault0));
                Assert.True(results0.Span.SequenceEqual(results2.FirstOrDefault().Data.Span));
            });
        }
Beispiel #12
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();
                }
            }
        }
Beispiel #13
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();
            }
        }
        public async Task ReadWriteSingleBits()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                const string datablock = "DB1";
                var baseOffset         = 10000 * 8;
                var writeResults       = (await client.WriteAsync(WriteItem.Create(datablock, baseOffset, false),
                                                                  WriteItem.Create(datablock, baseOffset + 5, false))).ToArray();


                var results = (await client.ReadAsync(ReadItem.Create <bool>(datablock, baseOffset),
                                                      ReadItem.Create <bool>(datablock, baseOffset + 5))).ToArray();


                Assert.Equal(2, results.Count());
                Assert.Equal(typeof(bool), results[0].Type);
                Assert.False((bool)results[0].Value);
                Assert.Equal(typeof(bool), results[1].Type);
                Assert.False((bool)results[1].Value);

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, baseOffset, true),
                                                        WriteItem.Create(datablock, baseOffset + 5, true))).ToArray();

                results = (await client.ReadAsync(ReadItem.Create <bool>(datablock, baseOffset),
                                                  ReadItem.Create <bool>(datablock, baseOffset + 5))).ToArray();

                Assert.Equal(2, results.Count());
                Assert.Equal(typeof(bool), results[0].Type);
                Assert.True((bool)results[0].Value);
                Assert.Equal(typeof(bool), results[1].Type);
                Assert.True((bool)results[1].Value);

                writeResults = (await client.WriteAsync(WriteItem.Create(datablock, baseOffset, false),
                                                        WriteItem.Create(datablock, baseOffset + 5, false))).ToArray();
            });
        }
Beispiel #15
0
 public void CreateTest()
 {
     var ri  = ReadItem.Create <byte[]>("DB1", 0, 10);
     var r2i = ReadItem.Create <byte[]>("DB1081", 0, 1081);
 }
 public void TestReadItemInvalidStringsThrowsException()
 {
     Assert.Throws <Dacs7TypeNotSupportedException>(() => ReadItem.Create <string[]>("DB1", 0));
     Assert.Throws <Dacs7TypeNotSupportedException>(() => ReadItem.Create <string[]>("DB1", 0, 2));
 }