public void Success_NonNullable(FlatBufferDeserializationOption option)
        {
            FlatBufferSerializer serializer = new FlatBufferSerializer(option);

            WriteThroughTable_Required <ValueStruct> table = new()
            {
                Item = new ValueStruct {
                    Value = 1
                }
            };

            byte[] result = new byte[1024];

            var code = serializer.Compile <WriteThroughTable_Required <ValueStruct> >().CSharp;

            serializer.Serialize(table, result);

            var parsed = serializer.Parse <WriteThroughTable_Required <ValueStruct> >(result);

            Assert.Equal(1, parsed.Item.Value);

            parsed.Item = new ValueStruct {
                Value = 4
            };

            // Re-read and verify the in-struct writethrough succeeded.
            parsed = serializer.Parse <WriteThroughTable_Required <ValueStruct> >(result);
            Assert.Equal(4, parsed.Item.Value);
        }
示例#2
0
        public void ReadBareMinParams([Values] TestUtils.DeviceType deviceType)
        {
            Setup(128, new LogSettings {
                MemorySizeBits = 22, SegmentSizeBits = 22, PageSizeBits = 10
            }, deviceType);
            uContext.ResumeThread();

            try
            {
                var key1 = new KeyStruct {
                    kfield1 = 13, kfield2 = 14
                };
                var value = new ValueStruct {
                    vfield1 = 23, vfield2 = 24
                };

                uContext.Upsert(ref key1, ref value, Empty.Default, 0);

                var(status, output) = uContext.Read(key1);
                AssertCompleted(new(StatusCode.Found), status);

                Assert.AreEqual(value.vfield1, output.value.vfield1);
                Assert.AreEqual(value.vfield2, output.value.vfield2);
                Assert.AreEqual(key1.kfield1, 13);
                Assert.AreEqual(key1.kfield2, 14);
            }
            finally
            {
                uContext.SuspendThread();
            }
        }
示例#3
0
        public void NativeInMemWriteRead([Values] TestUtils.DeviceType deviceType)
        {
            Setup(128, new LogSettings {
                PageSizeBits = 10, MemorySizeBits = 12, SegmentSizeBits = 22
            }, deviceType);
            uContext.ResumeThread();

            try
            {
                InputStruct  input  = default;
                OutputStruct output = default;

                var key1 = new KeyStruct {
                    kfield1 = 13, kfield2 = 14
                };
                var value = new ValueStruct {
                    vfield1 = 23, vfield2 = 24
                };

                uContext.Upsert(ref key1, ref value, Empty.Default, 0);
                var status = uContext.Read(ref key1, ref input, ref output, Empty.Default, 0);

                AssertCompleted(new (StatusCode.Found), status);
                Assert.AreEqual(value.vfield1, output.value.vfield1);
                Assert.AreEqual(value.vfield2, output.value.vfield2);
            }
            finally
            {
                uContext.SuspendThread();
            }
        }
示例#4
0
        public void ReadNoRefKeyInputOutput([Values] TestUtils.DeviceType deviceType)
        {
            InputStruct input = default;

            Setup(128, new LogSettings {
                MemorySizeBits = 22, SegmentSizeBits = 22, PageSizeBits = 10
            }, deviceType);
            uContext.ResumeThread();

            try
            {
                var key1 = new KeyStruct {
                    kfield1 = 13, kfield2 = 14
                };
                var value = new ValueStruct {
                    vfield1 = 23, vfield2 = 24
                };

                uContext.Upsert(ref key1, ref value, Empty.Default, 0);
                var status = uContext.Read(key1, input, out OutputStruct output, Empty.Default, 111);
                AssertCompleted(new(StatusCode.Found), status);

                // Verify the read data
                Assert.AreEqual(value.vfield1, output.value.vfield1);
                Assert.AreEqual(value.vfield2, output.value.vfield2);
                Assert.AreEqual(key1.kfield1, 13);
                Assert.AreEqual(key1.kfield2, 14);
            }
            finally
            {
                uContext.SuspendThread();
            }
        }
