plcopen/structures.py
author etisserant
Wed, 06 Jun 2007 17:54:33 +0200
changeset 15 fc897b7bfa7b
parent 14 cd0133ed377b
child 18 ee18a387e80a
permissions -rw-r--r--
Enhanced standard function decalration fr cvs
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     3
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
     4
import string, os, sys
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
     5
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     6
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     7
#based on the plcopen standard. 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     8
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     9
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    10
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    11
#See COPYING file for copyrights details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    12
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    13
#This library is free software; you can redistribute it and/or
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
diff changeset
    14
#modify it under the terms of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    15
#License as published by the Free Software Foundation; either
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    16
#version 2.1 of the License, or (at your option) any later version.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    17
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    18
#This library is distributed in the hope that it will be useful,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    19
#but WITHOUT ANY WARRANTY; without even the implied warranty of
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    21
#Lesser General Public License for more details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    22
#
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
diff changeset
    23
#You should have received a copy of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    24
#License along with this library; if not, write to the Free Software
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    25
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    26
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    27
9
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
    28
LANGUAGES = ["IL","ST","FBD","LD","SFC"]
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
    29
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    30
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    31
#                        Function Block Types definitions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    32
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    33
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    34
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    35
Ordored list of common Function Blocks defined in the IEC 61131-3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    36
Each block have this attributes:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    37
    - "name" : The block name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    38
    - "type" : The block type. It can be "function", "functionBlock" or "program"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    39
    - "extensible" : Boolean that define if the block is extensible
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    40
    - "inputs" : List of the block inputs
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    41
    - "outputs" : List of the block outputs
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    42
    - "comment" : Comment that will be displayed in the block popup
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    43
Inputs and outputs are a tuple of characteristics that are in order:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    44
    - The name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    45
    - The data type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    46
    - The default modifier which can be "none", "negated", "rising" or "falling"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    47
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    48
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
    49
