예제 #1
0
        private IEnumerable <CompiledInstruction> compileInstructionGroup(
            NodeInstructionGroup instructionGroup,
            SignalTable signalTable)
        {
            if (instructionGroup == null)
            {
                throw new ArgumentNullException("instructionGroup");
            }
            if (signalTable == null)
            {
                throw new ArgumentNullException("signalTable");
            }
            var result = new List <CompiledInstruction>();

            if (instructionGroup.Language.ToString() !=
                SoapBox.Snap.LD.Extensions.Workbench.Documents.PageEditor_.InstructionGroupItems.LD)
            {
                throw new ArgumentOutOfRangeException("Can only compile ladder logic");
            }

            foreach (var instruction in instructionGroup.NodeInstructionChildren)
            {
                result.AddRange(compileInstruction(instruction, signalTable));
            }

            return(result);
        }
예제 #2
0
        private IEnumerable <CompiledInstruction> compilePageCollection(
            NodePageCollection pageCollection,
            SignalTable signalTable)
        {
            if (pageCollection == null)
            {
                throw new ArgumentNullException("pageCollection");
            }
            if (signalTable == null)
            {
                throw new ArgumentNullException("signalTable");
            }
            var result = new List <CompiledInstruction>();

            foreach (var subFolder in pageCollection.NodePageCollectionChildren)
            {
                result.AddRange(compilePageCollection(subFolder, signalTable));
            }

            foreach (var page in pageCollection.NodePageChildren)
            {
                result.AddRange(compilePage(page, signalTable));
            }

            return(result);
        }
예제 #3
0
        private CompiledNumericSignal numericSignal(NodeSignal signal, SignalTable signalTable)
        {
            var signalAddress  = signalTable.GetNumericSignalAddress(signal);
            var compiledSignal = new CompiledNumericSignal(false, signalAddress, signalTable.NumericAddressBits);

            return(compiledSignal);
        }
예제 #4
0
        private CompiledBooleanSignal booleanSignal(NodeSignal signal, SignalTable signalTable)
        {
            var signalAddress  = signalTable.GetBooleanSignalAddress(signal);
            var compiledSignal = new CompiledBooleanSignal(false, signalAddress, signalTable.BooleanAddressBits);

            return(compiledSignal);
        }
예제 #5
0
        private CompiledInstruction compileContact(NodeInstruction instruction, SignalTable signalTable, Byte opCode)
        {
            var contactSignalIn       = instruction.NodeSignalInChildren[0];
            var compiledContactSignal = booleanSignal(contactSignalIn, signalTable);

            return(new CompiledInstruction(
                       opCode,
                       3,
                       compiledContactSignal));
        }
예제 #6
0
        private CompiledInstruction compileEdge(NodeInstruction instruction, SignalTable signalTable, Byte opCode)
        {
            var stateSignal         = instruction.NodeSignalChildren[0];
            var compiledStateSignal = booleanSignal(stateSignal, signalTable);

            return(new CompiledInstruction(
                       opCode,
                       5,
                       compiledStateSignal));
        }
예제 #7
0
        private CompiledInstruction compileCoil(NodeInstruction instruction, SignalTable signalTable)
        {
            var stateSignal         = instruction.NodeSignalChildren[0];
            var compiledStateSignal = booleanSignal(stateSignal, signalTable);

            return(new CompiledInstruction(
                       0x00,
                       3,
                       compiledStateSignal));
        }
예제 #8
0
        private CompiledInstruction compileSetReset(NodeInstruction instruction, SignalTable signalTable)
        {
            var resetSignalIn       = instruction.NodeSignalInChildren[0];
            var compiledResetSignal = booleanSignal(resetSignalIn, signalTable);
            var stateSignal         = instruction.NodeSignalChildren[0];
            var compiledStateSignal = booleanSignal(stateSignal, signalTable);

            return(new CompiledInstruction(
                       0x12,
                       5,
                       compiledResetSignal, compiledStateSignal));
        }
예제 #9
0
        private CompiledInstruction compileComparison(NodeInstruction instruction, SignalTable signalTable, Byte opCode)
        {
            var operand1SignalIn       = instruction.NodeSignalInChildren[0];
            var compiledOperand1Signal = numericSignal(operand1SignalIn, signalTable);
            var operand2SignalIn       = instruction.NodeSignalInChildren[1];
            var compiledOperand2Signal = numericSignal(operand2SignalIn, signalTable);

            return(new CompiledInstruction(
                       opCode,
                       5,
                       compiledOperand1Signal, compiledOperand2Signal));
        }
