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