protected InstructionLDAbstractCounter(IEditorItem parent, NodeInstruction instruction, FieldInstructionType instructionType)
            : base(parent, instructionType)
        {
            if (instruction == null)
            {
                var newInstruction = NodeInstruction.BuildWith(InstructionType);
                // Output signal: Named boolean - the counter done signal
                newInstruction = newInstruction.NodeSignalChildren.Append(
                    NodeSignal.BuildWith(
                        new FieldSignalName(Resources.Strings.LD_Snap_Ctr_DefaultName),
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.BOOL.LOW)
                    );
                newInstruction = newInstruction.NodeSignalChildren.Append( // memory of counter value
                    NodeSignal.BuildWith(
                        new FieldSignalName(Resources.Strings.LD_Snap_Ctr_DefaultName + SEPARATOR + Resources.Strings.LD_Snap_Ctr_CountName),
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.NUMBER.ZERO)
                    );
                newInstruction = newInstruction.NodeSignalChildren.Append( // previous rung-in condition
                    NodeSignal.BuildWith(
                        new FieldSignalName(Resources.Strings.LD_Snap_Ctr_DefaultName + SEPARATOR + Resources.Strings.LD_Snap_Ctr_OneshotStateName),
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.BOOL.LOW)
                    );
                // Input signal: setpoint
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0)));
                // Input signal: reset
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldConstant(FieldDataType.DataTypeEnum.BOOL, false)));
                Instruction = newInstruction;
            }
            else
            {
                if (instruction.InstructionType != InstructionType)
                {
                    throw new InvalidOperationException("Tried to instantiate a Counter but passed a different instruction type.");
                }
                Instruction = instruction;
            }

            // Build the context menu
            if (extensionService != null)
            {
                ContextMenu        = extensionService.SortAndJoin(ldInstructionContextMenu, m_staticMenuItemSeparator, contextMenu);
                ContextMenuEnabled = true;
            }
        }
예제 #2
0
        protected InstructionLDMathBase(IEditorItem parent, FieldInstructionType instructionType, NodeInstruction instruction,
                                        string defaultName, bool factory, string instructionName, string firstSignalName, string secondSignalName)
            : base(parent, instructionType)
        {
            if (factory)
            {
                return;
            }

            this.InstructionName  = instructionName;
            this.FirstSignalName  = firstSignalName;
            this.SecondSignalName = secondSignalName;

            if (instruction == null)
            {
                var newInstruction = NodeInstruction.BuildWith(InstructionType);
                // Output signal: Named number - result
                newInstruction = newInstruction.NodeSignalChildren.Append(
                    NodeSignal.BuildWith(
                        new FieldSignalName(defaultName),
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.NUMBER.ZERO)
                    );
                // Input signal: First
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0)));
                // Input signal: Second
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0)));
                Instruction = newInstruction;
            }
            else
            {
                if (instruction.InstructionType != InstructionType)
                {
                    throw new InvalidOperationException("Tried to instantiate a different instruction type.");
                }
                Instruction = instruction;
            }

            // Build the context menu
            if (extensionService != null)
            {
                ContextMenu        = extensionService.SortAndJoin(ldInstructionContextMenu, m_staticMenuItemSeparator, contextMenu);
                ContextMenuEnabled = true;
            }
        }
예제 #3
0
        private void readTextBox()
        {
            var    tb           = textBox;
            var    selectedText = tb.SelectedText;
            string unselectedText;

            if (selectedText.Length > 0)
            {
                unselectedText = tb.Text.Replace(selectedText, string.Empty);
            }
            else
            {
                unselectedText = tb.Text;
            }

            if (unselectedText.Length > 0)
            {
                // check to see if the value entered is a valid literal
                object parseResult;
                if (SignalIn.CompatibleTypes.TryParse(unselectedText, out parseResult))
                {
                    tb.Text          = parseResult.ToString();
                    m_editedSignalIn = NodeSignalIn.BuildWith(
                        SignalIn.DataType, SignalIn.CompatibleTypes,
                        new FieldConstant(SignalIn.CompatibleTypes.DataType, parseResult));
                }
                else
                {
                    Tuple <string, NodeSignal> closestSignal = findClosestMatchingSignal(unselectedText);
                    if (closestSignal != null)
                    {
                        var signalName = closestSignal.Item1;
                        var nSignal    = closestSignal.Item2;
                        tb.Text            = signalName;
                        tb.SelectionStart  = tb.Text.ToLower().IndexOf(unselectedText.ToLower()) + unselectedText.Length;
                        tb.SelectionLength = tb.Text.Length - unselectedText.Length;
                        m_editedSignalIn   = NodeSignalIn.BuildWith(
                            SignalIn.DataType, SignalIn.CompatibleTypes,
                            closestSignal.Item2.SignalId);
                    }
                    else
                    {
                        m_editedSignalIn = SignalIn;
                    }
                }
            }
            else
            {
                m_editedSignalIn = SignalIn;
            }
        }