예제 #10
0
        private CompiledInstruction compileTimer(NodeInstruction instruction, SignalTable signalTable, Byte opCode)
        {
            var setpointSignalIn       = instruction.NodeSignalInChildren[0];
            var compiledSetpointSignal = numericSignal(setpointSignalIn, signalTable);
            var doneSignal             = instruction.NodeSignalChildren[0];
            var compiledDoneSignal     = booleanSignal(doneSignal, signalTable);
            var elapsedSignal          = instruction.NodeSignalChildren[1];
            var compiledElapsedSignal  = numericSignal(elapsedSignal, signalTable);

            return(new CompiledInstruction(
                       opCode,
                       5,
                       compiledSetpointSignal, compiledDoneSignal, compiledElapsedSignal));
        }
예제 #11
0
        private void setNumericAddressToValueWithAddInstruction(
            byte address, decimal value,
            SignalTable signalTable, List <CompiledInstruction> compiledInstructions)
        {
            var compiledOperand1Signal = new CompiledNumericSignal(value);
            var operand2SignalIn       = NodeSignalIn.BuildWith(
                new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0M));
            var compiledOperand2Signal = numericSignal(operand2SignalIn, signalTable);

            var compiledResultSignal = new CompiledNumericSignal(false, address, signalTable.NumericAddressBits);
            var add = new CompiledInstruction(
                0x78, // add
                7,
                compiledOperand1Signal, compiledOperand2Signal, compiledResultSignal);

            compiledInstructions.Add(add);
            compiledInstructions.Add(new CompiledInstruction(0x03, 3)); // series instruction end (end of rung)
        }
예제 #12
0
        public override object Evaluate(ParseTreeNode node)
        {
            string entityName  = node.ChildNodes[1].Token.Text;
            var    portSignals = new SignalTable();

            this.declaredObjects.entityPrototype = new ComponentPrototype(entityName, portSignals);

            var portClauseNode = node.ChildNodes[3].ChildNodes[1].ChildNodes[0];

            foreach (var declarationNode in portClauseNode.ChildNodes[2].ChildNodes)
            {
                if (declarationNode.ChildNodes[0].Term.Name == "object_declaration")
                {
                    var signals = (List <ISignal>)EvaluateGeneral(declarationNode.ChildNodes[0]);
                    foreach (var signal in signals)
                    {
                        portSignals[signal.name] = signal;
                        this.declaredObjects.signalTable[signal.name] = signal;
                    }
                }
                else if (declarationNode.ChildNodes[0].Term.Name == "attribute_specification")
                {
                    var attribute = EvaluateGeneral(declarationNode.ChildNodes[0]);
                    if (attribute != null)
                    {
                        var pair = (KeyValuePair <Tuple <string, string>, object>)attribute;

                        if (pair.Key.Item1 == this.declaredObjects.entityPrototype.name)
                        {
                            this.declaredObjects.entityAttribute[pair.Key.Item2] = pair.Value;
                        }
                        else if (portSignals.ContainsKey(pair.Key.Item1))
                        {
                            portSignals[pair.Key.Item1].attribute[pair.Key.Item2] = pair.Value;
                        }
                    }
                }
            }

            this.declaredObjects.componentDeclarations[entityName] = this.declaredObjects.entityPrototype;
            return(null);
        }
예제 #13
0
        private CompiledInstruction compileCounter(NodeInstruction instruction, SignalTable signalTable, Byte opCode)
        {
            var setpointSignalIn       = instruction.NodeSignalInChildren[0];
            var compiledSetpointSignal = numericSignal(setpointSignalIn, signalTable);
            var resetSignalIn          = instruction.NodeSignalInChildren[1];
            var compiledResetSignal    = booleanSignal(resetSignalIn, signalTable);

            var doneSignal          = instruction.NodeSignalChildren[0];
            var compiledDoneSignal  = booleanSignal(doneSignal, signalTable);
            var countSignal         = instruction.NodeSignalChildren[1];
            var compiledCountSignal = numericSignal(countSignal, signalTable);
            var stateSignal         = instruction.NodeSignalChildren[2];
            var compiledStateSignal = booleanSignal(stateSignal, signalTable);

            return(new CompiledInstruction(
                       opCode,
                       6,
                       compiledSetpointSignal, compiledResetSignal,
                       compiledDoneSignal, compiledCountSignal, compiledStateSignal));
        }
