author | Laurent Bessard |
Tue, 15 Jan 2013 10:57:06 +0100 | |
changeset 902 | ffa8ee5ee2fe |
parent 868 | 7e5da4962bea |
child 906 | de452d65865c |
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 |
|
690
ef60d7e188e6
Added a semaphore when starting runtime's python thread to make sure startPLC doesn't return before PLC is really initialized.
Edouard Tisserant
parents:
614
diff
changeset
|
26 |
from threading import Timer, Thread, Lock, Semaphore |
301 | 27 |
import ctypes, os, commands, types, sys |
592 | 28 |
from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator |
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 |
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
37 |
def get_last_traceback(tb): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
38 |
while tb.tb_next: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
39 |
tb = tb.tb_next |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
40 |
return tb |
229 | 41 |
|
42 |
lib_ext ={ |
|
43 |
"linux2":".so", |
|
44 |
"win32":".dll", |
|
45 |
}.get(sys.platform, "") |
|
46 |
||
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
47 |
def PLCprint(message): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
48 |
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
|
49 |
sys.stdout.flush() |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
50 |
|
229 | 51 |
class PLCObject(pyro.ObjBase): |
235 | 52 |
_Idxs = [] |
368 | 53 |
def __init__(self, workingdir, daemon, argv, statuschange, evaluator, website): |
229 | 54 |
pyro.ObjBase.__init__(self) |
301 | 55 |
self.evaluator = evaluator |
229 | 56 |
self.argv = [workingdir] + argv # force argv[0] to be "path" to exec... |
57 |
self.workingdir = workingdir |
|
58 |
self.PLCStatus = "Stopped" |
|
59 |
self.PLClibraryHandle = None |
|
352 | 60 |
self.PLClibraryLock = Lock() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
61 |
self.DummyIteratorLock = None |
229 | 62 |
# Creates fake C funcs proxies |
63 |
self._FreePLC() |
|
64 |
self.daemon = daemon |
|
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
65 |
self.statuschange = statuschange |
301 | 66 |
self.hmi_frame = None |
368 | 67 |
self.website = website |
229 | 68 |
|
69 |
# Get the last transfered PLC if connector must be restart |
|
70 |
try: |
|
71 |
self.CurrentPLCFilename=open( |
|
72 |
self._GetMD5FileName(), |
|
73 |
"r").read().strip() + lib_ext |
|
74 |
except Exception, e: |
|
75 |
self.PLCStatus = "Empty" |
|
76 |
self.CurrentPLCFilename=None |
|
77 |
||
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
|
229 | 82 |
def _GetMD5FileName(self): |
83 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
84 |
||
85 |
def _GetLibFileName(self): |
|
86 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
87 |
||
88 |
||
89 |
def _LoadNewPLC(self): |
|
90 |
""" |
|
91 |
Load PLC library |
|
92 |
Declare all functions, arguments and return values |
|
93 |
""" |
|
94 |
try: |
|
95 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
96 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
97 |
||
98 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
99 |
self._startPLC.restype = ctypes.c_int |
|
100 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
101 |
||
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
102 |
self._stopPLC_real = self.PLClibraryHandle.stopPLC |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
103 |
self._stopPLC_real.restype = None |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
104 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
105 |
self._PythonIterator = getattr(self.PLClibraryHandle, "PythonIterator", None) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
106 |
if self._PythonIterator is not None: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
107 |
self._PythonIterator.restype = ctypes.c_char_p |
851
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
108 |
self._PythonIterator.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_void_p)] |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
109 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
110 |
self._stopPLC = self._stopPLC_real |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
111 |
else: |
717 | 112 |
# If python confnode is not enabled, we reuse _PythonIterator |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
113 |
# as a call that block pythonthread until StopPLC |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
114 |
self.PythonIteratorLock = Lock() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
115 |
self.PythonIteratorLock.acquire() |
868
7e5da4962bea
Fix bug of PythonIterator signature in PLCObject when not using PythonLibrary
Edouard Tisserant
parents:
867
diff
changeset
|
116 |
def PythonIterator(res, blkid): |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
117 |
self.PythonIteratorLock.acquire() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
118 |
self.PythonIteratorLock.release() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
119 |
return None |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
120 |
self._PythonIterator = PythonIterator |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
121 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
122 |
def __StopPLC(): |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
123 |
self._stopPLC_real() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
124 |
self.PythonIteratorLock.release() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
125 |
self._stopPLC = __StopPLC |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
126 |
|
229 | 127 |
|
128 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
129 |
self._ResetDebugVariables.restype = None |
|
130 |
||
235 | 131 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 132 |
self._RegisterDebugVariable.restype = None |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
133 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int, ctypes.c_void_p] |
229 | 134 |
|
135 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
136 |
self._FreeDebugData.restype = None |
|
137 |
||
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
138 |
self._GetDebugData = self.PLClibraryHandle.GetDebugData |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
139 |
self._GetDebugData.restype = ctypes.c_int |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
140 |
self._GetDebugData.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p)] |
235 | 141 |
|
142 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
614 | 143 |
self._suspendDebug.restype = ctypes.c_int |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
144 |
self._suspendDebug.argtypes = [ctypes.c_int] |
235 | 145 |
|
146 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
147 |
self._resumeDebug.restype = None |
|
148 |
||
229 | 149 |
return True |
150 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
151 |
PLCprint(traceback.format_exc()) |
229 | 152 |
return False |
153 |
||
154 |
def _FreePLC(self): |
|
155 |
""" |
|
156 |
Unload PLC library. |
|
157 |
This is also called by __init__ to create dummy C func proxies |
|
158 |
""" |
|
352 | 159 |
self.PLClibraryLock.acquire() |
229 | 160 |
# Forget all refs to library |
161 |
self._startPLC = lambda:None |
|
162 |
self._stopPLC = lambda:None |
|
163 |
self._ResetDebugVariables = lambda:None |
|
479
c28f40b27798
Bug on RegisterDebugVariable when no PLC running fixed
laurent
parents:
477
diff
changeset
|
164 |
self._RegisterDebugVariable = lambda x, y:None |
229 | 165 |
self._IterDebugData = lambda x,y:None |
166 |
self._FreeDebugData = lambda:None |
|
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
167 |
self._GetDebugData = lambda:-1 |
614 | 168 |
self._suspendDebug = lambda x:-1 |
235 | 169 |
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
|
170 |
self._PythonIterator = lambda:"" |
229 | 171 |
self.PLClibraryHandle = None |
172 |
# Unload library explicitely |
|
173 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
174 |
dlclose(self._PLClibraryHandle) |
|
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
175 |
self._PLClibraryHandle = None |
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
176 |
|
352 | 177 |
self.PLClibraryLock.release() |
229 | 178 |
return False |
179 |
||
299 | 180 |
def PrepareRuntimePy(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
181 |
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
|
182 |
self.python_threads_vars["WorkingDir"] = self.workingdir |
368 | 183 |
self.python_threads_vars["website"] = self.website |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
184 |
self.python_threads_vars["_runtime_begin"] = [] |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
185 |
self.python_threads_vars["_runtime_cleanup"] = [] |
734 | 186 |
self.python_threads_vars["PLCObject"] = self |
187 |
self.python_threads_vars["PLCBinary"] = self.PLClibraryHandle |
|
368 | 188 |
|
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
189 |
for filename in os.listdir(self.workingdir): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
190 |
name, ext = os.path.splitext(filename) |
391
9b1801ef99b5
fix runtime.py filename case to avoid problem on multi-platform
greg
parents:
368
diff
changeset
|
191 |
if name.upper().startswith("RUNTIME") and ext.upper() == ".PY": |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
192 |
try: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
193 |
# TODO handle exceptions in runtime.py |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
194 |
# pyfile may redefine _runtime_cleanup |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
195 |
# or even call _PythonThreadProc itself. |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
196 |
execfile(os.path.join(self.workingdir, filename), self.python_threads_vars) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
197 |
except: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
198 |
PLCprint(traceback.format_exc()) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
199 |
runtime_begin = self.python_threads_vars.get("_%s_begin" % name, None) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
200 |
if runtime_begin is not None: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
201 |
self.python_threads_vars["_runtime_begin"].append(runtime_begin) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
202 |
runtime_cleanup = self.python_threads_vars.get("_%s_cleanup" % name, None) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
203 |
if runtime_cleanup is not None: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
204 |
self.python_threads_vars["_runtime_cleanup"].append(runtime_cleanup) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
205 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
206 |
for runtime_begin in self.python_threads_vars.get("_runtime_begin", []): |
329 | 207 |
runtime_begin() |
368 | 208 |
|
209 |
if self.website is not None: |
|
210 |
self.website.PLCStarted() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
211 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
212 |
def FinishRuntimePy(self): |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
213 |
for runtime_cleanup in self.python_threads_vars.get("_runtime_cleanup", []): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
214 |
runtime_cleanup() |
368 | 215 |
if self.website is not None: |
216 |
self.website.PLCStopped() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
217 |
self.python_threads_vars = None |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
218 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
219 |
def PythonThreadProc(self): |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
220 |
self.PLCStatus = "Started" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
221 |
self.StatusChange() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
222 |
self.StartSem.release() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
223 |
self.evaluator(self.PrepareRuntimePy) |
851
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
224 |
res,cmd,blkid = "None","None",ctypes.c_void_p() |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
225 |
compile_cache={} |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
226 |
while True: |
851
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
227 |
# print "_PythonIterator(", res, ")", |
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
228 |
cmd = self._PythonIterator(res,blkid) |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
229 |
FBID = blkid.value |
851
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
230 |
# print " -> ", cmd, blkid |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
231 |
if cmd is None: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
232 |
break |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
233 |
try : |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
234 |
self.python_threads_vars["FBID"]=FBID |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
235 |
ccmd,AST =compile_cache.get(FBID, (None,None)) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
236 |
if ccmd is None or ccmd!=cmd: |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
237 |
AST = compile(cmd, '<plc>', 'eval') |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
238 |
compile_cache[FBID]=(cmd,AST) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
239 |
result,exp = self.evaluator(eval,cmd,self.python_threads_vars) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
240 |
if exp is not None: |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
241 |
raise(exp) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
242 |
else: |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
243 |
res=str(result) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
244 |
self.python_threads_vars["FBID"]=None |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
245 |
except Exception,e: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
246 |
res = "#EXCEPTION : "+str(e) |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
247 |
PLCprint(('*** Python eval EXCEPTION ***\n'+ |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
248 |
'| Function Block ID: %d\n'+ |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
249 |
'| Command : "%s"\n'+ |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
250 |
'| Exception : "%s"')%(FBID,cmd,str(e))) |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
251 |
self.PLCStatus = "Stopped" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
252 |
self.StatusChange() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
253 |
self.evaluator(self.FinishRuntimePy) |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
254 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
255 |
def StartPLC(self): |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
256 |
PLCprint("StartPLC") |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
257 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
258 |
c_argv = ctypes.c_char_p * len(self.argv) |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
259 |
error = None |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
260 |
if self._LoadNewPLC(): |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
261 |
if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
262 |
self.StartSem=Semaphore(0) |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
263 |
self.PythonThread = Thread(target=self.PythonThreadProc) |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
264 |
self.PythonThread.start() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
265 |
self.StartSem.acquire() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
266 |
else: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
267 |
error = "starting" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
268 |
else: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
269 |
error = "loading" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
270 |
if error is not None: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
271 |
PLCprint("Problem %s PLC"%error) |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
272 |
self.PLCStatus = "Broken" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
273 |
self.StatusChange() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
274 |
self._FreePLC() |
352 | 275 |
|
229 | 276 |
def StopPLC(self): |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
277 |
PLCprint("StopPLC") |
229 | 278 |
if self.PLCStatus == "Started": |
352 | 279 |
self._stopPLC() |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
280 |
self.PythonThread.join() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
281 |
self._FreePLC() |
229 | 282 |
return True |
283 |
return False |
|
284 |
||
285 |
def _Reload(self): |
|
286 |
self.daemon.shutdown(True) |
|
287 |
self.daemon.sock.close() |
|
288 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
289 |
# never reached |
|
290 |
return 0 |
|
291 |
||
292 |
def ForceReload(self): |
|
293 |
# respawn python interpreter |
|
294 |
Timer(0.1,self._Reload).start() |
|
295 |
return True |
|
296 |
||
297 |
def GetPLCstatus(self): |
|
298 |
return self.PLCStatus |
|
299 |
||
300 |
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
|
301 |
PLCprint("NewPLC (%s)"%md5sum) |
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
302 |
if self.PLCStatus in ["Stopped", "Empty", "Broken"]: |
229 | 303 |
NewFileName = md5sum + lib_ext |
304 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
305 |
try: |
|
306 |
os.remove(os.path.join(self.workingdir, |
|
307 |
self.CurrentPLCFilename)) |
|
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
308 |
for filename in file(extra_files_log, "r").readlines() + [extra_files_log]: |
229 | 309 |
try: |
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
310 |
os.remove(os.path.join(self.workingdir, filename.strip())) |
229 | 311 |
except: |
312 |
pass |
|
313 |
except: |
|
314 |
pass |
|
315 |
||
316 |
try: |
|
317 |
# Create new PLC file |
|
318 |
open(os.path.join(self.workingdir,NewFileName), |
|
319 |
'wb').write(data) |
|
320 |
||
321 |
# Store new PLC filename based on md5 key |
|
322 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
323 |
||
324 |
# Then write the files |
|
325 |
log = file(extra_files_log, "w") |
|
326 |
for fname,fdata in extrafiles: |
|
327 |
fpath = os.path.join(self.workingdir,fname) |
|
328 |
open(fpath, "wb").write(fdata) |
|
329 |
log.write(fname+'\n') |
|
330 |
||
331 |
# Store new PLC filename |
|
332 |
self.CurrentPLCFilename = NewFileName |
|
333 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
334 |
PLCprint(traceback.format_exc()) |
229 | 335 |
return False |
336 |
if self.PLCStatus == "Empty": |
|
337 |
self.PLCStatus = "Stopped" |
|
338 |
return True |
|
339 |
return False |
|
340 |
||
341 |
def MatchMD5(self, MD5): |
|
342 |
try: |
|
343 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
344 |
return last_md5 == MD5 |
|
345 |
except: |
|
346 |
return False |
|
347 |
||
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
348 |
|
592 | 349 |
|
229 | 350 |
def SetTraceVariablesList(self, idxs): |
351 |
""" |
|
352 |
Call ctype imported function to append |
|
353 |
these indexes to registred variables in PLC debugger |
|
354 |
""" |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
355 |
if idxs: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
356 |
# suspend but dont disable |
614 | 357 |
if self._suspendDebug(False) == 0: |
358 |
# keep a copy of requested idx |
|
359 |
self._Idxs = idxs[:] |
|
360 |
self._ResetDebugVariables() |
|
361 |
for idx,iectype,force in idxs: |
|
362 |
if force !=None: |
|
363 |
c_type,unpack_func, pack_func = \ |
|
364 |
TypeTranslator.get(iectype, |
|
365 |
(None,None,None)) |
|
366 |
force = ctypes.byref(pack_func(c_type,force)) |
|
367 |
self._RegisterDebugVariable(idx, force) |
|
368 |
self._resumeDebug() |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
369 |
else: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
370 |
self._suspendDebug(True) |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
371 |
self._Idxs = [] |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
372 |
|
229 | 373 |
def GetTraceVariables(self): |
374 |
""" |
|
339 | 375 |
Return a list of variables, corresponding to the list of required idx |
229 | 376 |
""" |
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
|
377 |
if self.PLCStatus == "Started": |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
378 |
res=[] |
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
379 |
tick = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
380 |
size = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
381 |
buffer = ctypes.c_void_p() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
382 |
offset = 0 |
795
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
383 |
if self.PLClibraryLock.acquire(False): |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
384 |
if self._GetDebugData(ctypes.byref(tick), |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
385 |
ctypes.byref(size), |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
386 |
ctypes.byref(buffer)) == 0: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
387 |
if size.value: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
388 |
for idx, iectype, forced in self._Idxs: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
389 |
cursor = ctypes.c_void_p(buffer.value + offset) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
390 |
c_type,unpack_func, pack_func = \ |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
391 |
TypeTranslator.get(iectype, |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
392 |
(None,None,None)) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
393 |
if c_type is not None and offset < size.value: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
394 |
res.append(unpack_func( |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
395 |
ctypes.cast(cursor, |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
396 |
ctypes.POINTER(c_type)).contents)) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
397 |
offset += ctypes.sizeof(c_type) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
398 |
else: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
399 |
if c_type is None: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
400 |
PLCprint("Debug error - " + iectype + |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
401 |
" not supported !") |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
402 |
#if offset >= size.value: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
403 |
#PLCprint("Debug error - buffer too small ! %d != %d"%(offset, size.value)) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
404 |
break |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
405 |
self._FreeDebugData() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
406 |
self.PLClibraryLock.release() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
407 |
if offset and offset == size.value: |
460
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
408 |
return self.PLCStatus, tick.value, res |
592 | 409 |
#elif size.value: |
410 |
#PLCprint("Debug error - wrong buffer unpack ! %d != %d"%(offset, size.value)) |
|
411 |
return self.PLCStatus, None, [] |
|
412 |
||
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
413 |
def RemoteExec(self, script, **kwargs): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
414 |
try: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
415 |
exec script in kwargs |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
416 |
except: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
417 |
e_type, e_value, e_traceback = sys.exc_info() |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
418 |
line_no = traceback.tb_lineno(get_last_traceback(e_traceback)) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
419 |
return (-1, "RemoteExec script failed!\n\nLine %d: %s\n\t%s" % |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
420 |
(line_no, e_value, script.splitlines()[line_no - 1])) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
421 |
return (0, kwargs.get("returnVal", None)) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
422 |
|
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
423 |