示例#5
0
        public void SessionTest1()
        {
            using var session = fht.NewSession(new Functions());
            InputStruct  input  = default;
            OutputStruct output = default;

            var key1 = new KeyStruct {
                kfield1 = 13, kfield2 = 14
            };
            var value = new ValueStruct {
                vfield1 = 23, vfield2 = 24
            };

            session.Upsert(ref key1, ref value, Empty.Default, 0);
            var status = session.Read(ref key1, ref input, ref output, Empty.Default, 0);

            if (status.IsPending)
            {
                session.CompletePendingWithOutputs(out var outputs, wait: true);
                (status, output) = GetSinglePendingResult(outputs);
            }

            Assert.IsTrue(status.Found);
            Assert.AreEqual(value.vfield1, output.value.vfield1);
            Assert.AreEqual(value.vfield2, output.value.vfield2);
        }
示例#6
0
        public void SessionTest1()
        {
            using var session = fht.NewSession(new Functions());
            InputStruct  input  = default;
            OutputStruct output = default;

            var key1 = new KeyStruct {
                kfield1 = 13, kfield2 = 14
            };
            var value = new ValueStruct {
                vfield1 = 23, vfield2 = 24
            };

            session.Upsert(ref key1, ref value, Empty.Default, 0);
            var status = session.Read(ref key1, ref input, ref output, Empty.Default, 0);

            if (status == Status.PENDING)
            {
                session.CompletePending(true);
            }
            else
            {
                Assert.IsTrue(status == Status.OK);
            }

            Assert.IsTrue(output.value.vfield1 == value.vfield1);
            Assert.IsTrue(output.value.vfield2 == value.vfield2);
        }
示例#7
0
        public void SessionTest4()
        {
            using var session1 = fht.NewSession(new Functions());
            using var session2 = fht.NewSession(new Functions());
            var t1 = Task.CompletedTask.ContinueWith((t) =>
            {
                InputStruct input   = default;
                OutputStruct output = default;

                var key1 = new KeyStruct {
                    kfield1 = 14, kfield2 = 15
                };
                var value1 = new ValueStruct {
                    vfield1 = 24, vfield2 = 25
                };

                session1.Upsert(ref key1, ref value1, Empty.Default, 0);
                var status = session1.Read(ref key1, ref input, ref output, Empty.Default, 0);

                if (status.IsPending)
                {
                    session1.CompletePendingWithOutputs(out var outputs, wait: true);
                    (status, output) = GetSinglePendingResult(outputs);
                }

                Assert.IsTrue(status.Found);
                Assert.AreEqual(value1.vfield1, output.value.vfield1);
                Assert.AreEqual(value1.vfield2, output.value.vfield2);
            });

            var t2 = Task.CompletedTask.ContinueWith((t) =>
            {
                InputStruct input   = default;
                OutputStruct output = default;

                var key2 = new KeyStruct {
                    kfield1 = 15, kfield2 = 16
                };
                var value2 = new ValueStruct {
                    vfield1 = 25, vfield2 = 26
                };

                session2.Upsert(ref key2, ref value2, Empty.Default, 0);

                var status = session2.Read(ref key2, ref input, ref output, Empty.Default, 0);

                if (status.IsPending)
                {
                    session2.CompletePendingWithOutputs(out var outputs, wait: true);
                    (status, output) = GetSinglePendingResult(outputs);
                }

                Assert.IsTrue(status.Found);
                Assert.AreEqual(value2.vfield1, output.value.vfield1);
                Assert.AreEqual(value2.vfield2, output.value.vfield2);
            });

            t1.Wait();
            t2.Wait();
        }
示例#8
0
 /// <summary>
 /// 将采集到的数值更新到内部
 /// 注意:
 ///		1、如果值相等,允许重复设置。此时仅仅影响 _UpdateTime
 ///		2、如果已更新、且数值发生改变,则不再允许设置新值
 ///		3、除非Backup()之后,方才允许设置新值
 /// </summary>
 public void SetNewValue(ValueStruct value)
 {
     if (!_Changed)
     {
         SetNewValueForce(value);
     }
 }
示例#9
0
 /// <summary>
 /// 设置开关量“输出值”
 /// 如果是开关量1=True,0=False
 /// </summary>
 public void SetNewOutput(bool value, OUTPUT_MODE outMode)
 {
     Output      = new ValueStruct(value);
     _OutputMode = outMode;
     _UpdateTime = DateTime.Now;
     _Changed    = !_OutputMode.Equals(CurrValue);
 }
