Пример #1
0
        public void Check_ToData()
        {
            var item = new BitsPlcItem(0, 0, BitPosition.X3, 13);

            // Check: Boolean → Data
            var target = Enumerable.Range(0, 13).Select(i => true).ToList();

            item.Value.SetAllBitsTo(true);
            Assert.True(((bool[])item.Value).SequenceEqual(target));

            target = Enumerable.Range(0, 13).Select(i => false).ToList();
            item.Value.SetAllBitsTo(false);
            Assert.True(((bool[])item.Value).SequenceEqual(target));
        }
Пример #2
0
        public void Check_Clone()
        {
            var item  = new BitsPlcItem(0, 0, BitPosition.X3, 13);
            var clone = item.Clone();

            // Check equality (Equals is overriden).
            Assert.AreEqual(item, clone);

            // Check the value.
            Assert.AreEqual(item.Value, clone.Value);

            // Check if both items are different references.
            Assert.False(Object.ReferenceEquals(item, clone));
        }
        public async Task MonitorDifferentIdenticalItems()
        {
            var firstMonitoredItem  = new BytesPlcItem(dataBlock: Data.Datablock, position: Data.StartOfFixedBytes, byteAmount: 2);
            var secondMonitoredItem = new UInt16PlcItem(dataBlock: Data.Datablock, position: Data.StartOfFixedBytes);
            var thirdMonitoredItem  = new BitsPlcItem(dataBlock: Data.Datablock, position: Data.StartOfFixedBytes, bitPosition: BitPosition.X0, bitAmount: 16);

            var differentReadItems = new HashSet <IPlcItem>(new ReferenceComparer());
            var mockPlc            = new MockPlc();
            Func <ICollection <IPlcItem>, CancellationToken, Task> performReadItemsAsyncMock = (plcItems, cancellationToken) =>
            {
                foreach (var plcItem in plcItems)
                {
                    differentReadItems.Add(plcItem);
                }
                return(mockPlc.ReadItemsAsync(plcItems, cancellationToken));
            };

            // Create a mock of MockPlc that signals which items are read.
            var plcMock = new Mock <IPlc>(behavior: MockBehavior.Strict)
            {
                CallBase = true,
            };

            plcMock
            .Setup(x => x.ReadItemsAsync(It.IsAny <IPlcItem[]>(), It.IsAny <CancellationToken>()))
            .Returns(performReadItemsAsyncMock)
            .Verifiable()
            ;

            // Create the monitor with the mocked instance.
            var monitoredPlc = new PollingMonitorablePlc(plcMock.Object);

            // Start monitoring those items.
            monitoredPlc.MonitorItem(firstMonitoredItem);
            monitoredPlc.MonitorItem(secondMonitoredItem);
            monitoredPlc.MonitorItem(thirdMonitoredItem);
            monitoredPlc.Start();
            await Task.Delay(1000);

            monitoredPlc.Stop();
            Assert.AreEqual(1, differentReadItems.Count);
        }
Пример #4
0
        public void CheckItemIdentifierAndPlcString()
        {
            IPlcItem plcItem;

            plcItem = new BitPlcItem(dataBlock: 0, position: 0, bitPosition: BitPosition.X1, identifier: "Bit");
            Assert.AreEqual("Bit", plcItem.Identifier);
            Assert.AreEqual("DB0,X0.1,1", plcItem.PlcString);

            plcItem = new BytePlcItem(dataBlock: 1234, position: 0, identifier: "Byte");
            Assert.AreEqual("Byte", plcItem.Identifier);
            Assert.AreEqual("DB1234,B0,1", plcItem.PlcString);

            plcItem = new BitsPlcItem(type: PlcItemType.Input, dataBlock: 0, position: 0, bitPosition: BitPosition.X1, bitAmount: 2, identifier: "Bits");
            Assert.AreEqual("Bits", plcItem.Identifier);
            Assert.AreEqual("Input,X0.1,2", plcItem.PlcString);

            plcItem = new BytePlcItem(type: PlcItemType.Output, dataBlock: 0, position: 0);
            Assert.AreEqual("Output,B0,1", plcItem.Identifier);
            Assert.AreEqual(plcItem.Identifier, plcItem.PlcString);
        }
