author | lbessard |
Tue, 07 Aug 2007 17:37:38 +0200 | |
changeset 63 | 04a02b4b2a57 |
parent 58 | 39cd981ff242 |
child 78 | 049f2e7090a2 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
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 | 6 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
7 |
#based on the plcopen standard. |
|
8 |
# |
|
58 | 9 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
0 | 10 |
# |
11 |
#See COPYING file for copyrights details. |
|
12 |
# |
|
13 |
#This library is free software; you can redistribute it and/or |
|
5 | 14 |
#modify it under the terms of the GNU General Public |
0 | 15 |
#License as published by the Free Software Foundation; either |
16 |
#version 2.1 of the License, or (at your option) any later version. |
|
17 |
# |
|
18 |
#This library is distributed in the hope that it will be useful, |
|
19 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
58 | 21 |
#General Public License for more details. |
0 | 22 |
# |
5 | 23 |
#You should have received a copy of the GNU General Public |
0 | 24 |
#License along with this library; if not, write to the Free Software |
25 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
26 |
||
27 |
||
9 | 28 |
LANGUAGES = ["IL","ST","FBD","LD","SFC"] |
29 |
||
21 | 30 |
|
0 | 31 |
#------------------------------------------------------------------------------- |
32 |
# Function Block Types definitions |
|
33 |
#------------------------------------------------------------------------------- |
|
34 |
||
21 | 35 |
|
0 | 36 |
""" |
37 |
Ordored list of common Function Blocks defined in the IEC 61131-3 |
|
38 |
Each block have this attributes: |
|
39 |
- "name" : The block name |
|
40 |
- "type" : The block type. It can be "function", "functionBlock" or "program" |
|
41 |
- "extensible" : Boolean that define if the block is extensible |
|
42 |
- "inputs" : List of the block inputs |
|
43 |
- "outputs" : List of the block outputs |
|
44 |
- "comment" : Comment that will be displayed in the block popup |
|
45 |
Inputs and outputs are a tuple of characteristics that are in order: |
|
46 |
- The name |
|
47 |
- The data type |
|
48 |
- The default modifier which can be "none", "negated", "rising" or "falling" |
|
49 |
""" |
|
50 |
||
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
51 |
BlockTypes = [{"name" : "Standard function blocks", "list": |
0 | 52 |
[{"name" : "SR", "type" : "functionBlock", "extensible" : False, |
53 |
"inputs" : [("S1","BOOL","none"),("R","BOOL","none")], |
|
54 |
"outputs" : [("Q1","BOOL","none")], |
|
55 |
"comment" : "SR bistable\nThe SR bistable is a latch where the Set dominates."}, |
|
56 |
{"name" : "RS", "type" : "functionBlock", "extensible" : False, |
|
57 |
"inputs" : [("S","BOOL","none"),("R1","BOOL","none")], |
|
58 |
"outputs" : [("Q1","BOOL","none")], |
|
59 |
"comment" : "RS bistable\nThe RS bistable is a latch where the Reset dominates."}, |
|
60 |
{"name" : "SEMA", "type" : "functionBlock", "extensible" : False, |
|
61 |
"inputs" : [("CLAIM","BOOL","none"),("RELEASE","BOOL","none")], |
|
62 |
"outputs" : [("BUSY","BOOL","none")], |
|
63 |
"comment" : "Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources."}, |
|
64 |
{"name" : "R_TRIG", "type" : "functionBlock", "extensible" : False, |
|
65 |
"inputs" : [("CLK","BOOL","none")], |
|
66 |
"outputs" : [("Q","BOOL","none")], |
|
67 |
"comment" : "Rising edge detector\nThe output produces a single pulse when a rising edge is detected."}, |
|
68 |
{"name" : "F_TRIG", "type" : "functionBlock", "extensible" : False, |
|
69 |
"inputs" : [("CLK","BOOL","none")], |
|
70 |
"outputs" : [("Q","BOOL","none")], |
|
71 |
"comment" : "Falling edge detector\nThe output produces a single pulse when a falling edge is detected."}, |
|
72 |
{"name" : "CTU", "type" : "functionBlock", "extensible" : False, |
|
73 |
"inputs" : [("CU","BOOL","rising"),("R","BOOL","none"),("PV","INT","none")], |
|
74 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
75 |
"comment" : "Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value."}, |
|
76 |
{"name" : "CTD", "type" : "functionBlock", "extensible" : False, |
|
77 |
"inputs" : [("CD","BOOL","rising"),("LD","BOOL","none"),("PV","INT","none")], |
|
78 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
79 |
"comment" : "Down-counter\nThe down-counter can be used to signal when a count has reached zero, on counting down from a preset value."}, |
|
80 |
{"name" : "CTUD", "type" : "functionBlock", "extensible" : False, |
|
81 |
"inputs" : [("CU","BOOL","rising"),("CD","BOOL","rising"),("R","BOOL","none"),("LD","BOOL","none"),("PV","INT","none")], |
|
82 |
"outputs" : [("QU","BOOL","none"),("QD","BOOL","none"),("CV","INT","none")], |
|
83 |
"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."}, |
|
84 |
{"name" : "TP", "type" : "functionBlock", "extensible" : False, |
|
85 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
86 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
87 |
"comment" : "Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration."}, |
|
88 |
{"name" : "TOF", "type" : "functionBlock", "extensible" : False, |
|
89 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
90 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
91 |
"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."}, |
|
92 |
{"name" : "TON", "type" : "functionBlock", "extensible" : False, |
|
93 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
94 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
95 |
"comment" : "Off-delay timer\nThe off-delay timer can be used to delay setting an output false, for fixed period after input goes false."}, |
|
96 |
{"name" : "RTC", "type" : "functionBlock", "extensible" : False, |
|
97 |
"inputs" : [("EN","BOOL","none"),("PDT","DATE_AND_TIME","none")], |
|
98 |
"outputs" : [("Q","BOOL","none"),("CDT","DATE_AND_TIME","none")], |
|
99 |
"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."}, |
|
100 |
{"name" : "INTEGRAL", "type" : "functionBlock", "extensible" : False, |
|
101 |
"inputs" : [("RUN","BOOL","none"),("R1","BOOL","none"),("XIN","REAL","none"),("X0","REAL","none"),("CYCLE","TIME","none")], |
|
102 |
"outputs" : [("Q","BOOL","none"),("XOUT","REAL","none")], |
|
103 |
"comment" : "Integral\nThe integral function block integrates the value of input XIN over time."}, |
|
104 |
{"name" : "DERIVATIVE", "type" : "functionBlock", "extensible" : False, |
|
105 |
"inputs" : [("RUN","BOOL","none"),("XIN","REAL","none"),("CYCLE","TIME","none")], |
|
106 |
"outputs" : [("XOUT","REAL","none")], |
|
107 |
"comment" : "Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN."}, |
|
108 |
{"name" : "PID", "type" : "functionBlock", "extensible" : False, |
|
109 |
"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")], |
|
110 |
"outputs" : [("XOUT","REAL","none")], |
|
111 |
"comment" : "PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control."}, |
|
112 |
{"name" : "RAMP", "type" : "functionBlock", "extensible" : False, |
|
113 |
"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")], |
|
114 |
"outputs" : [("RAMP","BOOL","none"),("XOUT","REAL","none")], |
|
115 |
"comment" : "Ramp\nThe RAMP function block is modelled on example given in the standard but with the addition of a 'Holdback' feature."}, |
|
116 |
{"name" : "HYSTERESIS", "type" : "functionBlock", "extensible" : False, |
|
117 |
"inputs" : [("XIN1","REAL","none"),("XIN2","REAL","none"),("EPS","REAL","none")], |
|
118 |
"outputs" : [("Q","BOOL","none")], |
|
119 |
"comment" : "Hysteresis\nThe hysteresis function block provides a hysteresis boolean output driven by the difference of two floating point (REAL) inputs XIN1 and XIN2."}, |
|
120 |
{"name" : "RATIO_MONITOR", "type" : "functionBlock", "extensible" : False, |
|
121 |
"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")], |
|
122 |
"outputs" : [("ALARM","BOOL","none"),("TOTAL_ERR","BOOL","none")], |
|
57 | 123 |
"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."} |
124 |
]}, |
|
125 |
{"name" : "SVGUI function blocks", "list": |
|
126 |
[{"name" : "Container", "type" : "functionBlock", "extensible" : False, |
|
127 |
"inputs" : [("X","FLOAT","none"),("SetX","BOOL","none"),("Y","FLOAT","none"),("SetY","BOOL","none"),("Angle","FLOAT","none"),("SetAngle","BOOL","none")], |
|
128 |
"outputs" : [("X","FLOAT","none"),("X Changed","BOOL","none"),("Y","FLOAT","none"),("Y Changed","BOOL","none"),("Angle","FLOAT","none"),("Angle Changed","BOOL","none")], |
|
129 |
"comment" : "SVGUI Container"}, |
|
130 |
{"name" : "Button", "type" : "functionBlock", "extensible" : False, |
|
131 |
"inputs" : [("Show","BOOL","none"),("Toggle","BOOL","none")], |
|
132 |
"outputs" : [("Visible","BOOL","none"),("State","BOOL","none")], |
|
133 |
"comment" : "SVGUI Button"}, |
|
134 |
{"name" : "TextCtrl", "type" : "functionBlock", "extensible" : False, |
|
135 |
"inputs" : [("Text","STRING","none"),("Set Text","BOOL","none")], |
|
136 |
"outputs" : [("Text","STRING","none"),("Text Changed","BOOL","none")], |
|
137 |
"comment" : "SVGUI Text Control"}, |
|
138 |
{"name" : "ScrollBar", "type" : "functionBlock", "extensible" : False, |
|
139 |
"inputs" : [("Position","UINT","none"),("Set Position","BOOL","none")], |
|
140 |
"outputs" : [("Position","UINT","none"),("Position Changed","BOOL","none")], |
|
141 |
"comment" : "SVGUI ScrollBar"}, |
|
142 |
{"name" : "NoteBook", "type" : "functionBlock", "extensible" : False, |
|
143 |
"inputs" : [("Selected","UINT","none"),("Set Selected","BOOL","none")], |
|
144 |
"outputs" : [("Selected","UINT","none"),("Selected Changed","BOOL","none")], |
|
145 |
"comment" : "SVGUI Notebook"}, |
|
146 |
{"name" : "RotatingCtrl", "type" : "functionBlock", "extensible" : False, |
|
147 |
"inputs" : [("Angle","FLOAT","none"),("Set Angle","BOOL","none")], |
|
148 |
"outputs" : [("Angle","FLOAT","none"),("Angle changed","BOOL","none")], |
|
149 |
"comment" : "SVGUI Rotating Control"}, |
|
0 | 150 |
]} |
151 |
] |
|
152 |
||
153 |
""" |
|
154 |
Function that returns the block definition associated to the block type given |
|
155 |
""" |
|
156 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
157 |
def GetBlockType(type, inputs = None): |
0 | 158 |
for category in BlockTypes: |
159 |
for blocktype in category["list"]: |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
160 |
if inputs: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
161 |
block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
162 |
same_inputs = inputs == block_inputs |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
163 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
164 |
same_inputs = True |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
165 |
if blocktype["name"] == type and same_inputs: |
0 | 166 |
return blocktype |
167 |
return None |
|
168 |
||
169 |
||
170 |
#------------------------------------------------------------------------------- |
|
171 |
# Data Types definitions |
|
172 |
#------------------------------------------------------------------------------- |
|
173 |
||
174 |
""" |
|
175 |
Ordored list of common data types defined in the IEC 61131-3 |
|
176 |
Each type is associated to his direct parent type. It defines then a hierarchy |
|
177 |
between type that permits to make a comparison of two types |
|
178 |
""" |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
179 |
TypeHierarchy_list = [ |
22 | 180 |
("ANY", None), |
181 |
("ANY_DERIVED", "ANY"), |
|
182 |
("ANY_ELEMENTARY", "ANY"), |
|
183 |
("ANY_MAGNITUDE", "ANY_ELEMENTARY"), |
|
184 |
("ANY_BIT", "ANY_ELEMENTARY"), |
|
25 | 185 |
("ANY_NBIT", "ANY_BIT"), |
22 | 186 |
("ANY_STRING", "ANY_ELEMENTARY"), |
187 |
("ANY_DATE", "ANY_ELEMENTARY"), |
|
188 |
("ANY_NUM", "ANY_MAGNITUDE"), |
|
189 |
("ANY_REAL", "ANY_NUM"), |
|
190 |
("ANY_INT", "ANY_NUM"), |
|
25 | 191 |
("ANY_SINT", "ANY_INT"), |
192 |
("ANY_UINT", "ANY_INT"), |
|
27 | 193 |
("BOOL", "ANY_BIT"), |
25 | 194 |
("SINT", "ANY_SINT"), |
195 |
("INT", "ANY_SINT"), |
|
196 |
("DINT", "ANY_SINT"), |
|
197 |
("LINT", "ANY_SINT"), |
|
198 |
("USINT", "ANY_UINT"), |
|
199 |
("UINT", "ANY_UINT"), |
|
200 |
("UDINT", "ANY_UINT"), |
|
201 |
("ULINT", "ANY_UINT"), |
|
27 | 202 |
("REAL", "ANY_REAL"), |
203 |
("LREAL", "ANY_REAL"), |
|
22 | 204 |
("TIME", "ANY_MAGNITUDE"), |
27 | 205 |
("DATE", "ANY_DATE"), |
206 |
("TOD", "ANY_DATE"), |
|
207 |
("DT", "ANY_DATE"), |
|
208 |
("STRING", "ANY_STRING"), |
|
25 | 209 |
("BYTE", "ANY_NBIT"), |
210 |
("WORD", "ANY_NBIT"), |
|
211 |
("DWORD", "ANY_NBIT"), |
|
27 | 212 |
("LWORD", "ANY_NBIT") |
213 |
#("WSTRING", "ANY_STRING") # TODO |
|
214 |
] |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
215 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
216 |
TypeHierarchy = dict(TypeHierarchy_list) |
0 | 217 |
|
218 |
""" |
|
15 | 219 |
returns true if the given data type is the same that "reference" meta-type or one of its types. |
0 | 220 |
""" |
221 |
||
222 |
def IsOfType(test, reference): |
|
223 |
while test != None: |
|
224 |
if test == reference: |
|
225 |
return True |
|
226 |
test = TypeHierarchy[test] |
|
227 |
return False |
|
228 |
||
15 | 229 |
""" |
230 |
returns list of all types that correspont to the ANY* meta type |
|
231 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
232 |
def GetSubTypes(reference): |
22 | 233 |
return [ typename for typename, parenttype in TypeHierarchy_list if typename[:3] != "ANY" and IsOfType(typename, reference)] |
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
234 |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
235 |
|
21 | 236 |
#------------------------------------------------------------------------------- |
237 |
# Test identifier |
|
238 |
#------------------------------------------------------------------------------- |
|
239 |
||
240 |
||
241 |
||
242 |
# Test if identifier is valid |
|
243 |
def TestIdentifier(identifier): |
|
244 |
if identifier[0].isdigit(): |
|
245 |
return False |
|
246 |
words = identifier.split('_') |
|
247 |
for i, word in enumerate(words): |
|
248 |
if len(word) == 0 and i != 0: |
|
249 |
return False |
|
250 |
if len(word) != 0 and not word.isalnum(): |
|
251 |
return False |
|
252 |
return True |
|
253 |
||
254 |
||
255 |
#------------------------------------------------------------------------------- |
|
256 |
# Languages Keywords |
|
257 |
#------------------------------------------------------------------------------- |
|
258 |
||
259 |
||
260 |
# Keywords for Pou Declaration |
|
261 |
POU_KEYWORDS = ["FUNCTION", "END_FUNCTION", "FUNCTION_BLOCK", "END_FUNCTION_BLOCK", |
|
262 |
"PROGRAM", "END_PROGRAM", "EN", "ENO", "F_EDGE", "R_EDGE"] |
|
263 |
for category in BlockTypes: |
|
264 |
for block in category["list"]: |
|
265 |
if block["name"] not in POU_KEYWORDS: |
|
266 |
POU_KEYWORDS.append(block["name"]) |
|
267 |
||
268 |
||
269 |
# Keywords for Type Declaration |
|
270 |
TYPE_KEYWORDS = ["TYPE", "END_TYPE", "STRUCT", "END_STRUCT", "ARRAY", "OF", "T", |
|
271 |
"D", "TIME_OF_DAY", "DATE_AND_TIME"] |
|
272 |
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS]) |
|
273 |
||
274 |
||
275 |
# Keywords for Variable Declaration |
|
276 |
VAR_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", |
|
277 |
"VAR_EXTERNAL", "END_VAR", "AT", "CONSTANT", "RETAIN", "NON_RETAIN"] |
|
278 |
||
279 |
||
280 |
# Keywords for Configuration Declaration |
|
281 |
CONFIG_KEYWORDS = ["CONFIGURATION", "END_CONFIGURATION", "RESOURCE", "ON", "END_RESOURCE", |
|
282 |
"PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK", "VAR_ACCESS", "VAR_CONFIG", |
|
283 |
"VAR_GLOBAL", "END_VAR"] |
|
284 |
||
285 |
||
286 |
# Keywords for Structured Function Chart |
|
287 |
SFC_KEYWORDS = ["ACTION", "END_ACTION", "INITIAL_STEP", "STEP", "END_STEP", "TRANSITION", |
|
288 |
"FROM", "TO", "END_TRANSITION"] |
|
289 |
||
290 |
||
291 |
# Keywords for Instruction List |
|
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
40
diff
changeset
|
292 |
IL_KEYWORDS = ["TRUE", "FALSE", "LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN", |
21 | 293 |
"XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE", |
294 |
"LE", "LT", "JMP", "JMPC", "JMPNC", "CAL", "CALC", "CALNC", "RET", "RETC", "RETNC"] |
|
295 |
||
296 |
||
297 |
# Keywords for Instruction List and Structured Text |
|
47
2b2f8d88e6d3
Interface changed to show pou interface at the bottom of the window
lbessard
parents:
40
diff
changeset
|
298 |
ST_KEYWORDS = ["TRUE", "FALSE", "IF", "THEN", "ELSIF", "ELSE", "END_IF", "CASE", "OF", "END_CASE", |
21 | 299 |
"FOR", "TO", "BY", "DO", "END_FOR", "WHILE", "DO", "END_WHILE", "REPEAT", "UNTIL", |
300 |
"END_REPEAT", "EXIT", "RETURN", "NOT", "MOD", "AND", "XOR", "OR"] |
|
301 |
||
302 |
||
303 |
# All the keywords of IEC |
|
304 |
IEC_KEYWORDS = ["E", "TRUE", "FALSE"] |
|
305 |
IEC_KEYWORDS.extend([keyword for keyword in POU_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
306 |
IEC_KEYWORDS.extend([keyword for keyword in TYPE_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
307 |
IEC_KEYWORDS.extend([keyword for keyword in VAR_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
308 |
IEC_KEYWORDS.extend([keyword for keyword in CONFIG_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
309 |
IEC_KEYWORDS.extend([keyword for keyword in SFC_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
310 |
IEC_KEYWORDS.extend([keyword for keyword in IL_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
311 |
IEC_KEYWORDS.extend([keyword for keyword in ST_KEYWORDS if keyword not in IEC_KEYWORDS]) |
|
312 |
||
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
313 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
314 |
|
15 | 315 |
""" |
316 |
take a .csv file and translate it it a "csv_table" |
|
317 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
318 |
def csv_file_to_table(file): |
22 | 319 |
return [ map(string.strip,line.split(';')) for line in file.xreadlines()] |
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
320 |
|
15 | 321 |
""" |
322 |
seek into the csv table to a section ( section_name match 1st field ) |
|
323 |
return the matching row without first field |
|
324 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
325 |
def find_section(section_name, table): |
22 | 326 |
fields = [None] |
327 |
while(fields[0] != section_name): |
|
328 |
fields = table.pop(0) |
|
329 |
return fields[1:] |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
330 |
|
15 | 331 |
""" |
332 |
extract the standard functions standard parameter names and types... |
|
333 |
return a { ParameterName: Type, ...} |
|
334 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
335 |
def get_standard_funtions_input_variables(table): |
22 | 336 |
variables = find_section("Standard_functions_variables_types", table) |
337 |
standard_funtions_input_variables = {} |
|
338 |
fields = [True,True] |
|
339 |
while(fields[1]): |
|
340 |
fields = table.pop(0) |
|
341 |
variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!='']) |
|
342 |
standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type'] |
|
343 |
return standard_funtions_input_variables |
|
344 |
||
15 | 345 |
""" |
346 |
translate .csv file input declaration into PLCOpenEditor interessting values |
|
347 |
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...} |
|
348 |
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] |
|
349 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
350 |
def csv_input_translate(str_decl, variables, base): |
22 | 351 |
decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',') |
352 |
params = [] |
|
353 |
||
354 |
len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables]) |
|
355 |
||
356 |
for param_type in decl: |
|
357 |
if param_type in variables.keys(): |
|
358 |
param_name = param_type |
|
359 |
param_type = variables[param_type] |
|
360 |
elif len_of_not_predifined_variable > 1: |
|
361 |
param_name = "IN%d"%base |
|
362 |
base += 1 |
|
363 |
else: |
|
364 |
param_name = "IN" |
|
365 |
params.append((param_name, param_type, "none")) |
|
366 |
return params |
|
367 |
||
368 |
||
25 | 369 |
ANY_TO_ANY_LIST=[ |
370 |
# simple type conv are let as C cast |
|
22 | 371 |
(("ANY_NUM","ANY_BIT"),("ANY_NUM","ANY_BIT"), "(%(return_type)s)%(IN_value)s"), |
25 | 372 |
# TO_TIME |
373 |
(("ANY_INT","ANY_BIT"),("ANY_DATE","TIME"), "(%(return_type)s)__int_to_time(%(IN_value)s)"), |
|
374 |
(("ANY_REAL",),("ANY_DATE","TIME"), "(%(return_type)s)__real_to_time(%(IN_value)s)"), |
|
375 |
(("ANY_STRING",), ("ANY_DATE","TIME"), "(%(return_type)s)__string_to_time(%(IN_value)s)"), |
|
376 |
# FROM_TIME |
|
377 |
(("ANY_DATE","TIME"), ("ANY_REAL",), "(%(return_type)s)__time_to_real(%(IN_value)s)"), |
|
40 | 378 |
(("ANY_DATE","TIME"), ("ANY_INT","ANY_NBIT"), "(%(return_type)s)__time_to_int(%(IN_value)s)"), |
25 | 379 |
(("TIME",), ("ANY_STRING",), "(%(return_type)s)__time_to_string(%(IN_value)s)"), |
380 |
(("DATE",), ("ANY_STRING",), "(%(return_type)s)__date_to_string(%(IN_value)s)"), |
|
381 |
(("TOD",), ("ANY_STRING",), "(%(return_type)s)__tod_to_string(%(IN_value)s)"), |
|
382 |
(("DT",), ("ANY_STRING",), "(%(return_type)s)__dt_to_string(%(IN_value)s)"), |
|
383 |
# TO_STRING |
|
384 |
(("BOOL",), ("ANY_STRING",), "(%(return_type)s)__bool_to_string(%(IN_value)s)"), |
|
385 |
(("ANY_BIT",), ("ANY_STRING",), "(%(return_type)s)__bit_to_string(%(IN_value)s)"), |
|
386 |
(("ANY_REAL",), ("ANY_STRING",), "(%(return_type)s)__real_to_string(%(IN_value)s)"), |
|
387 |
(("ANY_SINT",), ("ANY_STRING",), "(%(return_type)s)__sint_to_string(%(IN_value)s)"), |
|
388 |
(("ANY_UINT",), ("ANY_STRING",), "(%(return_type)s)__uint_to_string(%(IN_value)s)"), |
|
389 |
# FROM_STRING |
|
390 |
(("ANY_STRING",), ("BOOL",), "(%(return_type)s)__string_to_bool(%(IN_value)s)"), |
|
391 |
(("ANY_STRING",), ("ANY_BIT",), "(%(return_type)s)__string_to_bit(%(IN_value)s)"), |
|
392 |
(("ANY_STRING",), ("ANY_SINT",), "(%(return_type)s)__string_to_sint(%(IN_value)s)"), |
|
393 |
(("ANY_STRING",), ("ANY_UINT",), "(%(return_type)s)__string_to_uint(%(IN_value)s)"), |
|
394 |
(("ANY_STRING",), ("ANY_REAL",), "(%(return_type)s)__string_to_real(%(IN_value)s)")] |
|
395 |
||
396 |
||
397 |
BCD_TO_ANY_LIST=[ |
|
398 |
(("BYTE",),("USINT",), "(%(return_type)s)__bcd_to_uint(%(IN_value)s)"), |
|
399 |
(("WORD",),("UINT",), "(%(return_type)s)__bcd_to_uint(%(IN_value)s)"), |
|
400 |
(("DWORD",),("UDINT",), "(%(return_type)s)__bcd_to_uint(%(IN_value)s)"), |
|
401 |
(("LWORD",),("ULINT",), "(%(return_type)s)__bcd_to_uint(%(IN_value)s)")] |
|
402 |
||
403 |
||
404 |
ANY_TO_BCD_LIST=[ |
|
405 |
(("USINT",),("BYTE",), "(%(return_type)s)__uint_to_bcd(%(IN_value)s)"), |
|
406 |
(("UINT",),("WORD",), "(%(return_type)s)__uint_to_bcd(%(IN_value)s)"), |
|
407 |
(("UDINT",),("DWORD",), "(%(return_type)s)__uint_to_bcd(%(IN_value)s)"), |
|
408 |
(("ULINT",),("LWORD",), "(%(return_type)s)__uint_to_bcd(%(IN_value)s)")] |
|
409 |
||
410 |
||
411 |
def ANY_TO_ANY_FORMAT_GEN(any_to_any_list, fdecl): |
|
412 |
||
413 |
for (InTypes, OutTypes, Format) in any_to_any_list: |
|
22 | 414 |
outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), OutTypes)) |
415 |
inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), InTypes)) |
|
416 |
if inps and outs and fdecl["outputs"][0][1] != fdecl["inputs"][0][1]: |
|
417 |
return Format |
|
418 |
||
419 |
return None |
|
18
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
420 |
|
ee18a387e80a
Enhanced standard funcrtion declaration from .csv file.
etisserant
parents:
15
diff
changeset
|
421 |
|
15 | 422 |
""" |
423 |
Returns this kind of declaration for all standard functions |
|
424 |
||
425 |
[{"name" : "Numerical", 'list': [ { |
|
426 |
'baseinputnumber': 1, |
|
427 |
'comment': 'Addition', |
|
428 |
'extensible': True, |
|
429 |
'inputs': [ ('IN1', 'ANY_NUM', 'none'), |
|
430 |
('IN2', 'ANY_NUM', 'none')], |
|
431 |
'name': 'ADD', |
|
432 |
'outputs': [('OUT', 'ANY_NUM', 'none')], |
|
433 |
'type': 'function'}, ...... ] },.....] |
|
434 |
""" |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
435 |
def get_standard_funtions(table): |
22 | 436 |
|
437 |
variables = get_standard_funtions_input_variables(table) |
|
438 |
||
439 |
fonctions = find_section("Standard_functions_type",table) |
|
440 |
||
441 |
Standard_Functions_Decl = [] |
|
442 |
Current_section = None |
|
443 |
||
444 |
translate = { |
|
445 |
"extensible" : lambda x: {"yes":True, "no":False}[x], |
|
446 |
"inputs" : lambda x:csv_input_translate(x,variables,baseinputnumber), |
|
447 |
"outputs":lambda x:[("OUT",x,"none")]} |
|
448 |
||
449 |
for fields in table: |
|
450 |
if fields[1]: |
|
451 |
# If function section name given |
|
452 |
if fields[0]: |
|
453 |
Current_section = {"name" : fields[0], "list" : []} |
|
454 |
Standard_Functions_Decl.append(Current_section) |
|
455 |
Function_decl_list = [] |
|
456 |
if Current_section: |
|
457 |
Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ]) |
|
458 |
baseinputnumber = int(Function_decl.get("baseinputnumber",1)) |
|
459 |
Function_decl["baseinputnumber"] = baseinputnumber |
|
460 |
for param, value in Function_decl.iteritems(): |
|
461 |
if param in translate: |
|
462 |
Function_decl[param] = translate[param](value) |
|
463 |
Function_decl["type"] = "function" |
|
464 |
||
25 | 465 |
if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*') : |
22 | 466 |
input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1]) |
25 | 467 |
output_types = GetSubTypes(Function_decl["outputs"][0][1]) |
22 | 468 |
else: |
469 |
input_ovrloading_types = [None] |
|
470 |
output_types = [None] |
|
471 |
||
472 |
funcdeclname_orig = Function_decl["name"] |
|
473 |
funcdeclname = Function_decl["name"].strip('*_') |
|
474 |
fdc = Function_decl["inputs"][:] |
|
475 |
for intype in input_ovrloading_types: |
|
476 |
if intype != None: |
|
477 |
Function_decl["inputs"] = [] |
|
478 |
for decl_tpl in fdc: |
|
479 |
if IsOfType(intype, decl_tpl[1]): |
|
480 |
Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])] |
|
481 |
else: |
|
482 |
Function_decl["inputs"] += [(decl_tpl)] |
|
483 |
||
484 |
if funcdeclname_orig.startswith('*'): |
|
485 |
funcdeclin = intype + '_' + funcdeclname |
|
486 |
else: |
|
487 |
funcdeclin = funcdeclname |
|
488 |
else: |
|
489 |
funcdeclin = funcdeclname |
|
490 |
||
491 |
for outype in output_types: |
|
492 |
if outype != None: |
|
493 |
decl_tpl = Function_decl["outputs"][0] |
|
494 |
Function_decl["outputs"] = [ (decl_tpl[0] , outype, decl_tpl[2])] |
|
495 |
if funcdeclname_orig.endswith('*'): |
|
496 |
funcdeclout = funcdeclin + '_' + outype |
|
497 |
else: |
|
498 |
funcdeclout = funcdeclin |
|
499 |
else: |
|
500 |
funcdeclout = funcdeclin |
|
501 |
Function_decl["name"] = funcdeclout |
|
502 |
||
503 |
||
504 |
fdecl = Function_decl |
|
505 |
res = eval(Function_decl["python_eval_c_code_format"]) |
|
506 |
||
507 |
if res != None : |
|
508 |
# create the copy of decl dict to be appended to section |
|
509 |
Function_decl_copy = Function_decl.copy() |
|
510 |
# Have to generate type description in comment with freshly redefined types |
|
511 |
Function_decl_copy["comment"] += ( |
|
512 |
"\n (" + |
|
513 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["inputs"]]).strip("[]").replace("'",'') + |
|
514 |
" ) => (" + |
|
515 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in Function_decl["outputs"]]).strip("[]").replace("'",'') + |
|
516 |
" )") |
|
517 |
Current_section["list"].append(Function_decl_copy) |
|
518 |
#pp.pprint(Function_decl_copy) |
|
519 |
else: |
|
520 |
raise "First function must be in a category" |
|
521 |
||
522 |
return Standard_Functions_Decl |
|
14
cd0133ed377b
Standard IEC functions declaration now made from iec_std.csv file for easier maintainance.
etisserant
parents:
9
diff
changeset
|
523 |
|
25 | 524 |
std_decl = get_standard_funtions(csv_file_to_table(open(os.path.join(os.path.split(__file__)[0],"iec_std.csv"))))#, True) |
525 |
||
526 |
BlockTypes.extend(std_decl) |
|
527 |