示例#10
0
        public void SessionTest3()
        {
            using var session = fht.NewSession(new Functions());
            Task.CompletedTask.ContinueWith((t) =>
            {
                InputStruct input   = default;
                OutputStruct output = default;

                var key1 = new KeyStruct {
                    kfield1 = 13, kfield2 = 14
                };
                var value = new ValueStruct {
                    vfield1 = 23, vfield2 = 24
                };

                session.Upsert(ref key1, ref value, Empty.Default, 0);
                var status = session.Read(ref key1, ref input, ref output, Empty.Default, 0);

                if (status == Status.PENDING)
                {
                    session.CompletePending(true);
                }
                else
                {
                    Assert.AreEqual(Status.OK, status);
                }

                Assert.AreEqual(value.vfield1, output.value.vfield1);
                Assert.AreEqual(value.vfield2, output.value.vfield2);
            }).Wait();
        }
        public void Success_Nullable(FlatBufferDeserializationOption option)
        {
            FlatBufferSerializer serializer = new FlatBufferSerializer(option);

            WriteThroughTable_Required <ValueStruct?> table = new()
            {
                Item = new ValueStruct {
                    Value = 1
                }
            };

            byte[] result = new byte[1024];

            var code = serializer.Compile <WriteThroughTable_Required <ValueStruct?> >().CSharp;

            serializer.Serialize(table, result);

            var parsed = serializer.Parse <WriteThroughTable_Required <ValueStruct?> >(result);

            Assert.Equal(1, parsed.Item.Value.Value);

            parsed.Item = new ValueStruct {
                Value = 4
            };

            // Re-read and verify the in-struct writethrough succeeded.
            parsed = serializer.Parse <WriteThroughTable_Required <ValueStruct?> >(result);
            Assert.Equal(4, parsed.Item.Value.Value);

            var ex = Assert.Throws <InvalidOperationException>(() => parsed.Item = null);

            Assert.Equal(
                "Nullable object must have a value.",
                ex.Message);
        }
示例#12
0
 public AcquireRawValue(int id, int controllerId, int controllerChannelNo, double value)
 {
     _Id                  = id;
     _ControllerId        = controllerId;
     _ControllerChannelNo = controllerChannelNo;
     _PLCAddr             = null;
     Value                = new ValueStruct(value);
 }
示例#13
0
        ValueStruct Convert(object obj)
        {
            ValueStruct val = new ValueStruct();

            val.Type  = GetTypeMarker(obj);
            val.Value = GetValue(obj, val.Type);
            return(val);
        }
示例#14
0
        public void SessionTest2()
        {
            using (var session1 = fht.NewSession())
                using (var session2 = fht.NewSession())
                {
                    InputStruct  input  = default(InputStruct);
                    OutputStruct output = default(OutputStruct);

                    var key1 = new KeyStruct {
                        kfield1 = 14, kfield2 = 15
                    };
                    var value1 = new ValueStruct {
                        vfield1 = 24, vfield2 = 25
                    };
                    var key2 = new KeyStruct {
                        kfield1 = 15, kfield2 = 16
                    };
                    var value2 = new ValueStruct {
                        vfield1 = 25, vfield2 = 26
                    };

                    session1.Upsert(ref key1, ref value1, Empty.Default, 0);
                    session2.Upsert(ref key2, ref value2, Empty.Default, 0);

                    var status = session1.Read(ref key1, ref input, ref output, Empty.Default, 0);

                    if (status == Status.PENDING)
                    {
                        session1.CompletePending(true);
                    }
                    else
                    {
                        Assert.IsTrue(status == Status.OK);
                    }

                    Assert.IsTrue(output.value.vfield1 == value1.vfield1);
                    Assert.IsTrue(output.value.vfield2 == value1.vfield2);

                    status = session2.Read(ref key2, ref input, ref output, Empty.Default, 0);

                    if (status == Status.PENDING)
                    {
                        session2.CompletePending(true);
                    }
                    else
                    {
                        Assert.IsTrue(status == Status.OK);
                    }

                    Assert.IsTrue(output.value.vfield1 == value2.vfield1);
                    Assert.IsTrue(output.value.vfield2 == value2.vfield2);
                }
        }