Пример #5
0
        public void ReadBits()
        {
            var bitsItem = new BitsPlcItem(dataBlock: this.Data.Datablock, position: 1, bitPosition: BitPosition.X1, bitAmount: 5);

            base.ExecuteTest
            (
                async(plc) =>
            {
                var result = await plc.ReadItemAsync(bitsItem);
                Assert.True(bitsItem.Value == result);

                // Get the target value.
                var booleans         = DataConverter.ToBooleans(this.Data.TargetBytes);
                var relevantBooleans = booleans.Skip(bitsItem.Position * 8 + ((byte)bitsItem.BitPosition)).Take((int)bitsItem.Value.Length).ToArray();
                var target           = new BitCollection(false, relevantBooleans);

                Assert.True(result == target);
            }
            );
        }
        public void WriteBits()
        {
            var allFalse    = new BitCollection(false, new[] { false, false, false, false, false, false });
            var allTrue     = new BitCollection(false, new[] { true, true, true, true, true, true });
            var partialTrue = new BitCollection(false, new[] { true, true, true, true });
            var mixed       = new BitCollection(false, new[] { false, false, true, true, true, true, false, false });

            var fullItem         = new BitsPlcItem(dataBlock: this.Data.Datablock, position: this.Data.StartOfModifiableBytes, BitPosition.X0, bitAmount: 8);
            var writeItem        = new BitsPlcItem(dataBlock: this.Data.Datablock, position: this.Data.StartOfModifiableBytes, BitPosition.X1, initialValue: allTrue);
            var partialWriteItem = new BitsPlcItem(dataBlock: this.Data.Datablock, position: this.Data.StartOfModifiableBytes, BitPosition.X2, initialValue: partialTrue);

            base.ExecuteTest
            (
                async(plc) =>
            {
                // Clear the whole byte.
                fullItem.Value.SetAllBitsTo(false);
                await plc.WriteItemAsync(fullItem);

                var result = await plc.WriteItemWithValidationAsync(writeItem);
                Assert.True(result);

                // Set everything to false.
                writeItem.Value.SetAllBitsTo(false);
                result = await plc.WriteItemWithValidationAsync(writeItem);
                Assert.True(result);

                // Make only a part true.
                result = await plc.WriteItemAsync(partialWriteItem);
                Assert.True(result);

                // Read everything and compare those two.
                var mirror = await plc.ReadItemAsync(fullItem);
                Assert.True(mirror.SequenceEqual(mixed));
            }
            );
        }
