author | greg |
Mon, 18 May 2009 07:58:41 +0200 | |
changeset 345 | a3520d75a722 |
parent 344 | 25b7b7f854bc |
child 350 | a3a5561bde1d |
permissions | -rwxr-xr-x |
229 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
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 |
||
25 |
import Pyro.core as pyro |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
26 |
from threading import Timer, Thread |
301 | 27 |
import ctypes, os, commands, types, sys |
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
28 |
import time |
301 | 29 |
|
229 | 30 |
if os.name in ("nt", "ce"): |
31 |
from _ctypes import LoadLibrary as dlopen |
|
32 |
from _ctypes import FreeLibrary as dlclose |
|
33 |
elif os.name == "posix": |
|
34 |
from _ctypes import dlopen, dlclose |
|
35 |
||
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
36 |
import traceback |
229 | 37 |
|
38 |
lib_ext ={ |
|
39 |
"linux2":".so", |
|
40 |
"win32":".dll", |
|
41 |
}.get(sys.platform, "") |
|
42 |
||
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
43 |
def PLCprint(message): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
44 |
sys.stdout.write("PLCobject : "+message+"\n") |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
45 |
sys.stdout.flush() |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
46 |
|
229 | 47 |
class PLCObject(pyro.ObjBase): |
235 | 48 |
_Idxs = [] |
329 | 49 |
def __init__(self, workingdir, daemon, argv, statuschange, evaluator): |
229 | 50 |
pyro.ObjBase.__init__(self) |
301 | 51 |
self.evaluator = evaluator |
229 | 52 |
self.argv = [workingdir] + argv # force argv[0] to be "path" to exec... |
53 |
self.workingdir = workingdir |
|
54 |
self.PLCStatus = "Stopped" |
|
55 |
self.PLClibraryHandle = None |
|
56 |
# Creates fake C funcs proxies |
|
57 |
self._FreePLC() |
|
58 |
self.daemon = daemon |
|
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
59 |
self.statuschange = statuschange |
301 | 60 |
self.hmi_frame = None |
229 | 61 |
|
62 |
# Get the last transfered PLC if connector must be restart |
|
63 |
try: |
|
64 |
self.CurrentPLCFilename=open( |
|
65 |
self._GetMD5FileName(), |
|
66 |
"r").read().strip() + lib_ext |
|
67 |
except Exception, e: |
|
68 |
self.PLCStatus = "Empty" |
|
69 |
self.CurrentPLCFilename=None |
|
70 |
||
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
71 |
def StatusChange(self): |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
72 |
if self.statuschange is not None: |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
73 |
self.statuschange(self.PLCStatus) |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
74 |
|
229 | 75 |
def _GetMD5FileName(self): |
76 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
77 |
||
78 |
def _GetLibFileName(self): |
|
79 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
80 |
||
81 |
||
82 |
def _LoadNewPLC(self): |
|
83 |
""" |
|
84 |
Load PLC library |
|
85 |
Declare all functions, arguments and return values |
|
86 |
""" |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
87 |
PLCprint("Load PLC") |
229 | 88 |
try: |
89 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
90 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
91 |
||
92 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
93 |
self._startPLC.restype = ctypes.c_int |
|
94 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
95 |
||
96 |
self._stopPLC = self.PLClibraryHandle.stopPLC |
|
97 |
self._stopPLC.restype = None |
|
98 |
||
99 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
100 |
self._ResetDebugVariables.restype = None |
|
101 |
||
235 | 102 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 103 |
self._RegisterDebugVariable.restype = None |
235 | 104 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int] |
229 | 105 |
|
106 |
self._IterDebugData = self.PLClibraryHandle.IterDebugData |
|
107 |
self._IterDebugData.restype = ctypes.c_void_p |
|
108 |
self._IterDebugData.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p)] |
|
109 |
||
110 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
111 |
self._FreeDebugData.restype = None |
|
112 |
||
113 |
self._WaitDebugData = self.PLClibraryHandle.WaitDebugData |
|
114 |
self._WaitDebugData.restype = ctypes.c_int |
|
235 | 115 |
|
116 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
117 |
self._suspendDebug.restype = None |
|
118 |
||
119 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
120 |
self._resumeDebug.restype = None |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
121 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
122 |
self._PythonIterator = self.PLClibraryHandle.PythonIterator |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
123 |
self._PythonIterator.restype = ctypes.c_char_p |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
124 |
self._PythonIterator.argtypes = [ctypes.c_char_p] |
235 | 125 |
|
229 | 126 |
return True |
127 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
128 |
PLCprint(traceback.format_exc()) |
229 | 129 |
return False |
130 |
||
131 |
def _FreePLC(self): |
|
132 |
""" |
|
133 |
Unload PLC library. |
|
134 |
This is also called by __init__ to create dummy C func proxies |
|
135 |
""" |
|
136 |
# Forget all refs to library |
|
137 |
self._startPLC = lambda:None |
|
138 |
self._stopPLC = lambda:None |
|
139 |
self._ResetDebugVariables = lambda:None |
|
140 |
self._RegisterDebugVariable = lambda x:None |
|
141 |
self._IterDebugData = lambda x,y:None |
|
142 |
self._FreeDebugData = lambda:None |
|
235 | 143 |
self._WaitDebugData = lambda:-1 |
144 |
self._suspendDebug = lambda:None |
|
145 |
self._resumeDebug = lambda:None |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
146 |
self._PythonIterator = lambda:"" |
229 | 147 |
self.PLClibraryHandle = None |
148 |
# Unload library explicitely |
|
149 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
150 |
PLCprint("Unload PLC") |
229 | 151 |
dlclose(self._PLClibraryHandle) |
152 |
res = self._DetectDirtyLibs() |
|
153 |
else: |
|
154 |
res = False |
|
155 |
||
156 |
self._PLClibraryHandle = None |
|
157 |
||
158 |
return res |
|
159 |
||
160 |
def _DetectDirtyLibs(self): |
|
161 |
# Detect dirty libs |
|
162 |
# Get lib dependencies (for dirty lib detection) |
|
163 |
if os.name == "posix": |
|
164 |
# parasiting libs listed with ldd |
|
165 |
badlibs = [ toks.split()[0] for toks in commands.getoutput( |
|
166 |
"ldd "+self._GetLibFileName()).splitlines() ] |
|
167 |
for badlib in badlibs: |
|
168 |
if badlib[:6] in ["libwx_", |
|
169 |
"libwxs", |
|
170 |
"libgtk", |
|
171 |
"libgdk", |
|
172 |
"libatk", |
|
173 |
"libpan", |
|
174 |
"libX11", |
|
175 |
]: |
|
176 |
#badhandle = dlopen(badlib, dl.RTLD_NOLOAD) |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
177 |
PLCprint("Dirty lib detected :" + badlib) |
229 | 178 |
#dlclose(badhandle) |
179 |
return True |
|
180 |
return False |
|
181 |
||
299 | 182 |
def PrepareRuntimePy(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
183 |
self.python_threads_vars = globals().copy() |
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
184 |
self.python_threads_vars["WorkingDir"] = self.workingdir |
283
d0e6fc0701fb
Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents:
280
diff
changeset
|
185 |
pyfile = os.path.join(self.workingdir, "runtime.py") |
301 | 186 |
hmifile = os.path.join(self.workingdir, "hmi.py") |
322
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
187 |
if os.path.exists(hmifile): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
188 |
try: |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
189 |
execfile(hmifile, self.python_threads_vars) |
332 | 190 |
if os.path.exists(pyfile): |
191 |
try: |
|
192 |
# TODO handle exceptions in runtime.py |
|
193 |
# pyfile may redefine _runtime_cleanup |
|
194 |
# or even call _PythonThreadProc itself. |
|
195 |
execfile(pyfile, self.python_threads_vars) |
|
196 |
except: |
|
197 |
PLCprint(traceback.format_exc()) |
|
322
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
198 |
if self.python_threads_vars.has_key('wx'): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
199 |
wx = self.python_threads_vars['wx'] |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
200 |
# try to instanciate the first frame found. |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
201 |
for name, obj in self.python_threads_vars.iteritems(): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
202 |
# obj is a class |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
203 |
if type(obj)==type(type) and issubclass(obj,wx.Frame): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
204 |
def create_frame(): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
205 |
self.hmi_frame = obj(None) |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
206 |
self.python_threads_vars[name] = self.hmi_frame |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
207 |
# keep track of class... never know |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
208 |
self.python_threads_vars['Class_'+name] = obj |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
209 |
self.hmi_frame.Bind(wx.EVT_CLOSE, OnCloseFrame) |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
210 |
self.hmi_frame.Show() |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
211 |
|
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
212 |
def OnCloseFrame(evt): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
213 |
wx.MessageBox("Please stop PLC to close") |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
214 |
create_frame() |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
215 |
break |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
216 |
except: |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
217 |
PLCprint(traceback.format_exc()) |
332 | 218 |
elif os.path.exists(pyfile): |
219 |
try: |
|
220 |
# TODO handle exceptions in runtime.py |
|
221 |
# pyfile may redefine _runtime_cleanup |
|
222 |
# or even call _PythonThreadProc itself. |
|
223 |
execfile(pyfile, self.python_threads_vars) |
|
224 |
except: |
|
225 |
PLCprint(traceback.format_exc()) |
|
299 | 226 |
runtime_begin = self.python_threads_vars.get("_runtime_begin",None) |
227 |
if runtime_begin is not None: |
|
329 | 228 |
runtime_begin() |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
229 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
230 |
def FinishRuntimePy(self): |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
332
diff
changeset
|
231 |
runtime_cleanup = None |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
332
diff
changeset
|
232 |
if self.python_threads_vars is not None: |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
332
diff
changeset
|
233 |
runtime_cleanup = self.python_threads_vars.get("_runtime_cleanup",None) |
299 | 234 |
if runtime_cleanup is not None: |
329 | 235 |
runtime_cleanup() |
301 | 236 |
if self.hmi_frame is not None: |
329 | 237 |
self.hmi_frame.Destroy() |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
238 |
self.python_threads_vars = None |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
239 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
240 |
def PythonThreadProc(self): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
241 |
PLCprint("PythonThreadProc started") |
329 | 242 |
self.evaluator(self.PrepareRuntimePy) |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
243 |
res,cmd = "None","None" |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
244 |
while self.PLCStatus == "Started": |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
245 |
#print "_PythonIterator(", res, ")", |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
246 |
cmd = self._PythonIterator(res) |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
247 |
#print " -> ", cmd |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
248 |
if cmd is None: |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
249 |
break |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
250 |
try : |
301 | 251 |
res = str(self.evaluator(eval,cmd,self.python_threads_vars)) |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
252 |
except Exception,e: |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
253 |
res = "#EXCEPTION : "+str(e) |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
254 |
PLCprint(res) |
329 | 255 |
self.evaluator(self.FinishRuntimePy) |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
256 |
PLCprint("PythonThreadProc interrupted") |
229 | 257 |
|
235 | 258 |
def StartPLC(self, debug=False): |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
259 |
PLCprint("StartPLC") |
229 | 260 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
261 |
c_argv = ctypes.c_char_p * len(self.argv) |
|
262 |
if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
|
235 | 263 |
if debug: |
327
e82c422ad811
Register the current watched variable in Debug Window before starting
lbessard
parents:
322
diff
changeset
|
264 |
for idx in self._Idxs: |
e82c422ad811
Register the current watched variable in Debug Window before starting
lbessard
parents:
322
diff
changeset
|
265 |
self._RegisterDebugVariable(idx) |
235 | 266 |
self._resumeDebug() |
229 | 267 |
self.PLCStatus = "Started" |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
268 |
self.StatusChange() |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
269 |
self.PythonThread = Thread(target=self.PythonThreadProc) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
270 |
self.PythonThread.start() |
229 | 271 |
return True |
272 |
else: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
273 |
PLCprint("Problem starting PLC") |
229 | 274 |
self._DoStopPLC() |
275 |
return False |
|
276 |
||
277 |
def _DoStopPLC(self): |
|
329 | 278 |
self.PLCStatus = "Stopped" |
339 | 279 |
self.StatusChange() |
229 | 280 |
self._stopPLC() |
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
281 |
if self.PythonThread.isAlive(): |
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
282 |
self.PythonThread.join() |
229 | 283 |
if self._FreePLC(): |
284 |
self.PLCStatus = "Dirty" |
|
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
285 |
self.StatusChange() |
229 | 286 |
return True |
287 |
||
288 |
def StopPLC(self): |
|
289 |
if self.PLCStatus == "Started": |
|
290 |
self._DoStopPLC() |
|
291 |
return True |
|
292 |
return False |
|
293 |
||
294 |
def _Reload(self): |
|
295 |
self.daemon.shutdown(True) |
|
296 |
self.daemon.sock.close() |
|
297 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
298 |
# never reached |
|
299 |
return 0 |
|
300 |
||
301 |
def ForceReload(self): |
|
302 |
# respawn python interpreter |
|
303 |
Timer(0.1,self._Reload).start() |
|
304 |
return True |
|
305 |
||
306 |
def GetPLCstatus(self): |
|
307 |
return self.PLCStatus |
|
308 |
||
309 |
def NewPLC(self, md5sum, data, extrafiles): |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
310 |
PLCprint("NewPLC (%s)"%md5sum) |
229 | 311 |
if self.PLCStatus in ["Stopped", "Empty", "Dirty"]: |
312 |
NewFileName = md5sum + lib_ext |
|
313 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
314 |
try: |
|
315 |
os.remove(os.path.join(self.workingdir, |
|
316 |
self.CurrentPLCFilename)) |
|
317 |
for filename in file(extra_files_log, "r").readlines() + extra_files_log: |
|
318 |
try: |
|
319 |
os.remove(os.path.join(self.workingdir, filename)) |
|
320 |
except: |
|
321 |
pass |
|
322 |
except: |
|
323 |
pass |
|
324 |
||
325 |
try: |
|
326 |
# Create new PLC file |
|
327 |
open(os.path.join(self.workingdir,NewFileName), |
|
328 |
'wb').write(data) |
|
329 |
||
330 |
# Store new PLC filename based on md5 key |
|
331 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
332 |
||
333 |
# Then write the files |
|
334 |
log = file(extra_files_log, "w") |
|
335 |
for fname,fdata in extrafiles: |
|
336 |
fpath = os.path.join(self.workingdir,fname) |
|
337 |
open(fpath, "wb").write(fdata) |
|
338 |
log.write(fname+'\n') |
|
339 |
||
340 |
# Store new PLC filename |
|
341 |
self.CurrentPLCFilename = NewFileName |
|
342 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
343 |
PLCprint(traceback.format_exc()) |
229 | 344 |
return False |
345 |
if self.PLCStatus == "Empty": |
|
346 |
self.PLCStatus = "Stopped" |
|
347 |
return True |
|
348 |
return False |
|
349 |
||
350 |
def MatchMD5(self, MD5): |
|
351 |
try: |
|
352 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
353 |
return last_md5 == MD5 |
|
354 |
except: |
|
355 |
return False |
|
356 |
||
357 |
def SetTraceVariablesList(self, idxs): |
|
358 |
""" |
|
359 |
Call ctype imported function to append |
|
360 |
these indexes to registred variables in PLC debugger |
|
361 |
""" |
|
235 | 362 |
self._suspendDebug() |
229 | 363 |
# keep a copy of requested idx |
364 |
self._Idxs = idxs[:] |
|
365 |
self._ResetDebugVariables() |
|
366 |
for idx in idxs: |
|
367 |
self._RegisterDebugVariable(idx) |
|
235 | 368 |
self._resumeDebug() |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
369 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
370 |
class IEC_STRING(ctypes.Structure): |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
371 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
372 |
Must be changed according to changes in iec_types.h |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
373 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
374 |
_fields_ = [("len", ctypes.c_uint8), |
302 | 375 |
("body", ctypes.c_char * 127)] |
229 | 376 |
|
238 | 377 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
378 |
"STEP" : (ctypes.c_uint8, lambda x:x.value), |
|
379 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
|
380 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value), |
|
381 |
"SINT" : (ctypes.c_int8, lambda x:x.value), |
|
382 |
"USINT" : (ctypes.c_uint8, lambda x:x.value), |
|
383 |
"BYTE" : (ctypes.c_uint8, lambda x:x.value), |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
384 |
"STRING" : (IEC_STRING, lambda x:x.body[:x.len]), |
238 | 385 |
"INT" : (ctypes.c_int16, lambda x:x.value), |
386 |
"UINT" : (ctypes.c_uint16, lambda x:x.value), |
|
387 |
"WORD" : (ctypes.c_uint16, lambda x:x.value), |
|
388 |
"WSTRING" : (None, None),#TODO |
|
389 |
"DINT" : (ctypes.c_int32, lambda x:x.value), |
|
390 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value), |
|
391 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value), |
|
392 |
"LINT" : (ctypes.c_int64, lambda x:x.value), |
|
393 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value), |
|
394 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value), |
|
395 |
"REAL" : (ctypes.c_float, lambda x:x.value), |
|
396 |
"LREAL" : (ctypes.c_double, lambda x:x.value), |
|
229 | 397 |
} |
398 |
||
399 |
def GetTraceVariables(self): |
|
400 |
""" |
|
339 | 401 |
Return a list of variables, corresponding to the list of required idx |
229 | 402 |
""" |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
403 |
if self.PLCStatus == "Started": |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
404 |
tick = self._WaitDebugData() |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
405 |
if tick == -1: |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
406 |
res = None |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
407 |
else: |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
408 |
idx = ctypes.c_int() |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
409 |
typename = ctypes.c_char_p() |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
410 |
res = [] |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
411 |
|
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
412 |
for given_idx in self._Idxs: |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
413 |
buffer=self._IterDebugData(ctypes.byref(idx), ctypes.byref(typename)) |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
414 |
c_type,unpack_func = self.TypeTranslator.get(typename.value, (None,None)) |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
415 |
if c_type is not None and given_idx == idx.value: |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
416 |
res.append(unpack_func(ctypes.cast(buffer, |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
417 |
ctypes.POINTER(c_type)).contents)) |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
418 |
else: |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
419 |
PLCprint("Debug error idx : %d, expected_idx %d, type : %s"%(idx.value, given_idx,typename.value)) |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
420 |
res.append(None) |
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
421 |
time.sleep(0.1) |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
422 |
self._FreeDebugData() |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
423 |
return tick, res |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
424 |
return -1, None |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
425 |
|
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
426 |
|
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
427 |