예제 #4
0
        protected InstructionLDStringContains(IEditorItem parent, NodeInstruction instruction)
            : base(parent, m_InstructionType)
        {
            if (instruction == null)
            {
                var newInstruction = NodeInstruction.BuildWith(InstructionType);
                // Output signal: Named boolean - result of the comparison
                newInstruction = newInstruction.NodeSignalChildren.Append(
                    NodeSignal.BuildWith(
                        new FieldSignalName(Resources.Strings.LD_Snap_StringContains_DefaultName),
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.BOOL.LOW)
                    );
                // Input signal: string to search
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.STRING),
                        new FieldConstant(FieldDataType.DataTypeEnum.STRING, string.Empty)));
                // Input signal: string to find
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.STRING),
                        new FieldConstant(FieldDataType.DataTypeEnum.STRING, string.Empty)));
                // Input signal: case sensitive
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldConstant(FieldDataType.DataTypeEnum.BOOL, false)));
                Instruction = newInstruction;
            }
            else
            {
                if (instruction.InstructionType != InstructionType)
                {
                    throw new InvalidOperationException("Tried to instantiate a StringContains but passed a different instruction type.");
                }
                Instruction = instruction;
            }

            // Build the context menu
            if (extensionService != null)
            {
                ContextMenu        = extensionService.SortAndJoin(ldInstructionContextMenu, m_staticMenuItemSeparator, contextMenu);
                ContextMenuEnabled = true;
            }
        }
예제 #5
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)
        }
예제 #6
0
        /// <summary>
        /// Displays the Dialog as modal
        /// </summary>
        /// <returns>Signal ID</returns>
        public NodeSignalIn ShowDialog(INodeWrapper requester, NodeSignalIn originalSignalIn)
        {
            m_dataTypefilter = originalSignalIn.CompatibleTypes.DataType;
            OriginalSignalIn = originalSignalIn;
            setDefaultValues();
            NodeItem = requester;
            Window dlg = new SignalChooserDialogView();

            dlg.Owner       = mainWindowExport.Value;
            dlg.DataContext = this;
            dlg.ShowDialog();
            if (SignalSelected)
            {
                return(NodeSignalIn.BuildWith(OriginalSignalIn.DataType, OriginalSignalIn.CompatibleTypes, SignalId));
            }
            else
            {
                return(NodeSignalIn.BuildWith(OriginalSignalIn.DataType, OriginalSignalIn.CompatibleTypes, Literal));
            }
        }
예제 #7
0
 protected InstructionLDAbstractContact(IEditorItem parent, FieldInstructionType instructionType, NodeInstruction instruction)
     : base(parent, instructionType)
 {
     if (instruction == null)
     {
         var newInstruction = NodeInstruction.BuildWith(InstructionType);
         // Input signal: (coil that we're a contact off) default to always false
         newInstruction = newInstruction.NodeSignalInChildren.Append(
             NodeSignalIn.BuildWith(
                 new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                 new FieldConstant(FieldDataType.DataTypeEnum.BOOL, false)));
         Instruction = newInstruction;
     }
     else
     {
         if (instructionType != instruction.InstructionType)
         {
             throw new ArgumentOutOfRangeException("Tried to instantiate a contact of type " +
                                                   instructionType.ToString() + " with an instruction of a different type.");
         }
         Instruction = instruction;
     }
 }