示例#15
0
        /// <summary>
        /// 强制将采集到的数值更新到内部
        /// 说明:
        ///		一般不直接使用强制更新,除非有充分的理由 ~_*
        /// </summary>
        public void SetNewValueForce(double value)
        {
            _UpdateTime = DateTime.Now;

            CurrValue = new ValueStruct(value);

            if (CurrValue != PreviousValue)
            {
                _Changed         = true;
                _LastChangedTime = _UpdateTime;
            }
        }
示例#16
0
        public void SessionTest2()
        {
            using var session1 = fht.NewSession(new Functions());
            using var session2 = fht.NewSession(new Functions());
            InputStruct  input  = default;
            OutputStruct output = default;

            var key1 = new KeyStruct {
                kfield1 = 14, kfield2 = 15
            };
            var value1 = new ValueStruct {
                vfield1 = 24, vfield2 = 25
            };
            var key2 = new KeyStruct {
                kfield1 = 15, kfield2 = 16
            };
            var value2 = new ValueStruct {
                vfield1 = 25, vfield2 = 26
            };

            session1.Upsert(ref key1, ref value1, Empty.Default, 0);
            session2.Upsert(ref key2, ref value2, Empty.Default, 0);

            var status = session1.Read(ref key1, ref input, ref output, Empty.Default, 0);

            if (status == Status.PENDING)
            {
                session1.CompletePending(true);
            }
            else
            {
                Assert.AreEqual(Status.OK, status);
            }

            Assert.AreEqual(value1.vfield1, output.value.vfield1);
            Assert.AreEqual(value1.vfield2, output.value.vfield2);

            status = session2.Read(ref key2, ref input, ref output, Empty.Default, 0);

            if (status == Status.PENDING)
            {
                session2.CompletePending(true);
            }
            else
            {
                Assert.AreEqual(Status.OK, status);
            }

            Assert.AreEqual(value2.vfield1, output.value.vfield1);
            Assert.AreEqual(value2.vfield2, output.value.vfield2);
        }
示例#17
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="id">IO点/软单元/采集点全局唯一索引号</param>
 public AcquireValue(int id)
 {
     _Id              = id;
     CurrValue        = new ValueStruct();
     PreviousValue    = new ValueStruct();
     Output           = new ValueStruct();
     DefaultValue     = ValueStruct.NullDefault;
     _OutputMode      = OUTPUT_MODE.omAuto;
     _ZeroOffset      = 0;
     _Gain            = 0;
     _CalibrateValue  = 0;
     _UpdateTime      = DateTime.MinValue;
     _LastChangedTime = DateTime.MinValue;
     _Changed         = false;
 }
示例#18
0
        /// <summary>
        /// 设置开关量“输出值”根据当前值翻转
        /// 如果当前值是 Null、False,则输出为 True,否则输出 False
        /// </summary>
        public void SetNewOutputRollingOver(OUTPUT_MODE outMode)
        {
            if (CurrValue.IsNull || CurrValue.B == false)
            {
                Output = new ValueStruct(true);
            }
            else
            {
                Output = new ValueStruct(false);
            }

            _OutputMode = outMode;
            _UpdateTime = DateTime.Now;
            _Changed    = true;
        }
示例#19
0
        public AcquireRawValue(int id, IControllerBase controller, string controllerChannelAlias, double value)
        {
            _Id                  = id;
            _ControllerId        = controller.ControllerId;
            _ControllerChannelNo = 0;

            if (controller.ControllerType.IsPLCController())
            {
                _PLCAddr = new FxAddress(controllerChannelAlias, controller.ControllerType);
            }
            else
            {
                _PLCAddr = null;
            }
            Value = new ValueStruct(value);
        }
示例#20
0
    public void ExtensionMethod_WorksForVectors()
    {
        ValueStruct v = default;

        Assert.Equal(128, v.D_Length);

        for (int i = 0; i < v.D_Length; ++i)
        {
            v.D(i) = (byte)i;
        }

        for (int i = 0; i < v.D_Length; ++i)
        {
            Assert.Equal((byte)i, v.D(i));
        }
    }
