forked from MichaelDrostWago/CodesysProgramGeneration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateModule.py
More file actions
177 lines (147 loc) · 6.63 KB
/
CreateModule.py
File metadata and controls
177 lines (147 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# encoding:utf-8
from __future__ import print_function
from datetime import date
import re
today = date.today()
FW_VERSION = 'FW27'
CODESYS_VERSION = 'V3.5 SP19 P2'
CONFIG_STRUCT="""\
//Name Type Init Comment
sName : String(10);
"""
VISU_STRUCT="""\
//Name Type Init Comment
xCmdDummy : BOOL;
xStatDummy : BOOL;
"""
INIT_METHOD="""\
// Initialize your module
IF THIS^._xInit Then
;
THIS^._xInit := FALSE;
END_IF
"""
########################################################################### WRTIE DECLARATION
# functions for code generation
def writeFbDeclaration(fwVersion, codesysVersion, actDate, fbName, ack, mode, optionConfig, optionVisu):
contentString = """// ===================================
// MICHAEL DROST COPYRIGHT (2024)
// Firmware Version: """
contentString = contentString + str(fwVersion) + "\n"
contentString = contentString + "// Codesys Verison: "
contentString = contentString + str(codesysVersion) + "\n"
contentString = contentString + "// Release Date: "
contentString = contentString + str(actDate) + "\n"
contentString = contentString + """// Author: MDrost
// Functionality:
// ===================================
FUNCTION_BLOCK """
contentString = contentString + str(fbName) + "\n"
contentString = contentString + """VAR_INPUT
//Name Type Init Comment
xEnable : bool; // Enable Module"""
if ack == True: contentString = contentString + """
xAck : Bool; // Acknowledge Error"""
if mode == True: contentString = contentString + """
xManual : Bool; // Manual Mode;"""
if optionConfig != '': contentString = contentString + """
utConfig :""" + str(optionConfig) + "; // Module Configuration\n"
contentString = contentString + """
END_VAR
VAR_IN_OUT
//Name Type Init Comment
"""
if optionVisu != '': contentString = contentString + "utVisItf :" + str(optionVisu) + ";\n"
contentString = contentString + """
END_VAR
VAR_OUTPUT
// Name Type Init Comment
wState : word; // // state of the functionblock
END_VAR
VAR
//Name Type Init Comment
_xInit : bool := TRUE; // Init Cycle"""
if ack == True: contentString = contentString + """
_rtrigAck : R_TRIG; // Acknowledge Flag"""
contentString = contentString + """
END_VAR
VAR CONSTANT
// Name Type Init Comment
END_VAR
"""
return contentString
########################################################################### WRITE IMPLEMENTATION
def writeFbImplementation(ack, mode):
contentString = """\
if NOT xEnable THEN RETURN; END_IF
//=========================================================
// +++ INIT +++
mInit();
//=========================================================
//=========================================================
// +++ FLAGS & TIMER +++"""
if ack == True: contentString = contentString + """
_rtrigAck(CLK := xAck);"""
contentString = contentString + """
//=========================================================
//=========================================================
// +++ INPUTS +++
//=========================================================
//=========================================================
// +++ PROGRAMM +++
//=========================================================
//=========================================================
// +++ OUTPUTS +++
//=========================================================
//=========================================================
// +++ VISU +++
//=========================================================
//=========================================================
// +++ ALARMS +++
//=========================================================
"""
return contentString
########################################################################### CREATE NAME
def createName(prefix, moduleName):
# Regular expression to find all uppercase letters
pattern = r'([A-Z])'
# Substitute each uppercase letter with _ followed by the letter
modified_string = re.sub(pattern, r'_\1', moduleName)
result = str(prefix).upper() + modified_string.upper()
return result
########################################################################### FUNCTION
proj = projects.primary
print("Generate default Module")
moduleName = system.ui.query_string("Modulename")
# create basic module structure
proj.create_folder(moduleName)
folder = proj.find(moduleName, recursive = True)[0]
folder.create_folder('DUT')
folder.create_folder('POU')
folder.create_folder('VIS')
# create options for module
print("Generate options for module")
res = system.ui.select_many("Please select one or more options", PromptChoice.OKCancel, PromptResult.OK, ("Ack", "Mode", "ST_Config", "VISU_ITF"))
print("The returned result is: '%s'" % str(res)) # res is a tuple
if res[1][2]: # choosen config option
stConfig = createName('ST_CONF', moduleName)
dut = folder.create_dut(stConfig)
dut.textual_declaration.insert(2, 0, CONFIG_STRUCT)
else:
stConfig = ''
if res[1][3]: # choosen Visu option
stVis = createName('ST_VIS', moduleName)
dut = folder.create_dut(stVis)
dut.textual_declaration.insert(2, 0, VISU_STRUCT)
else:
stVis = ''
# create module
pou = folder.create_pou(createName('FB', moduleName), PouType.FunctionBlock)
implementation = pou.textual_declaration.replace(writeFbDeclaration(FW_VERSION, CODESYS_VERSION, today,createName('FB', moduleName), res[1][0], res[1][1], stConfig, stVis))
# create implementation in module
pou.textual_implementation.replace(writeFbImplementation(res[1][0], res[1][1]))
# add init method to module
initMethod = pou.create_method('mInit', 'BOOL')
# write init method implementation
initMethod.textual_implementation.replace(INIT_METHOD)
print("DONE!")