예제 #8
0
        public CompiledProgram Compile(NodeRuntimeApplication rta, SignalTable signalTable)
        {
            if (rta == null)
            {
                throw new ArgumentNullException("rta");
            }
            if (signalTable == null)
            {
                throw new ArgumentNullException("signalTable");
            }
            var runtimeId = Guid.Parse(rta.RuntimeId.ToString());
            var versionId = Guid.Parse(rta.ID.ToString());

            var logic = rta.Logic;
            var compiledInstructions = new List <CompiledInstruction>();

            var discreteInputs = rta.DeviceConfiguration.GetChildrenRecursive()
                                 .Select(x => x.Value)
                                 .OfType <NodeDiscreteInput>();

            foreach (var discreteInput in discreteInputs)
            {
                if (discreteInput.Forced.BoolValue)
                {
                    var address = Int16.Parse(discreteInput.Address.ToString());
                    this.driveSignalWithCoilToConstantValue(
                        address, discreteInput.ForcedValue.BoolValue,
                        signalTable, compiledInstructions);
                }
            }

            var analogInputs = rta.DeviceConfiguration.GetChildrenRecursive()
                               .Select(x => x.Value)
                               .OfType <NodeAnalogInput>();

            foreach (var analogInput in analogInputs)
            {
                if (analogInput.Forced.BoolValue)
                {
                    if (analogInput.ForcedValue.DataType != FieldDataType.DataTypeEnum.NUMBER)
                    {
                        throw new Exception("Data type of forced value must be NUMBER.");
                    }
                    var address = byte.Parse(analogInput.Address.ToString());
                    this.setNumericAddressToValueWithAddInstruction(
                        address, (decimal)(analogInput.ForcedValue.Value),
                        signalTable, compiledInstructions);
                }
            }

            compiledInstructions.AddRange(compilePageCollection(logic, signalTable));

            // discrete output literals
            var discreteOutputs = rta.DeviceConfiguration.GetChildrenRecursive()
                                  .Select(x => x.Value)
                                  .OfType <NodeDiscreteOutput>();

            foreach (var discreteOutput in discreteOutputs)
            {
                var literal = discreteOutput.SignalIn.Literal;
                var address = Int16.Parse(discreteOutput.Address.ToString());
                if (literal != null)
                {
                    // insert a new rung to set the literal value
                    if (literal.DataType != FieldDataType.DataTypeEnum.BOOL)
                    {
                        throw new Exception("Data type must be BOOL.");
                    }
                    var literalValue = (bool)(literal.Value);

                    this.driveSignalWithCoilToConstantValue(
                        address, literalValue,
                        signalTable, compiledInstructions);
                }
            }

            // boolean jumpers (from internal signals to outputs)
            foreach (var booleanJumper in signalTable.BooleanJumpers)
            {
                var compiledContactSignal = new CompiledBooleanSignal(true, booleanJumper.Item1, signalTable.BooleanAddressBits);
                var contact = new CompiledInstruction(
                    0x01, // NO contact
                    3,
                    compiledContactSignal);
                compiledInstructions.Add(contact);

                var compiledDiscreteOutputSignal = new CompiledBooleanSignal(false, booleanJumper.Item2, signalTable.BooleanAddressBits);
                var coil = new CompiledInstruction(
                    0x00, // coil
                    3,
                    compiledDiscreteOutputSignal);
                compiledInstructions.Add(coil);
                compiledInstructions.Add(new CompiledInstruction(0x03, 3)); // series instruction end (end of rung)
            }

            // boolean forces
            foreach (var discreteOutput in discreteOutputs)
            {
                var address = Int16.Parse(discreteOutput.Address.ToString());
                if (discreteOutput.Forced.BoolValue)
                {
                    this.driveSignalWithCoilToConstantValue(
                        address, discreteOutput.ForcedValue.BoolValue,
                        signalTable, compiledInstructions);
                }
            }

            // analog output literals
            var analogOutputs = rta.DeviceConfiguration.GetChildrenRecursive()
                                .Select(x => x.Value)
                                .OfType <NodeAnalogOutput>();

            foreach (var analogOutput in analogOutputs)
            {
                var literal = analogOutput.SignalIn.Literal;
                var address = Byte.Parse(analogOutput.Address.ToString());
                if (literal != null)
                {
                    // insert a new rung to set the literal value
                    if (literal.DataType != FieldDataType.DataTypeEnum.NUMBER)
                    {
                        throw new Exception("Data type of literal must be NUMBER.");
                    }
                    this.setNumericAddressToValueWithAddInstruction(
                        address, (decimal)(literal.Value),
                        signalTable, compiledInstructions);
                }
            }

            // analog jumpers (from internal signals to outputs)
            foreach (var numericJumper in signalTable.NumericJumpers)
            {
                var sourceAddress      = numericJumper.Item1;
                var destinationAddress = numericJumper.Item2;

                var compiledOperand1Signal = new CompiledNumericSignal(true, sourceAddress, signalTable.NumericAddressBits);
                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, destinationAddress, 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)
            }

            // analog forces
            foreach (var analogOutput in analogOutputs)
            {
                var address = Byte.Parse(analogOutput.Address.ToString());
                if (analogOutput.Forced.BoolValue)
                {
                    if (analogOutput.ForcedValue.DataType != FieldDataType.DataTypeEnum.NUMBER)
                    {
                        throw new Exception("Data type of forced value must be NUMBER.");
                    }
                    this.setNumericAddressToValueWithAddInstruction(
                        address, (decimal)(analogOutput.ForcedValue.Value),
                        signalTable, compiledInstructions);
                }
            }

            // end of program
            compiledInstructions.Add(new CompiledInstruction(PROGRAM_END, 8));

            var packedInstructions = bitPackInstructions(compiledInstructions);

            return(new CompiledProgram(
                       runtimeId,
                       versionId,
                       signalTable.BooleanAddressBits,
                       signalTable.NumericAddressBits,
                       packedInstructions));
        }