示例#1
0
        /// <summary>
        /// Create a single component from XML data
        /// </summary>
        /// <param name="node">XML node containing component data</param>
        /// <returns>Created Component</returns>
        private static ComponentBase CreateComponent(XmlNode node)
        {
            ComponentBase component;

            switch (node.LocalName)
            {
                #region Basic
            case "Coil":
                Coil coil = new Coil();
                coil.Mode = node.Attributes["Mode"].Value.ToEnum <Coil.CoilMode>();
                coil.Type = node.Attributes["Type"].Value.ToEnum <Coil.CoilType>();
                component = coil;
                break;

            case "Contact":
                Contact contact = new Contact();
                contact.IsClosed   = node.Attributes["IsClosed"].Value.ToBool();
                contact.IsInverted = node.Attributes["IsInverted"].Value.ToBool();
                contact.Type       = node.Attributes["Type"].Value.ToEnum <Contact.ContactType>();
                component          = contact;
                break;

            case "SC": component = new SC(); break;

            case "OSF": component = new OSF(); break;

            case "OSR": component = new OSR(); break;
                #endregion Basic

                #region Compare Components
            case "EQU": component = new EQU(); break;

            case "GEQ": component = new GEQ(); break;

            case "GRT": component = new GRT(); break;

            case "LEG": component = new LEG(); break;

            case "LES": component = new LES(); break;

            case "NEQ": component = new NEQ(); break;
                #endregion Compare Components

                #region Counter Components
            case "CTC": component = new CTC(); break;

            case "CTD": component = new CTD(); break;

            case "CTU": component = new CTU(); break;

            case "RES": component = new RES(); break;
                #endregion Counter Components

                #region Math Components
            case "ADD": component = new ADD(); break;

            case "DIV": component = new DIV(); break;

            case "MUL": component = new MUL(); break;

            case "SUB": component = new SUB(); break;

            case "MOV": component = new MOV(); break;
                #endregion Math Components

                #region Analog Components
            case "ADC":
                ADC adc = new ADC();
                adc.Destination = node.Attributes["Destination"].Value;
                component       = adc;
                break;

            case "PWM":
                PWM pwm = new PWM();
                pwm.DudyCycle = node.Attributes["DudyCycle"].Value;
                component     = pwm;
                break;
                #endregion Analog Components

                #region Function Components
            case "ELF":
                ELF elf = new ELF();
                elf.Name  = node.Attributes["Name"].Value;
                elf.Code  = node.InnerText;
                component = elf;
                break;
                #endregion Function Components

            default:
                throw new ArgumentException("Unknow Component", "node");
            }

            component.Comment = node.Attributes["Comment"].Value;

            #region Extra Processing based on Components Base Class
            if (component is NameableComponent)
            {
                (component as NameableComponent).Name = node.Attributes["Name"].Value;
            }

            if (component is CompareComponent)
            {
                (component as CompareComponent).VarA = node.Attributes["VarA"].Value;
                (component as CompareComponent).VarB = node.Attributes["VarB"].Value;
            }
            else if (component is CounterComponent)
            {
                (component as CounterComponent).Limit = node.Attributes["Limit"].Value;
            }
            else if (component is MathComponent)
            {
                (component as MathComponent).Destination = node.Attributes["Destination"].Value;
                (component as MathComponent).VarA        = node.Attributes["VarA"].Value;
                (component as MathComponent).VarB        = node.Attributes["VarB"].Value;
            }
            #endregion Extra Processing based on Components Base Class

            return(component);
        }
示例#2
0
        public void Basic()
        {
            #region Startup
            Node PowerRail = new Node();
            PowerRail.LogicLevel = false;

            Coil    coil    = new Coil(PowerRail);
            Contact contact = new Contact();
            contact.LeftLide = PowerRail;
            OSF osf = new OSF();
            osf.LeftLide = PowerRail;
            OSR osr = new OSR();
            osr.LeftLide = PowerRail;
            SC sc = new SC(PowerRail);
            #endregion Startup

            #region Coil
            coil.Execute();
            Assert.IsFalse(coil.InternalState);
            PowerRail.LogicLevel = true;
            Assert.IsFalse(coil.InternalState);
            coil.Execute();
            Assert.IsTrue(coil.InternalState);

            PowerRail.LogicLevel = false;
            Assert.IsTrue(coil.InternalState);
            coil.Execute();
            Assert.IsFalse(coil.InternalState);

            coil.Mode = Coil.CoilMode.Negated;
            Assert.IsFalse(coil.InternalState);
            coil.Execute();
            Assert.IsTrue(coil.InternalState);

            PowerRail.LogicLevel = true;
            coil.Execute();
            Assert.IsFalse(coil.InternalState);

            coil.Mode            = Coil.CoilMode.Set;
            PowerRail.LogicLevel = false;
            coil.Execute();
            coil.Execute();
            Assert.IsFalse(coil.InternalState);

            PowerRail.LogicLevel = true;
            coil.Execute();
            Assert.IsTrue(coil.InternalState);
            PowerRail.LogicLevel = false;
            coil.Execute();
            Assert.IsFalse(coil.InternalState);

            coil.Mode = Coil.CoilMode.Reset;
            coil.Execute();
            Assert.IsFalse(coil.InternalState);
            PowerRail.LogicLevel = true;
            coil.Execute();
            Assert.IsTrue(coil.InternalState);
            PowerRail.LogicLevel = false;
            coil.Execute();
            Assert.IsFalse(coil.InternalState);
            #endregion Coil

            #region Contact
            PowerRail.LogicLevel = true;
            contact.Execute();
            Assert.IsFalse(contact.InternalState);

            contact.IsClosed = true;
            contact.Execute();
            Assert.IsTrue(contact.InternalState);

            PowerRail.LogicLevel = false;
            contact.Execute();
            Assert.IsFalse(contact.InternalState);

            contact.IsInverted = true;
            contact.Execute();
            Assert.IsFalse(contact.InternalState);

            PowerRail.LogicLevel = true;
            contact.Execute();
            Assert.IsFalse(contact.InternalState);

            contact.IsClosed = false;
            contact.Execute();
            Assert.IsTrue(contact.InternalState);
            #endregion Contact

            #region Short Circuit
            PowerRail.LogicLevel = false;
            sc.Execute();
            Assert.IsFalse(sc.InternalState);
            PowerRail.LogicLevel = true;
            sc.Execute();
            Assert.IsTrue(sc.InternalState);
            #endregion Short Circuit

            #region OSF
            PowerRail.LogicLevel = false;
            osf.Execute();
            Assert.IsFalse(osf.InternalState);

            PowerRail.LogicLevel = true;
            osf.Execute();
            Assert.IsFalse(osf.InternalState);

            PowerRail.LogicLevel = false;
            osf.Execute();
            Assert.IsTrue(osf.InternalState);
            osf.Execute();
            Assert.IsFalse(osf.InternalState);
            #endregion OSF

            #region OSR
            PowerRail.LogicLevel = false;
            osr.Execute();
            Assert.IsFalse(osr.InternalState);

            PowerRail.LogicLevel = true;
            osr.Execute();
            Assert.IsTrue(osr.InternalState);

            osr.Execute();
            Assert.IsFalse(osr.InternalState);

            PowerRail.LogicLevel = false;
            osr.Execute();
            Assert.IsFalse(osr.InternalState);
            #endregion OSR
        }