示例#21
0
        /// <summary>
        /// 强制将采集到的数值更新到内部
        /// 说明:
        ///		一般不直接使用强制更新,除非有充分的理由 ~_*
        /// </summary>
        public void SetNewValueForce(ValueStruct value)
        {
            _UpdateTime = DateTime.Now;

            CurrValue = value;

            if (CurrValue != PreviousValue)
            {
                _Changed         = true;
                _LastChangedTime = _UpdateTime;
            }

            // 如果是“脉冲式开关量”,则将初次获取值视作“默认值”
            if (DefaultValue.IsNull && value.ValueTypeConst == ValueTypeEnum.vtBool)
            {
                DefaultValue = value;
            }
        }
示例#22
0
    static void Main(string[] args)
    {
        var value1 = new ValueStruct()
        {
            byte1 = 0x88, byte2 = 0x99, byte3 = 0xAA, byte4 = 0xBB
        };
        var value2 = new ValueStruct()
        {
            byte1 = 0x11, byte2 = 0x22, byte3 = 0x33, byte4 = 0x44
        };

        Console.WriteLine(value1.uint1);
        Console.WriteLine(value2.uint1);
        if (value1.uint1 > value2.uint1)
        {
            Console.WriteLine("value1 is greater than value2");
        }
        Console.ReadLine();
    }
示例#23
0
        public void TypeCastPrimitiveTypes()
        {
            long        originalPrimitiveType = long.MaxValue;
            Span <byte> byteSpan          = BitConverter.GetBytes(originalPrimitiveType);
            Span <int>  intSpan           = MemoryMarshal.Cast <byte, int>(byteSpan);
            int         castPrimitiveType = intSpan[0];

            Console.WriteLine($"Primitive Test : OriginalValue (long) = {originalPrimitiveType}, Cast Value (int) = {castPrimitiveType}");

            var structInstance = new ValueStruct(25, 01);

            byteSpan = BitConverter.GetBytes(structInstance.Property1);
            intSpan  = MemoryMarshal.Cast <byte, int>(byteSpan);
            int castStructType = intSpan[0];

            Console.WriteLine($"Struct Test : OriginalValue (long) = {structInstance.Property1}, Cast Value (int) = {castStructType}");

            Console.ReadLine();
        }
