示例#1
0
        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();
            });
        }
示例#2
0
        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();
            });
        }
示例#3
0
        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();
            });
        }
示例#4
0
        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);
        }
示例#5
0
        public async Task CreateWriteReferenceTest()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                var item         = WriteItem.Create("DB210", 192, false);
                var writeResults = (await client.WriteAsync(item)).ToArray();

                item         = WriteItem.Create("DB210", 193, false);
                writeResults = (await client.WriteAsync(item)).ToArray();


                item         = WriteItem.Create("DB210", 35440, false);
                writeResults = (await client.WriteAsync(item)).ToArray();


                item         = WriteItem.Create("DB210", 35441, false);
                writeResults = (await client.WriteAsync(item)).ToArray();


                item         = WriteItem.Create("DB210", 70688, false);
                writeResults = (await client.WriteAsync(item)).ToArray();


                item         = WriteItem.Create("DB210", 70689, false);
                writeResults = (await client.WriteAsync(item)).ToArray();

                item         = WriteItem.Create("DB210", 105936, false);
                writeResults = (await client.WriteAsync(item)).ToArray();


                item         = WriteItem.Create("DB210", 105937, false);
                writeResults = (await client.WriteAsync(item)).ToArray();
            });
        }
示例#6
0
        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();
            });
        }
示例#7
0
        private static IEnumerable <WriteItem> BuildWritePackages(DataPack w)
        {
            var result = new List <WriteItem>();

            if (!w.HasBitMask)
            {
                result.Add(WriteItem.Create(w.Selector, (ushort)w.Offset, w.Data));
            }
            else
            {
                if (w.Data.Length > 0)
                {
                    SetupBitMask(w, result, end: false);
                }

                if (w.Data.Length > 2)
                {
                    result.Add(WriteItem.Create(w.Selector, (w.Offset + 1), w.Data.Slice(1, w.Data.Length - 2)));
                }

                if (w.Data.Length > 1)
                {
                    SetupBitMask(w, result, end: true);
                }
            }
            return(result);
        }
示例#8
0
 public void TestWriteItemInvalidStringsThrowsException()
 {
     Assert.Throws <InvalidCastException>(() => WriteItem.CreateFromTag("DB2.10046,s,20", new[] { "", "" }));
     Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.CreateFromTag("DB2.10046,s,5", "      "));
     Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.CreateFromTag("DB2.10046,s", "123456"));
     Assert.Throws <Dacs7TypeNotSupportedException>(() => WriteItem.Create <string[]>("DB1", 0, 2, new[] { "", "" }));
 }
示例#9
0
        public async Task CreateMultiWriteTest()
        {
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                var items = new List <WriteItem>();
                var data  = new byte[18];

                items.Add(WriteItem.Create("DB1993", 8, new byte[86]));

                items.Add(WriteItem.Create("DB1993", 2122, new byte[878]));
                items.Add(WriteItem.Create("DB1993", 102, new byte[18]));
                items.Add(WriteItem.Create("DB1993", 3008, new byte[492]));
                items.Add(WriteItem.Create("DB1993", 4122, new byte[378]));
                items.Add(WriteItem.Create("DB1993", 122, new byte[18]));
                items.Add(WriteItem.Create("DB1993", 14, new byte[86]));
                items.Add(WriteItem.Create("DB1993", 722, new byte[78]));
                items.Add(WriteItem.Create("DB1993", 1422, new byte[78]));

                for (int i = 0; i < 37; i++)
                {
                    items.Add(WriteItem.Create("DB1993", 142 + (i * 20), data));
                }

                var writeResults = (await client.WriteAsync(items)).ToArray();
            });
        }
示例#10
0
        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");
            });
        }