Пример #7
0
        public void CheckItemBuilder()
        {
            var itemBuilder = new PlcItemBuilder();

            var plcItem = itemBuilder
                          .Construct("Generic")
                          .ForData()
                          .AtDatablock(0)
                          .AtPosition(0, BitPosition.X2)
                          .ForBitAmount(3)
                          .Build()
            ;

            Assert.AreEqual((uint)3, plcItem.Value.Length);

            BitsPlcItem bitsItem = itemBuilder
                                   .ConstructBitsPlcItem()
                                   .ForFlags()
                                   .AtPosition(20, BitPosition.X5)
                                   .ForBitAmount(5)
                                   .Build()
            ;

            Assert.AreEqual((uint)5, ((IPlcItem)bitsItem).Value.Length);

            BitPlcItem bitItem = itemBuilder
                                 .ConstructBitPlcItem("Bit")
                                 .ForData()
                                 .AtDatablock(0)
                                 .AtPosition(5)
                                 .AsSet()
                                 .Build()
            ;

            Assert.AreEqual((uint)1, ((IPlcItem)bitItem).Value.Length);

            BytesPlcItem bytesItem = itemBuilder
                                     .ConstructBytesPlcItem(identifier: "Bytes")
                                     .ForOutput()
                                     .AtPosition(0)
                                     .WithInitialValue(new[] { byte.MinValue, byte.MaxValue })
                                     .Build()
            ;

            Assert.AreEqual((uint)2, ((IPlcItem)bytesItem).Value.ByteLength);

            BytePlcItem byteItem = itemBuilder
                                   .ConstructBytePlcItem("Byte")
                                   .ForInput()
                                   .AtPosition(10)
                                   .WithInitialValue(Byte.MaxValue)
                                   .Build()
            ;

            Assert.AreEqual((uint)sizeof(Byte), ((IPlcItem)byteItem).Value.ByteLength);

            Int16PlcItem int16Item = itemBuilder
                                     .ConstructInt16PlcItem("Int16")
                                     .AtDatablock(0)
                                     .AtPosition(1)
                                     .WithoutInitialValue()
                                     .Build()
            ;

            Assert.AreEqual((uint)sizeof(Int16), ((IPlcItem)int16Item).Value.ByteLength);

            Int32PlcItem int32Item = itemBuilder
                                     .ConstructInt32PlcItem("Int32")
                                     .AtDatablock(0)
                                     .AtPosition(1)
                                     .WithInitialValue(int.MinValue)
                                     .Build()
            ;

            Assert.AreEqual((uint)sizeof(Int32), ((IPlcItem)int32Item).Value.ByteLength);

            Int64PlcItem int64Item = itemBuilder
                                     .ConstructInt64PlcItem("Int64")
                                     .AtDatablock(0)
                                     .AtPosition(1)
                                     .WithInitialValue(long.MinValue)
                                     .Build()
            ;

            Assert.AreEqual((uint)sizeof(Int64), ((IPlcItem)int64Item).Value.ByteLength);

            UInt16PlcItem uInt16Item = itemBuilder
                                       .ConstructUInt16PlcItem("UInt16")
                                       .AtDatablock(0)
                                       .AtPosition(1)
                                       .WithoutInitialValue()
                                       .Build()
            ;

            Assert.AreEqual((uint)sizeof(UInt16), ((IPlcItem)uInt16Item).Value.ByteLength);

            UInt32PlcItem uInt32PlcItem = itemBuilder
                                          .ConstructUInt32PlcItem("UInt32")
                                          .AtDatablock(0)
                                          .AtPosition(1)
                                          .WithInitialValue(uint.MaxValue)
                                          .Build()
            ;

            Assert.AreEqual((uint)sizeof(UInt32), ((IPlcItem)uInt32PlcItem).Value.ByteLength);

            UInt64PlcItem uInt64PlcItem = itemBuilder
                                          .ConstructUInt64PlcItem("UInt64")
                                          .AtDatablock(0)
                                          .AtPosition(1)
                                          .WithInitialValue(ulong.MaxValue)
                                          .Build()
            ;

            Assert.AreEqual((uint)sizeof(UInt64), ((IPlcItem)uInt64PlcItem).Value.ByteLength);

            WordPlcItem wordItem = itemBuilder
                                   .ConstructWordPlcItem("Word")
                                   .AtDatablock(0)
                                   .AtPosition(2)
                                   .WithInitialValue(32458)
                                   .Build()
            ;

            Assert.AreEqual((uint)2, ((IPlcItem)wordItem).Value.ByteLength);

            DWordPlcItem dwordItem = itemBuilder
                                     .ConstructDWordPlcItem("DWord")
                                     .AtDatablock(0)
                                     .AtPosition(2)
                                     .WithInitialValue(uint.MaxValue)
                                     .Build()
            ;

            Assert.AreEqual((uint)4, ((IPlcItem)dwordItem).Value.ByteLength);

            LWordPlcItem lwordItem = itemBuilder
                                     .ConstructLWordPlcItem("LWord")
                                     .AtDatablock(0)
                                     .AtPosition(2)
                                     .WithInitialValue(ulong.MaxValue)
                                     .Build()
            ;

            Assert.AreEqual((uint)8, ((IPlcItem)lwordItem).Value.ByteLength);

            TextPlcItem textItem = itemBuilder
                                   .ConstructTextPlcItem("Text")
                                   .WithEncoding(Encoding.UTF7)
                                   .AtDatablock(0)
                                   .AtPosition(3)
                                   .WithInitialValue("Some String")
                                   .Build()
            ;

            Assert.AreEqual((uint)Encoding.UTF7.GetBytes("Some String").Length, ((IPlcItem)textItem).Value.ByteLength);

            Utf8PlcItem utf8Item = itemBuilder
                                   .ConstructUtf8PlcItem("UTF-8")
                                   .AtDatablock(0)
                                   .AtPosition(4)
                                   .WithLength(10)
                                   .Build()
            ;

            Assert.AreEqual((uint)10, ((IPlcItem)utf8Item).Value.ByteLength);

            var initialText = "String whose length fits into a single byte.";
            DynamicUtf8PlcItem secondDynamicUtf8Item = itemBuilder
                                                       .ConstructUtf8PlcItem("UTF-8")
                                                       .AtDatablock(0)
                                                       .AtPosition(4)
                                                       .WithDynamicItemFromInitialValue(initialText)
                                                       .BuildDynamic()
            ;

            Assert.That(secondDynamicUtf8Item.LengthPlcItem.Value, Is.EqualTo((uint)Encoding.UTF8.GetBytes(initialText).Length));
            Assert.AreEqual(initialText, secondDynamicUtf8Item.Value);

            var items = new []
            {
                plcItem,
                bitsItem,
                bitItem,
                bytesItem,
                byteItem,
                int16Item,
                int32Item,
                int64Item,
                uInt16Item,
                uInt32PlcItem,
                uInt64PlcItem,
                wordItem,
                dwordItem,
                lwordItem,
                textItem,
                utf8Item,
                secondDynamicUtf8Item,
            };

            foreach (var item in items)
            {
                Debug.WriteLine($" -> {item}");
            }
        }