author | Laurent Bessard |
Mon, 10 Jun 2013 01:15:39 +0200 | |
changeset 1237 | 0c8b8ef9559b |
parent 1149 | 67df52302ea6 |
child 1239 | d1f6ea56555d |
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 |
814 | 26 |
|
27 |
LANGUAGES = ["IL","ST","FBD","LD","SFC"] |
|
28 |
||
29 |
LOCATIONDATATYPES = {"X" : ["BOOL"], |
|
30 |
"B" : ["SINT", "USINT", "BYTE", "STRING"], |
|
31 |
"W" : ["INT", "UINT", "WORD", "WSTRING"], |
|
32 |
"D" : ["DINT", "UDINT", "REAL", "DWORD"], |
|
33 |
"L" : ["LINT", "ULINT", "LREAL", "LWORD"]} |
|
34 |
||
35 |
_ = lambda x:x |
|
36 |
||
37 |
# Helper for emulate join on element list |
|
38 |
def JoinList(separator, mylist): |
|
39 |
if len(mylist) > 0 : |
|
40 |
return reduce(lambda x, y: x + separator + y, mylist) |
|
41 |
else : |
|
42 |
return mylist |
|
43 |
||
44 |
def generate_block(generator, block, block_infos, body, link, order=False, to_inout=False): |
|
45 |
body_type = body.getcontent()["name"] |
|
46 |
name = block.getinstanceName() |
|
47 |
type = block.gettypeName() |
|
48 |
executionOrderId = block.getexecutionOrderId() |
|
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
49 |
input_variables = block.inputVariables.getvariable() |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
50 |
output_variables = block.outputVariables.getvariable() |
814 | 51 |
inout_variables = {} |
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
52 |
for input_variable in input_variables: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
53 |
for output_variable in output_variables: |
814 | 54 |
if input_variable.getformalParameter() == output_variable.getformalParameter(): |
55 |
inout_variables[input_variable.getformalParameter()] = "" |
|
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
56 |
input_names = [input[0] for input in block_infos["inputs"]] |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
57 |
output_names = [output[0] for output in block_infos["outputs"]] |
814 | 58 |
if block_infos["type"] == "function": |
59 |
if not generator.ComputedBlocks.get(block, False) and not order: |
|
60 |
generator.ComputedBlocks[block] = True |
|
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
61 |
connected_vars = [] |
1149
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
62 |
if not block_infos["extensible"]: |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
63 |
input_connected = dict([("EN", None)] + |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
64 |
[(input_name, None) for input_name in input_names]) |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
65 |
for variable in input_variables: |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
66 |
parameter = variable.getformalParameter() |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
67 |
if input_connected.has_key(parameter): |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
68 |
input_connected[parameter] = variable |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
69 |
if input_connected["EN"] is None: |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
70 |
input_connected.pop("EN") |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
71 |
input_parameters = input_names |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
72 |
else: |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
73 |
input_parameters = ["EN"] + input_names |
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
74 |
else: |
1149
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
75 |
input_connected = dict([(variable.getformalParameter(), variable) |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
76 |
for variable in input_variables]) |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
77 |
input_parameters = [variable.getformalParameter() |
67df52302ea6
Fixed bug in ST code generator with extensible standard functions
Laurent Bessard
parents:
1143
diff
changeset
|
78 |
for variable in input_variables] |
814 | 79 |
one_input_connected = False |
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
80 |
all_input_connected = True |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
81 |
for i, parameter in enumerate(input_parameters): |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
82 |
variable = input_connected.get(parameter) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
83 |
if variable is not None: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
84 |
input_info = (generator.TagName, "block", block.getlocalId(), "input", i) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
85 |
connections = variable.connectionPointIn.getconnections() |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
86 |
if connections is not None: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
87 |
if parameter != "EN": |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
88 |
one_input_connected = True |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
89 |
if inout_variables.has_key(parameter): |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
90 |
value = generator.ComputeExpression(body, connections, executionOrderId > 0, True) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
91 |
inout_variables[parameter] = value |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
92 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
93 |
value = generator.ComputeExpression(body, connections, executionOrderId > 0) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
94 |
connected_vars.append(([(parameter, input_info), (" := ", ())], |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
95 |
generator.ExtractModifier(variable, value, input_info))) |
814 | 96 |
else: |
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
97 |
all_input_connected = False |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
98 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
99 |
all_input_connected = False |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
100 |
if len(output_variables) > 1 or not all_input_connected: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
101 |
vars = [name + value for name, value in connected_vars] |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
102 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
103 |
vars = [value for name, value in connected_vars] |
814 | 104 |
if one_input_connected: |
105 |
for i, variable in enumerate(output_variables): |
|
106 |
parameter = variable.getformalParameter() |
|
1143
59818c488ead
Fixed bug in ST code generator when function output variable has no name
Laurent Bessard
parents:
1134
diff
changeset
|
107 |
if not inout_variables.has_key(parameter) and parameter in output_names + ["", "ENO"]: |
814 | 108 |
if variable.getformalParameter() == "": |
109 |
variable_name = "%s%d"%(type, block.getlocalId()) |
|
110 |
else: |
|
111 |
variable_name = "%s%d_%s"%(type, block.getlocalId(), parameter) |
|
112 |
if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] is not None or generator.Interface[-1][2]: |
|
113 |
generator.Interface.append(("VAR", None, False, [])) |
|
114 |
if variable.connectionPointOut in generator.ConnectionTypes: |
|
115 |
generator.Interface[-1][3].append((generator.ConnectionTypes[variable.connectionPointOut], variable_name, None, None)) |
|
116 |
else: |
|
117 |
generator.Interface[-1][3].append(("ANY", variable_name, None, None)) |
|
118 |
if len(output_variables) > 1 and parameter not in ["", "OUT"]: |
|
119 |
vars.append([(parameter, (generator.TagName, "block", block.getlocalId(), "output", i)), |
|
120 |
(" => %s"%variable_name, ())]) |
|
121 |
else: |
|
122 |
output_info = (generator.TagName, "block", block.getlocalId(), "output", i) |
|
123 |
output_name = variable_name |
|
124 |
generator.Program += [(generator.CurrentIndent, ()), |
|
125 |
(output_name, output_info), |
|
126 |
(" := ", ()), |
|
127 |
(type, (generator.TagName, "block", block.getlocalId(), "type")), |
|
128 |
("(", ())] |
|
129 |
generator.Program += JoinList([(", ", ())], vars) |
|
130 |
generator.Program += [(");\n", ())] |
|
131 |
else: |
|
132 |
generator.Warnings.append(_("\"%s\" function cancelled in \"%s\" POU: No input connected")%(type, generator.TagName.split("::")[-1])) |
|
133 |
elif block_infos["type"] == "functionBlock": |
|
134 |
if not generator.ComputedBlocks.get(block, False) and not order: |
|
135 |
generator.ComputedBlocks[block] = True |
|
136 |
vars = [] |
|
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
137 |
offset_idx = 0 |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
138 |
for variable in input_variables: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
139 |
parameter = variable.getformalParameter() |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
140 |
if parameter in input_names or parameter == "EN": |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
141 |
if parameter == "EN": |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
142 |
input_idx = 0 |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
143 |
offset_idx = 1 |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
144 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
145 |
input_idx = offset_idx + input_names.index(parameter) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
146 |
input_info = (generator.TagName, "block", block.getlocalId(), "input", input_idx) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
147 |
connections = variable.connectionPointIn.getconnections() |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
148 |
if connections is not None: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
149 |
value = generator.ComputeExpression(body, connections, executionOrderId > 0, inout_variables.has_key(parameter)) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
150 |
vars.append([(parameter, input_info), |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
151 |
(" := ", ())] + generator.ExtractModifier(variable, value, input_info)) |
814 | 152 |
generator.Program += [(generator.CurrentIndent, ()), |
153 |
(name, (generator.TagName, "block", block.getlocalId(), "name")), |
|
154 |
("(", ())] |
|
155 |
generator.Program += JoinList([(", ", ())], vars) |
|
156 |
generator.Program += [(");\n", ())] |
|
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
157 |
|
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
158 |
if link: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
159 |
connectionPoint = link.getposition()[-1] |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
160 |
output_parameter = link.getformalParameter() |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
161 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
162 |
connectionPoint = None |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
163 |
output_parameter = None |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
164 |
|
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
165 |
output_variable = None |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
166 |
output_idx = 0 |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
167 |
if output_parameter is not None: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
168 |
if output_parameter in output_names or output_parameter == "ENO": |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
169 |
for variable in output_variables: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
170 |
if variable.getformalParameter() == output_parameter: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
171 |
output_variable = variable |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
172 |
if output_parameter != "ENO": |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
173 |
output_idx = output_names.index(output_parameter) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
174 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
175 |
for i, variable in enumerate(output_variables): |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
176 |
blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
177 |
if (not connectionPoint or |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
178 |
block.getx() + blockPointx == connectionPoint.getx() and |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
179 |
block.gety() + blockPointy == connectionPoint.gety()): |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
180 |
output_variable = variable |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
181 |
output_parameter = variable.getformalParameter() |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
182 |
output_idx = i |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
183 |
|
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
184 |
if output_variable is not None: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
185 |
if block_infos["type"] == "function": |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
186 |
output_info = (generator.TagName, "block", block.getlocalId(), "output", output_idx) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
187 |
if inout_variables.has_key(output_parameter): |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
188 |
output_value = inout_variables[output_parameter] |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
189 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
190 |
if output_parameter == "": |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
191 |
output_name = "%s%d"%(type, block.getlocalId()) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
192 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
193 |
output_name = "%s%d_%s"%(type, block.getlocalId(), output_parameter) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
194 |
output_value = [(output_name, output_info)] |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
195 |
return generator.ExtractModifier(output_variable, output_value, output_info) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
196 |
|
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
197 |
if block_infos["type"] == "functionBlock": |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
198 |
output_info = (generator.TagName, "block", block.getlocalId(), "output", output_idx) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
199 |
output_name = generator.ExtractModifier(output_variable, [("%s.%s"%(name, output_parameter), output_info)], output_info) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
200 |
if to_inout: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
201 |
variable_name = "%s_%s"%(name, output_parameter) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
202 |
if not generator.IsAlreadyDefined(variable_name): |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
203 |
if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] is not None or generator.Interface[-1][2]: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
204 |
generator.Interface.append(("VAR", None, False, [])) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
205 |
if variable.connectionPointOut in generator.ConnectionTypes: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
206 |
generator.Interface[-1][3].append( |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
207 |
(generator.ConnectionTypes[output_variable.connectionPointOut], variable_name, None, None)) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
208 |
else: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
209 |
generator.Interface[-1][3].append(("ANY", variable_name, None, None)) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
210 |
generator.Program += [(generator.CurrentIndent, ()), |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
211 |
("%s := "%variable_name, ())] |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
212 |
generator.Program += output_name |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
213 |
generator.Program += [(";\n", ())] |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
214 |
return [(variable_name, ())] |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
215 |
return output_name |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
216 |
if link is not None: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
217 |
if output_parameter is None: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
218 |
output_parameter = "" |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
219 |
if name: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
220 |
blockname = "%s(%s)" % (name, type) |
814 | 221 |
else: |
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
222 |
blockname = type |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
223 |
raise ValueError, _("No output %s variable found in block %s in POU %s. Connection must be broken") % \ |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
965
diff
changeset
|
224 |
(output_parameter, blockname, generator.Name) |
814 | 225 |
|
226 |
def initialise_block(type, name, block = None): |
|
227 |
return [(type, name, None, None)] |
|
228 |
||
229 |
#------------------------------------------------------------------------------- |
|
230 |
# Function Block Types definitions |
|
231 |
#------------------------------------------------------------------------------- |
|
232 |
||
233 |
||
234 |
""" |
|
235 |
Ordored list of common Function Blocks defined in the IEC 61131-3 |
|
236 |
Each block have this attributes: |
|
237 |
- "name" : The block name |
|
238 |
- "type" : The block type. It can be "function", "functionBlock" or "program" |
|
239 |
- "extensible" : Boolean that define if the block is extensible |
|
240 |
- "inputs" : List of the block inputs |
|
241 |
- "outputs" : List of the block outputs |
|
242 |
- "comment" : Comment that will be displayed in the block popup |
|
243 |
- "generate" : Method that generator will call for generating ST block code |
|
244 |
Inputs and outputs are a tuple of characteristics that are in order: |
|
245 |
- The name |
|
246 |
- The data type |
|
247 |
- The default modifier which can be "none", "negated", "rising" or "falling" |
|
248 |
""" |
|
249 |
||
250 |
BlockTypes = [{"name" : _("Standard function blocks"), "list": |
|
251 |
[{"name" : "SR", "type" : "functionBlock", "extensible" : False, |
|
252 |
"inputs" : [("S1","BOOL","none"),("R","BOOL","none")], |
|
253 |
"outputs" : [("Q1","BOOL","none")], |
|
254 |
"comment" : _("SR bistable\nThe SR bistable is a latch where the Set dominates."), |
|
255 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
256 |
{"name" : "RS", "type" : "functionBlock", "extensible" : False, |
|
257 |
"inputs" : [("S","BOOL","none"),("R1","BOOL","none")], |
|
258 |
"outputs" : [("Q1","BOOL","none")], |
|
259 |
"comment" : _("RS bistable\nThe RS bistable is a latch where the Reset dominates."), |
|
260 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
261 |
{"name" : "SEMA", "type" : "functionBlock", "extensible" : False, |
|
262 |
"inputs" : [("CLAIM","BOOL","none"),("RELEASE","BOOL","none")], |
|
263 |
"outputs" : [("BUSY","BOOL","none")], |
|
264 |
"comment" : _("Semaphore\nThe semaphore provides a mechanism to allow software elements mutually exclusive access to certain ressources."), |
|
265 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
266 |
{"name" : "R_TRIG", "type" : "functionBlock", "extensible" : False, |
|
267 |
"inputs" : [("CLK","BOOL","none")], |
|
268 |
"outputs" : [("Q","BOOL","none")], |
|
269 |
"comment" : _("Rising edge detector\nThe output produces a single pulse when a rising edge is detected."), |
|
270 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
271 |
{"name" : "F_TRIG", "type" : "functionBlock", "extensible" : False, |
|
272 |
"inputs" : [("CLK","BOOL","none")], |
|
273 |
"outputs" : [("Q","BOOL","none")], |
|
274 |
"comment" : _("Falling edge detector\nThe output produces a single pulse when a falling edge is detected."), |
|
275 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
276 |
{"name" : "CTU", "type" : "functionBlock", "extensible" : False, |
|
277 |
"inputs" : [("CU","BOOL","rising"),("R","BOOL","none"),("PV","INT","none")], |
|
278 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
279 |
"comment" : _("Up-counter\nThe up-counter can be used to signal when a count has reached a maximum value."), |
|
280 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
281 |
{"name" : "CTD", "type" : "functionBlock", "extensible" : False, |
|
282 |
"inputs" : [("CD","BOOL","rising"),("LD","BOOL","none"),("PV","INT","none")], |
|
283 |
"outputs" : [("Q","BOOL","none"),("CV","INT","none")], |
|
284 |
"comment" : _("Down-counter\nThe down-counter can be used to signal when a count has reached zero, on counting down from a preset value."), |
|
285 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
286 |
{"name" : "CTUD", "type" : "functionBlock", "extensible" : False, |
|
287 |
"inputs" : [("CU","BOOL","rising"),("CD","BOOL","rising"),("R","BOOL","none"),("LD","BOOL","none"),("PV","INT","none")], |
|
288 |
"outputs" : [("QU","BOOL","none"),("QD","BOOL","none"),("CV","INT","none")], |
|
289 |
"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 and down on the other."), |
|
290 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
291 |
{"name" : "TP", "type" : "functionBlock", "extensible" : False, |
|
292 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
293 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
294 |
"comment" : _("Pulse timer\nThe pulse timer can be used to generate output pulses of a given time duration."), |
|
295 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
296 |
{"name" : "TON", "type" : "functionBlock", "extensible" : False, |
|
297 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
298 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
299 |
"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."), |
|
300 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
301 |
{"name" : "TOF", "type" : "functionBlock", "extensible" : False, |
|
302 |
"inputs" : [("IN","BOOL","none"),("PT","TIME","none")], |
|
303 |
"outputs" : [("Q","BOOL","none"),("ET","TIME","none")], |
|
304 |
"comment" : _("Off-delay timer\nThe off-delay timer can be used to delay setting an output false, for fixed period after input goes false."), |
|
305 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
306 |
]}, |
|
307 |
{"name" : _("Additional function blocks"), "list": |
|
308 |
[{"name" : "RTC", "type" : "functionBlock", "extensible" : False, |
|
309 |
"inputs" : [("IN","BOOL","none"),("PDT","DATE_AND_TIME","none")], |
|
310 |
"outputs" : [("Q","BOOL","none"),("CDT","DATE_AND_TIME","none")], |
|
311 |
"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."), |
|
312 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
313 |
{"name" : "INTEGRAL", "type" : "functionBlock", "extensible" : False, |
|
314 |
"inputs" : [("RUN","BOOL","none"),("R1","BOOL","none"),("XIN","REAL","none"),("X0","REAL","none"),("CYCLE","TIME","none")], |
|
315 |
"outputs" : [("Q","BOOL","none"),("XOUT","REAL","none")], |
|
316 |
"comment" : _("Integral\nThe integral function block integrates the value of input XIN over time."), |
|
317 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
318 |
{"name" : "DERIVATIVE", "type" : "functionBlock", "extensible" : False, |
|
319 |
"inputs" : [("RUN","BOOL","none"),("XIN","REAL","none"),("CYCLE","TIME","none")], |
|
320 |
"outputs" : [("XOUT","REAL","none")], |
|
321 |
"comment" : _("Derivative\nThe derivative function block produces an output XOUT proportional to the rate of change of the input XIN."), |
|
322 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
323 |
{"name" : "PID", "type" : "functionBlock", "extensible" : False, |
|
324 |
"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")], |
|
325 |
"outputs" : [("XOUT","REAL","none")], |
|
326 |
"comment" : _("PID\nThe PID (proportional, Integral, Derivative) function block provides the classical three term controller for closed loop control."), |
|
327 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
328 |
{"name" : "RAMP", "type" : "functionBlock", "extensible" : False, |
|
329 |
"inputs" : [("RUN","BOOL","none"),("X0","REAL","none"),("X1","REAL","none"),("TR","TIME","none"),("CYCLE","TIME","none")], |
|
330 |
"outputs" : [("BUSY","BOOL","none"),("XOUT","REAL","none")], |
|
331 |
"comment" : _("Ramp\nThe RAMP function block is modelled on example given in the standard."), |
|
332 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
333 |
{"name" : "HYSTERESIS", "type" : "functionBlock", "extensible" : False, |
|
334 |
"inputs" : [("XIN1","REAL","none"),("XIN2","REAL","none"),("EPS","REAL","none")], |
|
335 |
"outputs" : [("Q","BOOL","none")], |
|
336 |
"comment" : _("Hysteresis\nThe hysteresis function block provides a hysteresis boolean output driven by the difference of two floating point (REAL) inputs XIN1 and XIN2."), |
|
337 |
"generate" : generate_block, "initialise" : initialise_block}, |
|
338 |
## {"name" : "RATIO_MONITOR", "type" : "functionBlock", "extensible" : False, |
|
339 |
## "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")], |
|
340 |
## "outputs" : [("ALARM","BOOL","none"),("TOTAL_ERR","BOOL","none")], |
|
341 |
## "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."), |
|
342 |
## "generate" : generate_block, "initialise" : initialise_block} |
|
343 |
]}, |
|
344 |
] |
|
345 |
||
346 |
||
347 |
#------------------------------------------------------------------------------- |
|
348 |
# Data Types definitions |
|
349 |
#------------------------------------------------------------------------------- |
|
350 |
||
351 |
""" |
|
352 |
Ordored list of common data types defined in the IEC 61131-3 |
|
353 |
Each type is associated to his direct parent type. It defines then a hierarchy |
|
354 |
between type that permits to make a comparison of two types |
|
355 |
""" |
|
356 |
TypeHierarchy_list = [ |
|
357 |
("ANY", None), |
|
358 |
("ANY_DERIVED", "ANY"), |
|
359 |
("ANY_ELEMENTARY", "ANY"), |
|
360 |
("ANY_MAGNITUDE", "ANY_ELEMENTARY"), |
|
361 |
("ANY_BIT", "ANY_ELEMENTARY"), |
|
362 |
("ANY_NBIT", "ANY_BIT"), |
|
363 |
("ANY_STRING", "ANY_ELEMENTARY"), |
|
364 |
("ANY_DATE", "ANY_ELEMENTARY"), |
|
365 |
("ANY_NUM", "ANY_MAGNITUDE"), |
|
366 |
("ANY_REAL", "ANY_NUM"), |
|
367 |
("ANY_INT", "ANY_NUM"), |
|
368 |
("ANY_SINT", "ANY_INT"), |
|
369 |
("ANY_UINT", "ANY_INT"), |
|
370 |
("BOOL", "ANY_BIT"), |
|
371 |
("SINT", "ANY_SINT"), |
|
372 |
("INT", "ANY_SINT"), |
|
373 |
("DINT", "ANY_SINT"), |
|
374 |
("LINT", "ANY_SINT"), |
|
375 |
("USINT", "ANY_UINT"), |
|
376 |
("UINT", "ANY_UINT"), |
|
377 |
("UDINT", "ANY_UINT"), |
|
378 |
("ULINT", "ANY_UINT"), |
|
379 |
("REAL", "ANY_REAL"), |
|
380 |
("LREAL", "ANY_REAL"), |
|
381 |
("TIME", "ANY_MAGNITUDE"), |
|
382 |
("DATE", "ANY_DATE"), |
|
383 |
("TOD", "ANY_DATE"), |
|
384 |
("DT", "ANY_DATE"), |
|
385 |
("STRING", "ANY_STRING"), |
|
386 |
("BYTE", "ANY_NBIT"), |
|
387 |
("WORD", "ANY_NBIT"), |
|
388 |
("DWORD", "ANY_NBIT"), |
|
389 |
("LWORD", "ANY_NBIT") |
|
390 |
#("WSTRING", "ANY_STRING") # TODO |
|
391 |
] |
|
392 |
||
393 |
TypeHierarchy = dict(TypeHierarchy_list) |
|
394 |
||
395 |
""" |
|
396 |
returns true if the given data type is the same that "reference" meta-type or one of its types. |
|
397 |
""" |
|
398 |
def IsOfType(type, reference): |
|
399 |
if reference is None: |
|
400 |
return True |
|
401 |
elif type == reference: |
|
402 |
return True |
|
403 |
else: |
|
404 |
parent_type = TypeHierarchy[type] |
|
405 |
if parent_type is not None: |
|
406 |
return IsOfType(parent_type, reference) |
|
407 |
return False |
|
408 |
||
409 |
""" |
|
410 |
returns list of all types that correspont to the ANY* meta type |
|
411 |
""" |
|
412 |
def GetSubTypes(type): |
|
413 |
return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)] |
|
414 |
||
415 |
||
416 |
DataTypeRange_list = [ |
|
417 |
("SINT", (-2**7, 2**7 - 1)), |
|
418 |
("INT", (-2**15, 2**15 - 1)), |
|
419 |
("DINT", (-2**31, 2**31 - 1)), |
|
420 |
("LINT", (-2**31, 2**31 - 1)), |
|
421 |
("USINT", (0, 2**8 - 1)), |
|
422 |
("UINT", (0, 2**16 - 1)), |
|
423 |
("UDINT", (0, 2**31 - 1)), |
|
424 |
("ULINT", (0, 2**31 - 1)) |
|
425 |
] |
|
426 |
||
427 |
DataTypeRange = dict(DataTypeRange_list) |
|
428 |
||
429 |
||
430 |
||
431 |
#------------------------------------------------------------------------------- |
|
432 |
# Test identifier |
|
433 |
#------------------------------------------------------------------------------- |
|
434 |
||
965 | 435 |
IDENTIFIER_MODEL = re.compile( |
436 |
"(?:%(letter)s|_(?:%(letter)s|%(digit)s))(?:_?(?:%(letter)s|%(digit)s))*$" % |
|
437 |
{"letter": "[a-zA-Z]", "digit": "[0-9]"}) |
|
814 | 438 |
|
439 |
# Test if identifier is valid |
|
440 |
def TestIdentifier(identifier): |
|
965 | 441 |
return IDENTIFIER_MODEL.match(identifier) is not None |
814 | 442 |
|
443 |
#------------------------------------------------------------------------------- |
|
444 |
# Standard functions list generation |
|
445 |
#------------------------------------------------------------------------------- |
|
446 |
||
447 |
||
448 |
""" |
|
449 |
take a .csv file and translate it it a "csv_table" |
|
450 |
""" |
|
451 |
def csv_file_to_table(file): |
|
452 |
return [ map(string.strip,line.split(';')) for line in file.xreadlines()] |
|
453 |
||
454 |
""" |
|
455 |
seek into the csv table to a section ( section_name match 1st field ) |
|
456 |
return the matching row without first field |
|
457 |
""" |
|
458 |
def find_section(section_name, table): |
|
459 |
fields = [None] |
|
460 |
while(fields[0] != section_name): |
|
461 |
fields = table.pop(0) |
|
462 |
return fields[1:] |
|
463 |
||
464 |
""" |
|
465 |
extract the standard functions standard parameter names and types... |
|
466 |
return a { ParameterName: Type, ...} |
|
467 |
""" |
|
468 |
def get_standard_funtions_input_variables(table): |
|
469 |
variables = find_section("Standard_functions_variables_types", table) |
|
470 |
standard_funtions_input_variables = {} |
|
471 |
fields = [True,True] |
|
472 |
while(fields[1]): |
|
473 |
fields = table.pop(0) |
|
474 |
variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!='']) |
|
475 |
standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type'] |
|
476 |
return standard_funtions_input_variables |
|
477 |
||
478 |
""" |
|
479 |
translate .csv file input declaration into PLCOpenEditor interessting values |
|
480 |
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...} |
|
481 |
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] |
|
482 |
""" |
|
483 |
def csv_input_translate(str_decl, variables, base): |
|
484 |
decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',') |
|
485 |
params = [] |
|
486 |
||
487 |
len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables]) |
|
488 |
||
489 |
for param_type in decl: |
|
490 |
if param_type in variables.keys(): |
|
491 |
param_name = param_type |
|
492 |
param_type = variables[param_type] |
|
493 |
elif len_of_not_predifined_variable > 1: |
|
494 |
param_name = "IN%d"%base |
|
495 |
base += 1 |
|
496 |
else: |
|
497 |
param_name = "IN" |
|
498 |
params.append((param_name, param_type, "none")) |
|
499 |
return params |
|
500 |
||
501 |
||
502 |
ANY_TO_ANY_LIST=[ |
|
503 |
# simple type conv are let as C cast |
|
504 |
(("ANY_INT","ANY_BIT"),("ANY_NUM","ANY_BIT"), ("return_type", "__move_", "IN_type")), |
|
505 |
(("ANY_REAL",),("ANY_REAL",), ("return_type", "__move_", "IN_type")), |
|
506 |
# REAL_TO_INT |
|
507 |
(("ANY_REAL",),("ANY_SINT",), ("return_type", "__real_to_sint", None)), |
|
508 |
(("ANY_REAL",),("ANY_UINT",), ("return_type", "__real_to_uint", None)), |
|
509 |
(("ANY_REAL",),("ANY_BIT",), ("return_type", "__real_to_bit", None)), |
|
510 |
# TO_TIME |
|
511 |
(("ANY_INT","ANY_BIT"),("ANY_DATE","TIME"), ("return_type", "__int_to_time", None)), |
|
512 |
(("ANY_REAL",),("ANY_DATE","TIME"), ("return_type", "__real_to_time", None)), |
|
513 |
(("ANY_STRING",), ("ANY_DATE","TIME"), ("return_type", "__string_to_time", None)), |
|
514 |
# FROM_TIME |
|
515 |
(("ANY_DATE","TIME"), ("ANY_REAL",), ("return_type", "__time_to_real", None)), |
|
516 |
(("ANY_DATE","TIME"), ("ANY_INT","ANY_NBIT"), ("return_type", "__time_to_int", None)), |
|
517 |
(("TIME",), ("ANY_STRING",), ("return_type", "__time_to_string", None)), |
|
518 |
(("DATE",), ("ANY_STRING",), ("return_type", "__date_to_string", None)), |
|
519 |
(("TOD",), ("ANY_STRING",), ("return_type", "__tod_to_string", None)), |
|
520 |
(("DT",), ("ANY_STRING",), ("return_type", "__dt_to_string", None)), |
|
521 |
# TO_STRING |
|
522 |
(("BOOL",), ("ANY_STRING",), ("return_type", "__bool_to_string", None)), |
|
523 |
(("ANY_BIT",), ("ANY_STRING",), ("return_type", "__bit_to_string", None)), |
|
524 |
(("ANY_REAL",), ("ANY_STRING",), ("return_type", "__real_to_string", None)), |
|
525 |
(("ANY_SINT",), ("ANY_STRING",), ("return_type", "__sint_to_string", None)), |
|
526 |
(("ANY_UINT",), ("ANY_STRING",), ("return_type", "__uint_to_string", None)), |
|
527 |
# FROM_STRING |
|
528 |
(("ANY_STRING",), ("BOOL",), ("return_type", "__string_to_bool", None)), |
|
529 |
(("ANY_STRING",), ("ANY_BIT",), ("return_type", "__string_to_bit", None)), |
|
530 |
(("ANY_STRING",), ("ANY_SINT",), ("return_type", "__string_to_sint", None)), |
|
531 |
(("ANY_STRING",), ("ANY_UINT",), ("return_type", "__string_to_uint", None)), |
|
532 |
(("ANY_STRING",), ("ANY_REAL",), ("return_type", "__string_to_real", None))] |
|
533 |
||
534 |
||
535 |
BCD_TO_ANY_LIST=[ |
|
536 |
(("BYTE",),("USINT",), ("return_type", "__bcd_to_uint", None)), |
|
537 |
(("WORD",),("UINT",), ("return_type", "__bcd_to_uint", None)), |
|
538 |
(("DWORD",),("UDINT",), ("return_type", "__bcd_to_uint", None)), |
|
539 |
(("LWORD",),("ULINT",), ("return_type", "__bcd_to_uint", None))] |
|
540 |
||
541 |
||
542 |
ANY_TO_BCD_LIST=[ |
|
543 |
(("USINT",),("BYTE",), ("return_type", "__uint_to_bcd", None)), |
|
544 |
(("UINT",),("WORD",), ("return_type", "__uint_to_bcd", None)), |
|
545 |
(("UDINT",),("DWORD",), ("return_type", "__uint_to_bcd", None)), |
|
546 |
(("ULINT",),("LWORD",), ("return_type", "__uint_to_bcd", None))] |
|
547 |
||
548 |
||
549 |
def ANY_TO_ANY_FORMAT_GEN(any_to_any_list, fdecl): |
|
550 |
||
551 |
for (InTypes, OutTypes, Format) in any_to_any_list: |
|
552 |
outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), OutTypes)) |
|
553 |
inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), InTypes)) |
|
554 |
if inps and outs and fdecl["outputs"][0][1] != fdecl["inputs"][0][1]: |
|
555 |
return Format |
|
556 |
||
557 |
return None |
|
558 |
||
559 |
||
560 |
""" |
|
561 |
Returns this kind of declaration for all standard functions |
|
562 |
||
563 |
[{"name" : "Numerical", 'list': [ { |
|
564 |
'baseinputnumber': 1, |
|
565 |
'comment': 'Addition', |
|
566 |
'extensible': True, |
|
567 |
'inputs': [ ('IN1', 'ANY_NUM', 'none'), |
|
568 |
('IN2', 'ANY_NUM', 'none')], |
|
569 |
'name': 'ADD', |
|
570 |
'outputs': [('OUT', 'ANY_NUM', 'none')], |
|
571 |
'type': 'function'}, ...... ] },.....] |
|
572 |
""" |
|
573 |
def get_standard_funtions(table): |
|
574 |
||
575 |
variables = get_standard_funtions_input_variables(table) |
|
576 |
||
577 |
fonctions = find_section("Standard_functions_type",table) |
|
578 |
||
579 |
Standard_Functions_Decl = [] |
|
580 |
Current_section = None |
|
581 |
||
582 |
translate = { |
|
583 |
"extensible" : lambda x: {"yes":True, "no":False}[x], |
|
584 |
"inputs" : lambda x:csv_input_translate(x,variables,baseinputnumber), |
|
585 |
"outputs":lambda x:[("OUT",x,"none")]} |
|
586 |
||
587 |
for fields in table: |
|
588 |
if fields[1]: |
|
589 |
# If function section name given |
|
590 |
if fields[0]: |
|
591 |
words = fields[0].split('"') |
|
592 |
if len(words) > 1: |
|
593 |
section_name = words[1] |
|
594 |
else: |
|
595 |
section_name = fields[0] |
|
596 |
Current_section = {"name" : section_name, "list" : []} |
|
597 |
Standard_Functions_Decl.append(Current_section) |
|
598 |
Function_decl_list = [] |
|
599 |
if Current_section: |
|
600 |
Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ]) |
|
601 |
Function_decl["generate"] = generate_block |
|
602 |
Function_decl["initialise"] = lambda x,y:[] |
|
603 |
baseinputnumber = int(Function_decl.get("baseinputnumber",1)) |
|
604 |
Function_decl["baseinputnumber"] = baseinputnumber |
|
605 |
for param, value in Function_decl.iteritems(): |
|
606 |
if param in translate: |
|
607 |
Function_decl[param] = translate[param](value) |
|
608 |
Function_decl["type"] = "function" |
|
609 |
||
610 |
if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*') : |
|
611 |
input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1]) |
|
612 |
output_types = GetSubTypes(Function_decl["outputs"][0][1]) |
|
613 |
else: |
|
614 |
input_ovrloading_types = [None] |
|
615 |
output_types = [None] |
|
616 |
||
617 |
funcdeclname_orig = Function_decl["name"] |
|
618 |
funcdeclname = Function_decl["name"].strip('*_') |
|
619 |
fdc = Function_decl["inputs"][:] |
|
620 |
for intype in input_ovrloading_types: |
|
621 |
if intype != None: |
|
622 |
Function_decl["inputs"] = [] |
|
623 |
for decl_tpl in fdc: |
|
624 |
if IsOfType(intype, decl_tpl[1]): |
|
625 |
Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])] |
|
626 |
else: |
|
627 |
Function_decl["inputs"] += [(decl_tpl)] |
|
628 |
||
629 |
if funcdeclname_orig.startswith('*'): |
|
630 |
funcdeclin = intype + '_' + funcdeclname |
|
631 |
else: |
|
632 |
funcdeclin = funcdeclname |
|
633 |
else: |
|
634 |
funcdeclin = funcdeclname |
|
635 |
||
636 |
for outype in output_types: |
|
637 |
if outype != None: |
|
638 |
decl_tpl = Function_decl["outputs"][0] |
|
639 |
Function_decl["outputs"] = [ (decl_tpl[0] , outype, decl_tpl[2])] |
|
640 |
if funcdeclname_orig.endswith('*'): |
|
641 |
funcdeclout = funcdeclin + '_' + outype |
|
642 |
else: |
|
643 |
funcdeclout = funcdeclin |
|
644 |
else: |
|
645 |
funcdeclout = funcdeclin |
|
646 |
Function_decl["name"] = funcdeclout |
|
647 |
||
648 |
||
649 |
fdecl = Function_decl |
|
650 |
res = eval(Function_decl["python_eval_c_code_format"]) |
|
651 |
||
652 |
if res != None : |
|
653 |
# create the copy of decl dict to be appended to section |
|
654 |
Function_decl_copy = Function_decl.copy() |
|
655 |
Current_section["list"].append(Function_decl_copy) |
|
656 |
else: |
|
657 |
raise "First function must be in a category" |
|
658 |
||
659 |
return Standard_Functions_Decl |
|
660 |
||
661 |
std_decl = get_standard_funtions(csv_file_to_table(open(os.path.join(os.path.split(__file__)[0],"iec_std.csv"))))#, True) |
|
662 |
||
663 |
BlockTypes.extend(std_decl) |
|
664 |
||
665 |
for section in BlockTypes: |
|
666 |
for desc in section["list"]: |
|
667 |
words = desc["comment"].split('"') |
|
668 |
if len(words) > 1: |
|
669 |
desc["comment"] = words[1] |
|
670 |
desc["usage"] = ( |
|
671 |
"\n (" + |
|
672 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in desc["inputs"]]).strip("[]").replace("'",'') + |
|
673 |
" ) => (" + |
|
674 |
str([ " " + fctdecl[1]+":"+fctdecl[0] for fctdecl in desc["outputs"]]).strip("[]").replace("'",'') + |
|
675 |
" )") |
|
676 |
||
677 |
||
678 |
#------------------------------------------------------------------------------- |
|
679 |
# Languages Keywords |
|
680 |
#------------------------------------------------------------------------------- |
|
681 |
||
682 |
||
683 |
# Keywords for Pou Declaration |
|
684 |
POU_BLOCK_START_KEYWORDS = ["FUNCTION", "FUNCTION_BLOCK", "PROGRAM"] |
|
685 |
POU_BLOCK_END_KEYWORDS = ["END_FUNCTION", "END_FUNCTION_BLOCK", "END_PROGRAM"] |
|
686 |
POU_KEYWORDS = ["EN", "ENO", "F_EDGE", "R_EDGE"] + POU_BLOCK_START_KEYWORDS + POU_BLOCK_END_KEYWORDS |
|
687 |
for category in BlockTypes: |
|
688 |
for block in category["list"]: |
|
689 |
if block["name"] not in POU_KEYWORDS: |
|
690 |
POU_KEYWORDS.append(block["name"]) |
|
691 |
||
692 |
||
693 |
# Keywords for Type Declaration |
|
694 |
TYPE_BLOCK_START_KEYWORDS = ["TYPE", "STRUCT"] |
|
695 |
TYPE_BLOCK_END_KEYWORDS = ["END_TYPE", "END_STRUCT"] |
|
696 |
TYPE_KEYWORDS = ["ARRAY", "OF", "T", "D", "TIME_OF_DAY", "DATE_AND_TIME"] + TYPE_BLOCK_START_KEYWORDS + TYPE_BLOCK_END_KEYWORDS |
|
697 |
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS]) |
|
698 |
||
699 |
||
700 |
# Keywords for Variable Declaration |
|
701 |
VAR_BLOCK_START_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", "VAR_EXTERNAL"] |
|
702 |
VAR_BLOCK_END_KEYWORDS = ["END_VAR"] |
|
703 |
VAR_KEYWORDS = ["AT", "CONSTANT", "RETAIN", "NON_RETAIN"] + VAR_BLOCK_START_KEYWORDS + VAR_BLOCK_END_KEYWORDS |
|
704 |
||
705 |
||
706 |
# Keywords for Configuration Declaration |
|
707 |
CONFIG_BLOCK_START_KEYWORDS = ["CONFIGURATION", "RESOURCE", "VAR_ACCESS", "VAR_CONFIG", "VAR_GLOBAL"] |
|
708 |
CONFIG_BLOCK_END_KEYWORDS = ["END_CONFIGURATION", "END_RESOURCE", "END_VAR"] |
|
709 |
CONFIG_KEYWORDS = ["ON", "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK"] + CONFIG_BLOCK_START_KEYWORDS + CONFIG_BLOCK_END_KEYWORDS |
|
710 |
||
711 |
# Keywords for Structured Function Chart |
|
712 |
SFC_BLOCK_START_KEYWORDS = ["ACTION", "INITIAL_STEP", "STEP", "TRANSITION"] |
|
713 |
SFC_BLOCK_END_KEYWORDS = ["END_ACTION", "END_STEP", "END_TRANSITION"] |
|
949 | 714 |
SFC_KEYWORDS = ["FROM", "TO"] + SFC_BLOCK_START_KEYWORDS + SFC_BLOCK_END_KEYWORDS |
814 | 715 |
|
716 |
||
717 |
# Keywords for Instruction List |
|
718 |
IL_KEYWORDS = ["TRUE", "FALSE", "LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN", |
|
719 |
"XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE", |
|
720 |
"LE", "LT", "JMP", "JMPC", "JMPCN", "CAL", "CALC", "CALCN", "RET", "RETC", "RETCN"] |
|
721 |
||
722 |
||
723 |
# Keywords for Structured Text |
|
724 |
ST_BLOCK_START_KEYWORDS = ["IF", "ELSIF", "ELSE", "CASE", "FOR", "WHILE", "REPEAT"] |
|
725 |
ST_BLOCK_END_KEYWORDS = ["END_IF", "END_CASE", "END_FOR", "END_WHILE", "END_REPEAT"] |
|
726 |
ST_KEYWORDS = ["TRUE", "FALSE", "THEN", "OF", "TO", "BY", "DO", "DO", "UNTIL", "EXIT", |
|
727 |
"RETURN", "NOT", "MOD", "AND", "XOR", "OR"] + ST_BLOCK_START_KEYWORDS + ST_BLOCK_END_KEYWORDS |
|
728 |
||
729 |
# All the keywords of IEC |
|
730 |
IEC_BLOCK_START_KEYWORDS = [] |
|
731 |
IEC_BLOCK_END_KEYWORDS = [] |
|
732 |
IEC_KEYWORDS = ["E", "TRUE", "FALSE"] |
|
733 |
for all_keywords, keywords_list in [(IEC_BLOCK_START_KEYWORDS, [POU_BLOCK_START_KEYWORDS, TYPE_BLOCK_START_KEYWORDS, |
|
734 |
VAR_BLOCK_START_KEYWORDS, CONFIG_BLOCK_START_KEYWORDS, |
|
735 |
SFC_BLOCK_START_KEYWORDS, ST_BLOCK_START_KEYWORDS]), |
|
736 |
(IEC_BLOCK_END_KEYWORDS, [POU_BLOCK_END_KEYWORDS, TYPE_BLOCK_END_KEYWORDS, |
|
737 |
VAR_BLOCK_END_KEYWORDS, CONFIG_BLOCK_END_KEYWORDS, |
|
738 |
SFC_BLOCK_END_KEYWORDS, ST_BLOCK_END_KEYWORDS]), |
|
739 |
(IEC_KEYWORDS, [POU_KEYWORDS, TYPE_KEYWORDS, VAR_KEYWORDS, CONFIG_KEYWORDS, |
|
740 |
SFC_KEYWORDS, IL_KEYWORDS, ST_KEYWORDS])]: |
|
741 |
for keywords in keywords_list: |
|
742 |
all_keywords.extend([keyword for keyword in keywords if keyword not in all_keywords]) |
|
743 |