BlockTypes = [{"name" : "Standard function blocks", "list":
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    50
               [{"name" : "SR", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    51
                    "inputs" : [("S1","BOOL","none"),("R","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    52
                    "outputs" : [("Q1","BOOL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    53
                    "comment" : "SR bistable\nThe SR bistable is a latch where the Set dominates."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    54
                {"name" : "RS", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    55
                    "inputs" : [("S","BOOL","none"),("R1","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    56
                    "outputs" : [("Q1","BOOL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    57
                    "comment" : "RS bistable\nThe RS bistable is a latch where the Reset dominates."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    58
                {"name" : "SEMA", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    59
                    "inputs" : [("CLAIM","BOOL","none"),("RELEASE","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    60
                    "outputs" : [("BUSY","BOOL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    61
                    "comment" : "Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    62
                {"name" : "R_TRIG", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    63
                    "inputs" : [("CLK","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    64
                    "outputs" : [("Q","BOOL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    65
                    "comment" : "Rising edge detector\nThe output produces a single pulse when a rising edge is detected."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    66
                {"name" : "F_TRIG", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    67
                    "inputs" : [("CLK","BOOL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    68
                    "outputs" : [("Q","BOOL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    69
                    "comment" : "Falling edge detector\nThe output produces a single pulse when a falling edge is detected."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    70
                {"name" : "CTU", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    71
                    "inputs" : [("CU","BOOL","rising"),("R","BOOL","none"),("PV","INT","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    72
                    "outputs" : [("Q","BOOL","none"),("CV","INT","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    73
                    "comment" : "Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    74
                {"name" : "CTD", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    75
                    "inputs" : [("CD","BOOL","rising"),("LD","BOOL","none"),("PV","INT","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    76
                    "outputs" : [("Q","BOOL","none"),("CV","INT","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    77
                    "comment" : "Down-counter\nThe down-counter can be used to signal when a count has reached zero, on counting down from a preset value."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    78
                {"name" : "CTUD", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    79
                    "inputs" : [("CU","BOOL","rising"),("CD","BOOL","rising"),("R","BOOL","none"),("LD","BOOL","none"),("PV","INT","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    80
                    "outputs" : [("QU","BOOL","none"),("QD","BOOL","none"),("CV","INT","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    81
                    "comment" : "Up-down counter\nThe up-down counter has two inputs CU and CD. It can be used to both count up on one input ans down on the other."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    82
                {"name" : "TP", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    83
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    84
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    85
                    "comment" : "Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    86
                {"name" : "TOF", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    87
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    88
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    89
                    "comment" : "On-delay timer\nThe on-delay timer can be used to delay setting an output true, for fixed period after an input becomes true."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    90
                {"name" : "TON", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    91
                    "inputs" : [("IN","BOOL","none"),("PT","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    92
                    "outputs" : [("Q","BOOL","none"),("ET","TIME","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    93
                    "comment" : "Off-delay timer\nThe off-delay timer can be used to delay setting an output false, for fixed period after input goes false."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    94
                {"name" : "RTC", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    95
                    "inputs" : [("EN","BOOL","none"),("PDT","DATE_AND_TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    96
                    "outputs" : [("Q","BOOL","none"),("CDT","DATE_AND_TIME","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    97
                    "comment" : "Real time clock\nThe real time clock has many uses including time stamping, setting dates and times of day in batch reports, in alarm messages and so on."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    98
                {"name" : "INTEGRAL", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    99
                    "inputs" : [("RUN","BOOL","none"),("R1","BOOL","none"),("XIN","REAL","none"),("X0","REAL","none"),("CYCLE","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   100
                    "outputs" : [("Q","BOOL","none"),("XOUT","REAL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   101
                    "comment" : "Integral\nThe integral function block integrates the value of input XIN over time."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   102
                {"name" : "DERIVATIVE", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   103
                    "inputs" : [("RUN","BOOL","none"),("XIN","REAL","none"),("CYCLE","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   104
                    "outputs" : [("XOUT","REAL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   105
                    "comment" : "Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   106
                {"name" : "PID", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   107
                    "inputs" : [("AUTO","BOOL","none"),("PV","REAL","none"),("SP","REAL","none"),("X0","REAL","none"),("KP","REAL","none"),("TR","REAL","none"),("TD","REAL","none"),("CYCLE","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   108
                    "outputs" : [("XOUT","REAL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   109
                    "comment" : "PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   110
                {"name" : "RAMP", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   111
                    "inputs" : [("RUN","BOOL","none"),("X0","REAL","none"),("X1","REAL","none"),("TR","TIME","none"),("CYCLE","TIME","none"),("HOLDBACK","BOOL","none"),("ERROR","REAL","none"),("PV","REAL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   112
                    "outputs" : [("RAMP","BOOL","none"),("XOUT","REAL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   113
                    "comment" : "Ramp\nThe RAMP function block is modelled on example given in the standard but with the addition of a 'Holdback' feature."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   114
                {"name" : "HYSTERESIS", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   115
                    "inputs" : [("XIN1","REAL","none"),("XIN2","REAL","none"),("EPS","REAL","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   116
                    "outputs" : [("Q","BOOL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   117
                    "comment" : "Hysteresis\nThe hysteresis function block provides a hysteresis boolean output driven by the difference of two floating point (REAL) inputs XIN1 and XIN2."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   118
                {"name" : "RATIO_MONITOR", "type" : "functionBlock", "extensible" : False, 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   119
                    "inputs" : [("PV1","REAL","none"),("PV2","REAL","none"),("RATIO","REAL","none"),("TIMON","TIME","none"),("TIMOFF","TIME","none"),("TOLERANCE","BOOL","none"),("RESET","BOOL","none"),("CYCLE","TIME","none")], 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   120
                    "outputs" : [("ALARM","BOOL","none"),("TOTAL_ERR","BOOL","none")],
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   121
                    "comment" : "Ratio monitor\nThe ratio_monitor function block checks that one process value PV1 is always a given ratio (defined by input RATIO) of a second process value PV2."},
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   122
               ]}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   123
             ]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   124
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   125
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   126
Function that returns the block definition associated to the block type given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   127
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   128
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   129
def GetBlockType(type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   130
    for category in BlockTypes:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   131
        for blocktype in category["list"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   132
            if blocktype["name"] == type:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   133
                return blocktype
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   134
    return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   135
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   136
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   137
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   138
#                           Data Types definitions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   139
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   140
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   141
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   142
Ordored list of common data types defined in the IEC 61131-3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   143
Each type is associated to his direct parent type. It defines then a hierarchy
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   144
between type that permits to make a comparison of two types
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   145
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   146
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   147
TypeHierarchy = {"ANY" : None,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   148
                 "ANY_DERIVED" : "ANY",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   149
                 "ANY_ELEMENTARY" : "ANY",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   150
                 "ANY_MAGNITUDE": "ANY_ELEMENTARY",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   151
                 "ANY_BIT" : "ANY_ELEMENTARY",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   152
                 "ANY_STRING" : "ANY_ELEMENTARY",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   153
                 "ANY_DATE" : "ANY_ELEMENTARY",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   154
                 "ANY_NUM" : "ANY_MAGNITUDE",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   155
                 "ANY_REAL" : "ANY_NUM",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   156
                 "ANY_INT" : "ANY_NUM",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   157
                 "REAL" : "ANY_REAL",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   158
                 "LREAL" : "ANY_REAL",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   159
                 "SINT" : "ANY_INT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   160
                 "INT" : "ANY_INT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   161
                 "DINT" : "ANY_INT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   162
                 "LINT" : "ANY_INT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   163
                 "USINT" : "ANY_INT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   164
                 "UINT" : "ANY_INT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   165
                 "UDINT" : "ANY_INT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   166
                 "ULINT" : "ANY_INT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   167
                 "TIME" : "ANY_MAGNITUDE",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   168
                 "BOOL" : "ANY_BIT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   169
                 "BYTE" : "ANY_BIT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   170
                 "WORD" : "ANY_BIT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   171
                 "DWORD" : "ANY_BIT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   172
                 "LWORD" : "ANY_BIT",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   173
                 "STRING" : "ANY_STRING",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   174
                 "WSTRING" : "ANY_STRING",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   175
                 "DATE" : "ANY_DATE",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   176
                 "TOD" : "ANY_DATE",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   177
                 "DT" : "ANY_DATE"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   178
}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   179
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   180
"""
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   181
returns true if the given data type is the same that "reference" meta-type or one of its types.
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   182
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   183
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   184
def IsOfType(test, reference):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   185
    while test != None:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   186
        if test == reference:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
            return True
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   188
        test = TypeHierarchy[test]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   189
    return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   190
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   191
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   192
returns list of all types that correspont to the ANY* meta type
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   193
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   194
def GetSubTypes(reference):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   195
    return [ typename for typename in TypeHierarchy.iterkeys() if typename[:3] != "ANY" and IsOfType(typename, reference)]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   196
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   197
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   198
take a .csv file and translate it it a "csv_table"
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   199
"""            
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   200
def csv_file_to_table(file):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   201
	return [ map(string.strip,line.split(';')) for line in file.xreadlines()]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   202
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   203
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   204
seek into the csv table to a section ( section_name match 1st field )
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   205
return the matching row without first field
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   206
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   207
def find_section(section_name, table):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   208
	fields = [None]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   209
	while(fields[0] != section_name):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   210
		fields = table.pop(0)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   211
	return fields[1:]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   212
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   213
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   214
extract the standard functions standard parameter names and types...
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   215
return a { ParameterName: Type, ...}
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   216
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   217
def get_standard_funtions_input_variables(table):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   218
	variables = find_section("Standard_functions_variables_types", table)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   219
	standard_funtions_input_variables = {}
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   220
	fields = [True,True]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   221
	while(fields[1]):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   222
		fields = table.pop(0)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   223
		variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:])])
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   224
		standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type']
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   225
	return standard_funtions_input_variables
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   226
	
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   227
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   228
translate .csv file input declaration into PLCOpenEditor interessting values
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   229
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...}
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   230
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] 
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   231
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   232
def csv_input_translate(str_decl, variables, base):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   233
	decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',')
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   234
	param_types = []
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   235
	param_names = []
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   236
	modifiers = []
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   237
	if len(decl)>1 : suffix = str(base)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   238
	else: suffix = ''
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   239
	
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   240
	for param_type in decl:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   241
		predifined_variable_param_type = variables.get(param_type,None)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   242
		if predifined_variable_param_type :
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   243
			param_types.append(predifined_variable_param_type)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   244
			param_names.append(param_type)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   245
		else:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   246
			param_types.append(param_type)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   247
			param_names.append("IN"+suffix)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   248
			base+=1
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   249
			suffix = str(base)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   250
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   251
	modifiers = ["none"]*len(param_types)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   252
	return zip(param_names,param_types,modifiers)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   253
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   254
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   255
Fillin the PLCOpenEditor standard function dictionnary
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   256
translate input and output declaration to something more pythonesque
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   257
and add interface description to comment
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   258
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   259
def decl_function(dico_from_table, variables):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   260
	Function_decl = { "type" : "function" }
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   261
	for field, val in dico_from_table:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   262
		translate = {
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   263
			"baseinputnumber" : lambda x:int(x),
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   264
			"extensible" : lambda x: {"yes":True, "no":False}[x],
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   265
			"inputs" : lambda x:csv_input_translate(x,variables,Function_decl.get("baseinputnumber",1)),
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   266
			"outputs":lambda x:[("OUT",x,"none")]}
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   267
		Function_decl[field] = translate.get(field,lambda x:x)(val)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   268
	#Function_decl.pop("baseinputnumber")
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   269
	Function_decl.pop("overloaded")
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   270
	return Function_decl
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   271
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   272
"""
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   273
Returns this kind of declaration for all standard functions
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   274
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   275
            [{"name" : "Numerical", 'list': [   {   
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   276
                'baseinputnumber': 1,
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   277
                'comment': 'Addition',
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   278
                'extensible': True,
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   279
                'inputs': [   ('IN1', 'ANY_NUM', 'none'),
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   280
                              ('IN2', 'ANY_NUM', 'none')],
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   281
                'name': 'ADD',
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   282
                'outputs': [('OUT', 'ANY_NUM', 'none')],
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   283
                'type': 'function'}, ...... ] },.....]
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   284
"""
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   285
def get_standard_funtions(table):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   286
	
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   287
	variables = get_standard_funtions_input_variables(table)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   288
	
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   289
	fonctions = find_section("Standard_functions_type",table)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   290
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   291
	Standard_Functions_Decl = []
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   292
	Current_section = None
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   293
	for fields in table:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   294
		if fields[1]:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   295
			# If function section name given
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   296
			if fields[0] :
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   297
				if Current_section: 
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   298
					Standard_Functions_Decl.append(Current_section)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   299
				Current_section = { "name" : fields[0], "list" : [] }
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   300
				Function_decl_list = []
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   301
		
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   302
			dico_from_table = zip(fonctions, fields[1:])
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   303
			Function_decl = decl_function(dico_from_table,variables)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   304
			
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   305
			if Function_decl["name"].startswith("*"):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   306
				input_types = GetSubTypes(Function_decl["inputs"][0][1])
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   307
			else:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   308
				input_types = [None]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   309
			if Function_decl["name"].endswith("**"):
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   310
				output_types = GetSubTypes(Function_decl["outputs"][0][1])
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   311
			else:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   312
				output_types = [None]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   313
				
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   314
			funcdecl = Function_decl["name"]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   315
			for intype in input_types:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   316
				if intype != None:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   317
					decl_tpl = Function_decl["inputs"][0]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   318
					Function_decl["inputs"] = [decl_tpl[:1] + (intype,) + decl_tpl[2:]] + Function_decl["inputs"][1:]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   319
					funcdeclin = funcdecl.replace("*_", intype + '_')
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   320
					Function_decl["name"] = funcdeclin
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   321
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   322
				for outype in output_types:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   323
					if outype != None:
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   324
						decl_tpl = Function_decl["outputs"][0]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   325
						Function_decl["outputs"] = [decl_tpl[:1] + (outype,) + decl_tpl[2:]] + Function_decl["outputs"][1:]
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   326
						funcdeclout = funcdeclin.replace("_**", '_' + outype)
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   327
						Function_decl["name"] = funcdeclout
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   328
					
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   329
					# create the copy of decl dict to be appended to section
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   330
					Function_decl_copy = Function_decl.copy()
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   331
					# Have to generate type description in comment with freshly redefined types
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   332
					Function_decl_copy["comment"] += ("\n (" +
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   333
						str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["inputs"]]).strip("[]").replace("'",'') +
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   334
						" ) => (" +
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   335
						str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["outputs"]]).strip("[]").replace("'",'') +
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   336
						" )")
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   337
					Current_section["list"].append(Function_decl_copy)
14
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   338
	
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   339
	return Standard_Functions_Decl
cd0133ed377b Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents: 9
diff changeset
   340
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   341
#-------------------------------------------------------------------------------
9
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   342
#                             Test identifier
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   343
#-------------------------------------------------------------------------------
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   344
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   345
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   346
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   347
# Test if identifier is valid
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   348
def TestIdentifier(identifier):
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   349
     if identifier[0].isdigit():
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   350
        return False
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   351
     words = identifier.split('_')
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   352
     for i, word in enumerate(words):
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   353
         if len(word) == 0 and i != 0:
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   354
             return False
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   355
         if len(word) != 0 and not word.isalnum():
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   356
             return False
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   357
     return True
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   358
b29105e29081 Adding test on step names in SFC Editor
lbessard
parents: 5
diff changeset
   359
#-------------------------------------------------------------------------------
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   360
#                            Languages Keywords
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   361
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   362
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   363
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   364
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   365
# Keywords for Pou Declaration
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   366
POU_KEYWORDS = ["FUNCTION", "END_FUNCTION", "FUNCTION_BLOCK", "END_FUNCTION_BLOCK",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   367
 "PROGRAM", "END_PROGRAM", "EN", "ENO", "F_EDGE", "R_EDGE"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   368
for category in BlockTypes:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   369
    for block in category["list"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   370
        if block["name"] not in POU_KEYWORDS:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   371
            POU_KEYWORDS.append(block["name"])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   372
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   373
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   374
# Keywords for Type Declaration
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   375
TYPE_KEYWORDS = ["TYPE", "END_TYPE", "STRUCT", "END_STRUCT", "ARRAY", "OF", "T",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   376
 "D", "TIME_OF_DAY", "DATE_AND_TIME"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   377
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   378
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   379
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   380
# Keywords for Variable Declaration
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   381
VAR_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   382
 "VAR_EXTERNAL", "END_VAR", "AT", "CONSTANT", "RETAIN", "NON_RETAIN"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   383
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   384
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   385
# Keywords for Configuration Declaration
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   386
CONFIG_KEYWORDS = ["CONFIGURATION", "END_CONFIGURATION", "RESOURCE", "ON", "END_RESOURCE",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   387
 "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK", "VAR_ACCESS", "VAR_CONFIG", 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   388
 "VAR_GLOBAL", "END_VAR"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   389
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   390
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   391
# Keywords for Structured Function Chart
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   392
SFC_KEYWORDS = ["ACTION", "END_ACTION", "INITIAL_STEP", "STEP", "END_STEP", "TRANSITION",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   393
 "FROM", "TO", "END_TRANSITION"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   394
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   395
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   396
# Keywords for Instruction List
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   397
IL_KEYWORDS = ["LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   398
 "XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   399
 "LE", "LT", "JMP", "JMPC", "JMPNC", "CAL", "CALC", "CALNC", "RET", "RETC", "RETNC"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   400
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   401
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   402
# Keywords for Instruction List and Structured Text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   403
ST_KEYWORDS = ["IF", "THEN", "ELSIF", "ELSE", "END_IF", "CASE", "OF", "END_CASE", 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   404
 "FOR", "TO", "BY", "DO", "END_FOR", "WHILE", "DO", "END_WHILE", "REPEAT", "UNTIL", 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   405
 "END_REPEAT", "EXIT", "RETURN", "NOT", "MOD", "AND", "XOR", "OR"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   406
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   407
 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   408
# All the keywords of IEC
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   409
IEC_KEYWORDS = ["E", "TRUE", "FALSE"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   410
IEC_KEYWORDS.extend([keyword for keyword in POU_KEYWORDS if keyword not in IEC_KEYWORDS])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   411
IEC_KEYWORDS.extend([keyword for keyword in TYPE_KEYWORDS if keyword not in IEC_KEYWORDS])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   412
IEC_KEYWORDS.extend([keyword for keyword in VAR_KEYWORDS if keyword not in IEC_KEYWORDS])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   413
IEC_KEYWORDS.extend([keyword for keyword in CONFIG_KEYWORDS if keyword not in IEC_KEYWORDS])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   414
IEC_KEYWORDS.extend([keyword for keyword in SFC_KEYWORDS if keyword not in IEC_KEYWORDS])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   415
IEC_KEYWORDS.extend([keyword for keyword in IL_KEYWORDS if keyword not in IEC_KEYWORDS])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   416
IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   417
15
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   418
if __name__ == '__main__':
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   419
	import pprint
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   420
	pp = pprint.PrettyPrinter(indent=4)
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   421
	std_decl = get_standard_funtions(csv_file_to_table(open("iec_std.csv")))
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   422
	pp.pprint(std_decl)
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   423
else:
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   424
	# Put standard functions declaration in Bloktypes
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   425
	BlockTypes.extend(get_standard_funtions(csv_file_to_table(open(os.path.join(sys.path[0], "plcopen/iec_std.csv")))))
fc897b7bfa7b Enhanced standard function decalration fr cvs
etisserant
parents: 14
diff changeset
   426