示例#11
0
 public void TestWriteItemInvalidNumberOfBitsThrowsException()
 {
     Assert.Throws <Dacs7TypeNotSupportedException>(() => WriteItem.CreateFromTag("DB3.10000,x0,2", new[] { false, false }));
     Assert.Throws <Dacs7TypeNotSupportedException>(() => WriteItem.CreateFromTag("DB3.10000,x0", new[] { false, false }));
     Assert.Throws <Dacs7TypeNotSupportedException>(() => WriteItem.CreateFromTag("DB3.10000,x0", new[] { false, false }));
     Assert.Throws <Dacs7TypeNotSupportedException>(() => WriteItem.CreateFromTag("DB3.10000,x0,2", false));
     Assert.Throws <Dacs7TypeNotSupportedException>(() => WriteItem.Create("DB1", 0, 2, false));
     Assert.Throws <Dacs7TypeNotSupportedException>(() => WriteItem.Create("DB1", 0, new[] { false, false }));
     Assert.Throws <Dacs7TypeNotSupportedException>(() => WriteItem.Create("DB1", 0, 2, new[] { false, false }));
 }
示例#12
0
        public async Task WriteString()
        {
            const string datablock = "DB962";
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                var data = new List <WriteItem> {
                    WriteItem.Create <string>(datablock, 12, 8, "12345678")
                };

                var writeResults = await client.WriteAsync(data);
                Assert.Equal(data.Count, writeResults.Count());
                Assert.True(writeResults.All(x => x == ItemResponseRetValue.Success));
            }, 240);
        }
示例#13
0
        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));
            });
        }
示例#14
0
        public async Task WriteMultiCharsAndString()
        {
            const string datablock = "DB962";
            await PlcTestServer.ExecuteClientAsync(async (client) =>
            {
                var data = new List <WriteItem> {
                    WriteItem.Create <char[]>(datablock, 60, 14, "|010101010101|".ToCharArray()),
                    WriteItem.Create <char[]>(datablock, 2, 10, "|01010101|".ToCharArray()),
                    WriteItem.Create <string>(datablock, 12, 8, "|123456|"),
                    WriteItem.Create <char[]>(datablock, 36, 6, "|1234|".ToCharArray()),
                    WriteItem.Create <char[]>(datablock, 24, 4, "|12|".ToCharArray()),
                    WriteItem.Create <char[]>(datablock, 30, 4, "|12|".ToCharArray()),
                    WriteItem.Create <char[]>(datablock, 44, 4, "|12|".ToCharArray())
                };

                var writeResults = await client.WriteAsync(data);
                Assert.Equal(data.Count, writeResults.Count());
                Assert.True(writeResults.All(x => x == ItemResponseRetValue.Success));
            }, 240);
        }
示例#15
0
        public void TestWriteItemInvalidDataLengthThrowsException()
        {
            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 2, new byte[] { 0 }));
            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 1, new byte[] { 0, 0 }));

            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 2, new ushort[] { 0 }));
            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 1, new ushort[] { 0, 0 }));

            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 2, new short[] { 0 }));
            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 1, new short[] { 0, 0 }));

            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 2, new int[] { 0 }));
            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 1, new int[] { 0, 0 }));

            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 2, new uint[] { 0 }));
            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 1, new uint[] { 0, 0 }));

            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 2, new float[] { 0.0f }));
            Assert.Throws <ArgumentOutOfRangeException>(() => WriteItem.Create("DB1", 0, 1, new float[] { 0.0f, 0.0f }));
        }
示例#16
0
        private static void SetupBitMask(DataPack w, List <WriteItem> result, bool end = false)
        {
            var bytePos       = end ? w.Data.Length - 1 : 0;
            var bm            = end ? w.BitMaskEnd : w.BitMaskBegin;
            var currentByte   = w.Data.Span[bytePos];
            var currentOffset = (w.Offset + bytePos) * 8;

            for (var j = 0; j < 8; j++)
            {
                if (bm.GetBit(j))
                {
                    var bitOffset = (currentOffset + j);
                    result.Add(WriteItem.Create(w.Selector, bitOffset, currentByte.GetBit(j)));
                    bm = bm.SetBit(j, false);
                    if (bm == 0)
                    {
                        break;
                    }
                }
            }
        }
示例#17
0
        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();
            });
        }