示例#24
0
    public void SerializeAndParse_Full()
    {
        ValueStruct vs = new ValueStruct
        {
            A     = 1,
            B     = 2,
            C     = 3,
            Inner = new InnerValueStruct {
                A = 3.14f
            }
        };

        for (int i = 0; i < vs.D_Length; ++i)
        {
            vs.D(i) = (byte)i;
        }

        RefStruct rs = new()
        {
            A  = 1,
            VS = vs,
        };

        RootTable table = new()
        {
            refStruct         = rs,
            union             = new TestUnion(vs),
            valueStruct       = vs,
            valueStructVector = Enumerable.Range(0, 10).Select(x => vs).ToList(),
            vectorOfUnion     = Enumerable.Range(0, 10).Select(x => new TestUnion(vs)).ToList(),
        };

        ISerializer <RootTable> serializer = RootTable.Serializer;
        int maxBytes = serializer.GetMaxSize(table);

        byte[]    buffer  = new byte[maxBytes];
        int       written = serializer.Write(buffer, table);
        RootTable parsed  = serializer.Parse(buffer[..written]);
示例#25
0
        public void ReadAtAddressReadFlagsNone()
        {
            // Just functional test of ReadFlag so one device is enough
            deviceType = TestUtils.DeviceType.MLSD;

            Setup(128, new LogSettings {
                MemorySizeBits = 29
            }, deviceType);
            uContext.ResumeThread();

            try
            {
                InputStruct  input  = default;
                OutputStruct output = default;

                var key1 = new KeyStruct {
                    kfield1 = 13, kfield2 = 14
                };
                var value = new ValueStruct {
                    vfield1 = 23, vfield2 = 24
                };
                ReadOptions readOptions = new() { StartAddress = fht.Log.BeginAddress };

                uContext.Upsert(ref key1, ref value, Empty.Default, 0);
                var status = uContext.ReadAtAddress(ref input, ref output, ref readOptions, Empty.Default, 0);
                AssertCompleted(new(StatusCode.Found), status);

                Assert.AreEqual(value.vfield1, output.value.vfield1);
                Assert.AreEqual(value.vfield2, output.value.vfield2);
                Assert.AreEqual(key1.kfield1, 13);
                Assert.AreEqual(key1.kfield2, 14);
            }
            finally
            {
                uContext.SuspendThread();
            }
        }
示例#26
0
        public void ReadWithoutSerialID()
        {
            // Just checking without Serial ID so one device type is enough
            deviceType = TestUtils.DeviceType.MLSD;

            Setup(128, new LogSettings {
                MemorySizeBits = 29
            }, deviceType);
            uContext.ResumeThread();

            try
            {
                InputStruct  input  = default;
                OutputStruct output = default;

                var key1 = new KeyStruct {
                    kfield1 = 13, kfield2 = 14
                };
                var value = new ValueStruct {
                    vfield1 = 23, vfield2 = 24
                };

                uContext.Upsert(ref key1, ref value, Empty.Default, 0);
                var status = uContext.Read(ref key1, ref input, ref output, Empty.Default);
                AssertCompleted(new(StatusCode.Found), status);

                Assert.AreEqual(value.vfield1, output.value.vfield1);
                Assert.AreEqual(value.vfield2, output.value.vfield2);
                Assert.AreEqual(key1.kfield1, 13);
                Assert.AreEqual(key1.kfield2, 14);
            }
            finally
            {
                uContext.SuspendThread();
            }
        }
 public static void ChangeAProperty(ref ValueStruct valueStruct)
 {
     ++valueStruct.AProperty;
 }
示例#28
0
 /// <summary>
 /// 设置新的输出值,支持 Double(模拟量)/Int(数字量)/Bool(开关量)
 /// 如果是开关量,则可取 True(1)、False(0) 值
 /// </summary>
 public void SetNewOutput(ValueStruct value)
 {
     SetNewOutput(value, OUTPUT_MODE.omAuto);
 }
示例#29
0
 /// <summary>
 /// 将输出值复位
 /// 准备进入逻辑判断、运算环节前复位输出
 /// </summary>
 public void ResetOutput()
 {
     Output      = ValueStruct.NullDefault;
     _OutputMode = OUTPUT_MODE.omAuto;
     _UpdateTime = DateTime.Now;
 }
 public static void ChangeTheInstanceOfValueStruct(ref ValueStruct valueStruct)
 {
     valueStruct = new ValueStruct { AField = 1, AProperty = 1 };
 }
 public static void ChangeAField(ValueStruct valueStruct)
 {
     ++valueStruct.AField;
 }
示例#32
0
 /// <summary>
 /// 将“当前值”备份到“前一次值”
 /// </summary>
 public void Backup()
 {
     PreviousValue = CurrValue;
     Output        = new ValueStruct(ValueTypeEnum.vtNull);
     _Changed      = false;
 }
示例#33
0
        public void SessionTest5()
        {
            var session = fht.NewSession(new Functions());

            InputStruct  input  = default;
            OutputStruct output = default;

            var key1 = new KeyStruct {
                kfield1 = 16, kfield2 = 17
            };
            var value1 = new ValueStruct {
                vfield1 = 26, vfield2 = 27
            };

            session.Upsert(ref key1, ref value1, Empty.Default, 0);
            var status = session.Read(ref key1, ref input, ref output, Empty.Default, 0);

            if (status == Status.PENDING)
            {
                session.CompletePending(true);
            }
            else
            {
                Assert.IsTrue(status == Status.OK);
            }

            Assert.IsTrue(output.value.vfield1 == value1.vfield1);
            Assert.IsTrue(output.value.vfield2 == value1.vfield2);

            session.Dispose();

            session = fht.NewSession(new Functions());

            var key2 = new KeyStruct {
                kfield1 = 17, kfield2 = 18
            };
            var value2 = new ValueStruct {
                vfield1 = 27, vfield2 = 28
            };

            session.Upsert(ref key2, ref value2, Empty.Default, 0);

            status = session.Read(ref key2, ref input, ref output, Empty.Default, 0);

            if (status == Status.PENDING)
            {
                session.CompletePending(true);
            }
            else
            {
                Assert.IsTrue(status == Status.OK);
            }

            status = session.Read(ref key2, ref input, ref output, Empty.Default, 0);

            if (status == Status.PENDING)
            {
                session.CompletePending(true);
            }
            else
            {
                Assert.IsTrue(status == Status.OK);
            }

            Assert.IsTrue(output.value.vfield1 == value2.vfield1);
            Assert.IsTrue(output.value.vfield2 == value2.vfield2);

            session.Dispose();
        }