Exemplo n.º 1
0
        public void ReadDynamicText()
        {
            var itemBuilder = new Items.Builder.PlcItemBuilder();

            var target     = "ABC";
            var targetData = Encoding.UTF8.GetBytes(target);

            var bytesItem = itemBuilder
                            .ConstructBytesPlcItem()
                            .ForData()
                            .AtDatablock(Data.Datablock)
                            .AtPosition(Data.StartOfModifiableBytes)
                            .WithInitialValue(new[] { (byte)targetData.Length }.Concat(targetData).ToArray())
                            .Build()
            ;

            var dynamicTextItem = itemBuilder
                                  .ConstructUtf8PlcItem()
                                  .AtDatablock(Data.Datablock)
                                  .AtPosition(Data.StartOfModifiableBytes)
                                  .WithDynamicItem <BytePlcItem>()
                                  .BuildDynamic()
            ;

            base.ExecuteTest
            (
                async(plc) =>
            {
                var success = await plc.WriteItemAsync(bytesItem);
                Assert.True(success);

                // Read the dynamic item.
                var result = await plc.ReadItemAsync(dynamicTextItem);

                //Check value and length.
                Assert.AreEqual(target, result);
                Assert.AreEqual(target, dynamicTextItem.Value);
                Assert.AreEqual(result.Length, ((BytePlcItem)dynamicTextItem.LengthPlcItem).Value);
            }
            );
        }
        public void WriteDynamic()
        {
            var itemBuilder = new Items.Builder.PlcItemBuilder();

            var target = "ABC";
            //var targetData = Encoding.UTF8.GetBytes(target);

            var bytesItem = itemBuilder
                            .ConstructBytesPlcItem()
                            .ForData()
                            .AtDatablock(Data.Datablock)
                            .AtPosition(Data.StartOfModifiableBytes)
                            .ForByteAmount((uint)Data.WriteBytes.Length)
                            .Build()
            ;

            var dynamicTextItem = itemBuilder
                                  .ConstructUtf8PlcItem()
                                  .AtDatablock(Data.Datablock)
                                  .AtPosition(Data.StartOfModifiableBytes)
                                  .WithDynamicItemFromInitialValue(target)
                                  .BuildDynamic()
            ;

            base.ExecuteTest
            (
                async(plc) =>
            {
                // Clear the bytes.
                var result = await plc.WriteItemWithValidationAsync(bytesItem);
                Assert.True(result);

                // Write the whole dynamic item.
                result = await plc.WriteItemWithValidationAsync(dynamicTextItem);
                Assert.True(result);
            }
            );
        }