예제 #14
0
        private IEnumerable <CompiledInstruction> compilePage(
            NodePage page,
            SignalTable signalTable)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }
            if (signalTable == null)
            {
                throw new ArgumentNullException("signalTable");
            }
            var result = new List <CompiledInstruction>();

            foreach (var instructionGroup in page.NodeInstructionGroupChildren) // instructionGroup == rung
            {
                result.AddRange(compileInstructionGroup(instructionGroup, signalTable));
            }

            return(result);
        }
예제 #15
0
        private CompiledInstruction compileCntUD(NodeInstruction instruction, SignalTable signalTable)
        {
            var countDownSignalIn       = instruction.NodeSignalInChildren[0];
            var compiledCountDownSignal = booleanSignal(countDownSignalIn, signalTable);
            var resetSignalIn           = instruction.NodeSignalInChildren[1];
            var compiledResetSignal     = booleanSignal(resetSignalIn, signalTable);

            var zeroSignal                   = instruction.NodeSignalChildren[0];
            var compiledZeroSignal           = booleanSignal(zeroSignal, signalTable);
            var countSignal                  = instruction.NodeSignalChildren[1];
            var compiledCountSignal          = numericSignal(countSignal, signalTable);
            var countUpStateSignal           = instruction.NodeSignalChildren[2];
            var compiledCountUpStateSignal   = booleanSignal(countUpStateSignal, signalTable);
            var countDownStateSignal         = instruction.NodeSignalChildren[3];
            var compiledCountDownStateSignal = booleanSignal(countDownStateSignal, signalTable);

            return(new CompiledInstruction(
                       0x7D,
                       7,
                       compiledCountDownSignal, compiledResetSignal,
                       compiledZeroSignal, compiledCountSignal, compiledCountUpStateSignal, compiledCountDownStateSignal));
        }
예제 #16
0
 private CompiledBooleanSignal booleanSignal(NodeSignalIn signalIn, SignalTable signalTable)
 {
     if (signalIn.SignalId != null)
     {
         var signalAddress  = signalTable.GetBooleanSignalAddress(signalIn);
         var compiledSignal = new CompiledBooleanSignal(true, signalAddress, signalTable.BooleanAddressBits);
         return(compiledSignal);
     }
     else if (signalIn.Literal != null)
     {
         var literal = signalIn.Literal;
         if (literal.DataType != FieldDataType.DataTypeEnum.BOOL)
         {
             throw new Exception("Signal should be a BOOL.");
         }
         var literalValue = (bool)(literal.Value);
         return(new CompiledBooleanSignal(literalValue));
     }
     else
     {
         throw new Exception("Expecting one of SignalIn.SignalId or SignalIn.Literal to be non-null");
     }
 }
예제 #17
0
 private CompiledNumericSignal numericSignal(NodeSignalIn signalIn, SignalTable signalTable)
 {
     if (signalIn.SignalId != null)
     {
         var signalAddress  = signalTable.GetNumericSignalAddress(signalIn);
         var compiledSignal = new CompiledNumericSignal(true, signalAddress, signalTable.NumericAddressBits);
         return(compiledSignal);
     }
     else if (signalIn.Literal != null)
     {
         var literal = signalIn.Literal;
         if (literal.DataType != FieldDataType.DataTypeEnum.NUMBER)
         {
             throw new Exception("Signal should be a NUMBER.");
         }
         var literalValue = Convert.ToDecimal(literal.Value);
         return(new CompiledNumericSignal(literalValue));
     }
     else
     {
         throw new Exception("Expecting one of SignalIn.SignalId or SignalIn.Literal to be non-null");
     }
 }
예제 #18
0
        private void driveSignalWithCoilToConstantValue(
            Int16 address, bool value,
            SignalTable signalTable, List <CompiledInstruction> compiledInstructions)
        {
            if (!value) // if value is TRUE, only need the coil, otherwise need a NO contact
            {
                var compiledContactSignal = new CompiledBooleanSignal(false);
                var contact = new CompiledInstruction(
                    0x01, // NO contact
                    3,
                    compiledContactSignal);
                compiledInstructions.Add(contact);
            }

            var compiledDiscreteInputSignal = new CompiledBooleanSignal(false, address, signalTable.BooleanAddressBits);
            var coil = new CompiledInstruction(
                0x00, // coil
                3,
                compiledDiscreteInputSignal);

            compiledInstructions.Add(coil);
            compiledInstructions.Add(new CompiledInstruction(0x03, 3)); // series instruction end (end of rung)
        }
        public ArduinoRuntimeProxy(
            IRuntimeType runtimeType,
            string comPort,
            Lazy <IRuntimeService> runtimeService,
            IMessagingService messagingService)
        {
            if (runtimeType == null)
            {
                throw new ArgumentNullException("runtimeType");
            }
            if (runtimeService == null)
            {
                throw new ArgumentNullException("runtimeService");
            }
            if (messagingService == null)
            {
                throw new ArgumentNullException("messagingService");
            }
            this.Type                     = runtimeType;
            this.m_runtimeService         = runtimeService;
            this.m_arduinoRuntimeProtocol = new ArduinoRuntimeProtocol(comPort);
            this.messagingService         = messagingService;

            this.m_runtimeService.Value.SignalChanged += (s) =>
            {
                Console.WriteLine("Signal {0} changed. Forced: {1}, Forced Value: {2}",
                                  s.SignalName.ToString(),
                                  s.Forced.BoolValue,
                                  s.ForcedValue.Value.ToString());
            };

            this.m_arduinoRuntimeProtocol.Connect();
            this.m_information = this.m_arduinoRuntimeProtocol.GetInformation();
            this.m_arduinoRuntimeProtocol.Disconnect();
            this.m_signalTable = new SignalTable(this.m_information);
            this.m_compiler    = new ArduinoLadderCompiler(this.m_information);
        }
예제 #20
0
        public override object Evaluate(ParseTreeNode node)
        {
            string entityName  = node.ChildNodes[1].Token.Text;
            var    portSignals = new SignalTable();

            var portClauseNode = node.ChildNodes[4].ChildNodes[0];

            foreach (var declarationNode in portClauseNode.ChildNodes[2].ChildNodes)
            {
                if (declarationNode.ChildNodes[0].Term.Name == "object_declaration")
                {
                    var signals = (List <ISignal>)EvaluateGeneral(declarationNode.ChildNodes[0]);
                    foreach (var signal in signals)
                    {
                        portSignals[signal.name] = signal;
                    }
                }
            }

            var entity = new ComponentPrototype(entityName, portSignals);

            this.declaredObjects.componentDeclarations[entityName] = entity;
            return(entity);
        }
예제 #21
0
 private CompiledInstruction compileCntDN(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileCounter(instruction, signalTable, 0x2F));
 }
예제 #22
0
        private IEnumerable <CompiledInstruction> compileParallel(NodeInstruction parallelInstruction, SignalTable signalTable)
        {
            var result = new List <CompiledInstruction>();

            result.Add(new CompiledInstruction(0x15, 5)); // Parallel Start
            foreach (var instruction in parallelInstruction.NodeInstructionChildren)
            {
                result.AddRange(compileInstruction(instruction, signalTable));
            }
            result.Add(new CompiledInstruction(0x16, 5)); // Parallel End
            return(result);
        }
예제 #23
0
 private CompiledInstruction compileTmrOFF(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileTimer(instruction, signalTable, 0x14));
 }
예제 #24
0
 private CompiledInstruction compileLessThanOrEqual(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileComparison(instruction, signalTable, 0x1C));
 }
예제 #25
0
 private CompiledInstruction compileGreaterThan(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileComparison(instruction, signalTable, 0x19));
 }
예제 #26
0
 private CompiledInstruction compileChooseNumber(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileMath(instruction, signalTable, 0x7C));
 }
예제 #27
0
 private CompiledInstruction compileDivide(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileMath(instruction, signalTable, 0x7B));
 }
예제 #28
0
 private CompiledInstruction compileMultiply(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileMath(instruction, signalTable, 0x7A));
 }
예제 #29
0
 private CompiledInstruction compileSubtract(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileMath(instruction, signalTable, 0x79));
 }
예제 #30
0
 private CompiledInstruction compileNotEqual(NodeInstruction instruction, SignalTable signalTable)
 {
     return(compileComparison(instruction, signalTable, 0x1D));
 }