author | Laurent Bessard |
Thu, 09 May 2013 17:52:51 +0200 | |
changeset 1105 | f0e5b475a074 |
parent 1093 | b5f78cff4459 |
child 1121 | d3838e8f1b90 |
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 |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
28 |
from targets.typemapping import LogLevelsDefault, LogLevelsCount, TypeTranslator, UnpackDebugBuffer |
917 | 29 |
|
301 | 30 |
|
229 | 31 |
if os.name in ("nt", "ce"): |
32 |
from _ctypes import LoadLibrary as dlopen |
|
33 |
from _ctypes import FreeLibrary as dlclose |
|
34 |
elif os.name == "posix": |
|
35 |
from _ctypes import dlopen, dlclose |
|
36 |
||
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
37 |
import traceback |
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
38 |
def get_last_traceback(tb): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
39 |
while tb.tb_next: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
40 |
tb = tb.tb_next |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
41 |
return tb |
229 | 42 |
|
43 |
lib_ext ={ |
|
44 |
"linux2":".so", |
|
45 |
"win32":".dll", |
|
46 |
}.get(sys.platform, "") |
|
47 |
||
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
48 |
def PLCprint(message): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
49 |
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
|
50 |
sys.stdout.flush() |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
51 |
|
229 | 52 |
class PLCObject(pyro.ObjBase): |
235 | 53 |
_Idxs = [] |
368 | 54 |
def __init__(self, workingdir, daemon, argv, statuschange, evaluator, website): |
229 | 55 |
pyro.ObjBase.__init__(self) |
301 | 56 |
self.evaluator = evaluator |
229 | 57 |
self.argv = [workingdir] + argv # force argv[0] to be "path" to exec... |
58 |
self.workingdir = workingdir |
|
59 |
self.PLCStatus = "Stopped" |
|
60 |
self.PLClibraryHandle = None |
|
352 | 61 |
self.PLClibraryLock = Lock() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
62 |
self.DummyIteratorLock = None |
229 | 63 |
# Creates fake C funcs proxies |
64 |
self._FreePLC() |
|
65 |
self.daemon = daemon |
|
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
66 |
self.statuschange = statuschange |
301 | 67 |
self.hmi_frame = None |
368 | 68 |
self.website = website |
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
69 |
self._loading_error = None |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
70 |
self.python_runtime_vars = None |
229 | 71 |
|
72 |
# Get the last transfered PLC if connector must be restart |
|
73 |
try: |
|
74 |
self.CurrentPLCFilename=open( |
|
75 |
self._GetMD5FileName(), |
|
76 |
"r").read().strip() + lib_ext |
|
77 |
except Exception, e: |
|
78 |
self.PLCStatus = "Empty" |
|
79 |
self.CurrentPLCFilename=None |
|
80 |
||
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
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
|
917 | 85 |
def LogMessage(self, *args): |
86 |
if len(args) == 2: |
|
87 |
level, msg = args |
|
88 |
else: |
|
89 |
level = LogLevelsDefault |
|
90 |
msg, = args |
|
91 |
return self._LogMessage(level, msg, len(msg)) |
|
92 |
||
1093 | 93 |
def ResetLogCount(self): |
94 |
if self._ResetLogCount is not None: |
|
95 |
self._ResetLogCount() |
|
917 | 96 |
|
97 |
def GetLogCount(self, level): |
|
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
98 |
if self._GetLogCount is not None : |
917 | 99 |
return int(self._GetLogCount(level)) |
100 |
elif self._loading_error is not None and level==0: |
|
1093 | 101 |
return 1 |
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
102 |
|
917 | 103 |
def GetLogMessage(self, level, msgid): |
921 | 104 |
tick = ctypes.c_uint32() |
105 |
tv_sec = ctypes.c_uint32() |
|
106 |
tv_nsec = ctypes.c_uint32() |
|
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
107 |
if self._GetLogMessage is not None: |
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
108 |
maxsz = len(self._log_read_buffer)-1 |
921 | 109 |
sz = self._GetLogMessage(level, msgid, |
110 |
self._log_read_buffer, maxsz, |
|
111 |
ctypes.byref(tick), |
|
112 |
ctypes.byref(tv_sec), |
|
113 |
ctypes.byref(tv_nsec)) |
|
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
114 |
if sz and sz <= maxsz: |
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
115 |
self._log_read_buffer[sz] = '\x00' |
921 | 116 |
return self._log_read_buffer.value,tick.value,tv_sec.value,tv_nsec.value |
917 | 117 |
elif self._loading_error is not None and level==0: |
921 | 118 |
return self._loading_error,0,0,0 |
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
119 |
return None |
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
120 |
|
229 | 121 |
def _GetMD5FileName(self): |
122 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
123 |
||
124 |
def _GetLibFileName(self): |
|
125 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
126 |
||
127 |
||
1027
4e44c2c3e081
Fixed bug when starting Beremiz_runtime.py non empty (-a)
Edouard Tisserant
parents:
1014
diff
changeset
|
128 |
def LoadPLC(self): |
229 | 129 |
""" |
130 |
Load PLC library |
|
131 |
Declare all functions, arguments and return values |
|
132 |
""" |
|
133 |
try: |
|
134 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
135 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
136 |
||
137 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
138 |
self._startPLC.restype = ctypes.c_int |
|
139 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
140 |
||
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
141 |
self._stopPLC_real = self.PLClibraryHandle.stopPLC |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
142 |
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
|
143 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
144 |
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
|
145 |
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
|
146 |
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
|
147 |
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
|
148 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
149 |
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
|
150 |
else: |
717 | 151 |
# 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
|
152 |
# 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
|
153 |
self.PythonIteratorLock = Lock() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
154 |
self.PythonIteratorLock.acquire() |
868
7e5da4962bea
Fix bug of PythonIterator signature in PLCObject when not using PythonLibrary
Edouard Tisserant
parents:
867
diff
changeset
|
155 |
def PythonIterator(res, blkid): |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
156 |
self.PythonIteratorLock.acquire() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
157 |
self.PythonIteratorLock.release() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
158 |
return None |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
159 |
self._PythonIterator = PythonIterator |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
160 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
161 |
def __StopPLC(): |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
162 |
self._stopPLC_real() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
163 |
self.PythonIteratorLock.release() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
164 |
self._stopPLC = __StopPLC |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
165 |
|
229 | 166 |
|
167 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
168 |
self._ResetDebugVariables.restype = None |
|
169 |
||
235 | 170 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 171 |
self._RegisterDebugVariable.restype = None |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
172 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int, ctypes.c_void_p] |
229 | 173 |
|
174 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
175 |
self._FreeDebugData.restype = None |
|
176 |
||
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
177 |
self._GetDebugData = self.PLClibraryHandle.GetDebugData |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
178 |
self._GetDebugData.restype = ctypes.c_int |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
179 |
self._GetDebugData.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p)] |
235 | 180 |
|
181 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
614 | 182 |
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
|
183 |
self._suspendDebug.argtypes = [ctypes.c_int] |
235 | 184 |
|
185 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
186 |
self._resumeDebug.restype = None |
|
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
187 |
|
1093 | 188 |
self._ResetLogCount = self.PLClibraryHandle.ResetLogCount |
189 |
self._ResetLogCount.restype = None |
|
190 |
||
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
191 |
self._GetLogCount = self.PLClibraryHandle.GetLogCount |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
192 |
self._GetLogCount.restype = ctypes.c_uint32 |
917 | 193 |
self._GetLogCount.argtypes = [ctypes.c_uint8] |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
194 |
|
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
195 |
self._LogMessage = self.PLClibraryHandle.LogMessage |
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
196 |
self._LogMessage.restype = ctypes.c_int |
971
c4550f76ae05
reverted PLCObject.py. ctypes.POINTER(ctypes.c_uint8) != string
Edouard Tisserant
parents:
969
diff
changeset
|
197 |
self._LogMessage.argtypes = [ctypes.c_uint8, ctypes.c_char_p, ctypes.c_uint32] |
1093 | 198 |
|
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
199 |
self._log_read_buffer = ctypes.create_string_buffer(1<<14) #16K |
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
200 |
self._GetLogMessage = self.PLClibraryHandle.GetLogMessage |
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
201 |
self._GetLogMessage.restype = ctypes.c_uint32 |
921 | 202 |
self._GetLogMessage.argtypes = [ctypes.c_uint8, ctypes.c_uint32, ctypes.c_char_p, ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32)] |
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
203 |
|
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
204 |
self._loading_error = None |
1035
0f905e027d18
Better mdns resolution failure signaling, added fixed bug whith runtime autostart
Edouard Tisserant
parents:
1027
diff
changeset
|
205 |
|
0f905e027d18
Better mdns resolution failure signaling, added fixed bug whith runtime autostart
Edouard Tisserant
parents:
1027
diff
changeset
|
206 |
self.PythonRuntimeInit() |
0f905e027d18
Better mdns resolution failure signaling, added fixed bug whith runtime autostart
Edouard Tisserant
parents:
1027
diff
changeset
|
207 |
|
229 | 208 |
return True |
209 |
except: |
|
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
210 |
self._loading_error = traceback.format_exc() |
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
211 |
PLCprint(self._loading_error) |
229 | 212 |
return False |
213 |
||
1045
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
214 |
def UnLoadPLC(self): |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
215 |
self.PythonRuntimeCleanup() |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
216 |
self._FreePLC() |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
217 |
|
229 | 218 |
def _FreePLC(self): |
219 |
""" |
|
220 |
Unload PLC library. |
|
221 |
This is also called by __init__ to create dummy C func proxies |
|
222 |
""" |
|
352 | 223 |
self.PLClibraryLock.acquire() |
229 | 224 |
# Forget all refs to library |
1027
4e44c2c3e081
Fixed bug when starting Beremiz_runtime.py non empty (-a)
Edouard Tisserant
parents:
1014
diff
changeset
|
225 |
self._startPLC = lambda x,y:None |
229 | 226 |
self._stopPLC = lambda:None |
227 |
self._ResetDebugVariables = lambda:None |
|
479
c28f40b27798
Bug on RegisterDebugVariable when no PLC running fixed
laurent
parents:
477
diff
changeset
|
228 |
self._RegisterDebugVariable = lambda x, y:None |
229 | 229 |
self._IterDebugData = lambda x,y:None |
230 |
self._FreeDebugData = lambda:None |
|
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
231 |
self._GetDebugData = lambda:-1 |
614 | 232 |
self._suspendDebug = lambda x:-1 |
235 | 233 |
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
|
234 |
self._PythonIterator = lambda:"" |
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
235 |
self._GetLogCount = None |
917 | 236 |
self._LogMessage = lambda l,m,s:PLCprint("OFF LOG :"+m) |
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
237 |
self._GetLogMessage = None |
229 | 238 |
self.PLClibraryHandle = None |
239 |
# Unload library explicitely |
|
240 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
241 |
dlclose(self._PLClibraryHandle) |
|
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
242 |
self._PLClibraryHandle = None |
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
243 |
|
352 | 244 |
self.PLClibraryLock.release() |
229 | 245 |
return False |
246 |
||
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
247 |
def PythonRuntimeCall(self, methodname): |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
248 |
""" |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
249 |
Calls init, start, stop or cleanup method provided by |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
250 |
runtime python files, loaded when new PLC uploaded |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
251 |
""" |
1051
847d68c3e7ff
Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents:
1045
diff
changeset
|
252 |
for method in self.python_runtime_vars.get("_runtime_%s"%methodname, []): |
847d68c3e7ff
Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents:
1045
diff
changeset
|
253 |
res,exp = self.evaluator(method) |
847d68c3e7ff
Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents:
1045
diff
changeset
|
254 |
if exp is not None: |
1052
fa7c5034c1d2
Better display of Python exceptions from Py_Eval
Edouard Tisserant
parents:
1051
diff
changeset
|
255 |
self.LogMessage(0,'\n'.join(traceback.format_exception(*exp))) |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
256 |
|
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
257 |
def PythonRuntimeInit(self): |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
258 |
MethodNames = ["init", "start", "stop", "cleanup"] |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
259 |
self.python_runtime_vars = globals().copy() |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
260 |
self.python_runtime_vars["WorkingDir"] = self.workingdir |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
261 |
self.python_runtime_vars["website"] = self.website |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
262 |
for methodname in MethodNames : |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
263 |
self.python_runtime_vars["_runtime_%s"%methodname] = [] |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
264 |
self.python_runtime_vars["PLCObject"] = self |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
265 |
self.python_runtime_vars["PLCBinary"] = self.PLClibraryHandle |
368 | 266 |
|
972 | 267 |
try: |
268 |
for filename in os.listdir(self.workingdir): |
|
269 |
name, ext = os.path.splitext(filename) |
|
270 |
if name.upper().startswith("RUNTIME") and ext.upper() == ".PY": |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
271 |
execfile(os.path.join(self.workingdir, filename), self.python_runtime_vars) |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
272 |
for methodname in MethodNames: |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
273 |
method = self.python_runtime_vars.get("_%s_%s" % (name, methodname), None) |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
274 |
if method is not None: |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
275 |
self.python_runtime_vars["_runtime_%s"%methodname].append(method) |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
276 |
|
972 | 277 |
except: |
278 |
self.LogMessage(0,traceback.format_exc()) |
|
279 |
raise |
|
368 | 280 |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
281 |
self.PythonRuntimeCall("init") |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
282 |
|
368 | 283 |
if self.website is not None: |
284 |
self.website.PLCStarted() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
285 |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
286 |
|
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
287 |
def PythonRuntimeCleanup(self): |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
288 |
if self.python_runtime_vars is not None: |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
289 |
self.PythonRuntimeCall("cleanup") |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
290 |
|
368 | 291 |
if self.website is not None: |
292 |
self.website.PLCStopped() |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
293 |
|
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
294 |
self.python_runtime_vars = None |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
295 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
296 |
def PythonThreadProc(self): |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
297 |
self.PLCStatus = "Started" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
298 |
self.StatusChange() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
299 |
self.StartSem.release() |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
300 |
self.PythonRuntimeCall("start") |
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
|
301 |
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
|
302 |
compile_cache={} |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
303 |
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
|
304 |
# 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
|
305 |
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
|
306 |
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
|
307 |
# print " -> ", cmd, blkid |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
308 |
if cmd is None: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
309 |
break |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
310 |
try : |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
311 |
self.python_runtime_vars["FBID"]=FBID |
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
|
312 |
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
|
313 |
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
|
314 |
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
|
315 |
compile_cache[FBID]=(cmd,AST) |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
316 |
result,exp = self.evaluator(eval,cmd,self.python_runtime_vars) |
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
|
317 |
if exp is not None: |
1052
fa7c5034c1d2
Better display of Python exceptions from Py_Eval
Edouard Tisserant
parents:
1051
diff
changeset
|
318 |
res = "#EXCEPTION : "+str(exp[1]) |
fa7c5034c1d2
Better display of Python exceptions from Py_Eval
Edouard Tisserant
parents:
1051
diff
changeset
|
319 |
self.LogMessage(1,('PyEval@0x%x(Code="%s") Exception "%s"')%(FBID,cmd, |
fa7c5034c1d2
Better display of Python exceptions from Py_Eval
Edouard Tisserant
parents:
1051
diff
changeset
|
320 |
'\n'.join(traceback.format_exception(*exp)))) |
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
|
321 |
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
|
322 |
res=str(result) |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
323 |
self.python_runtime_vars["FBID"]=None |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
324 |
except Exception,e: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
325 |
res = "#EXCEPTION : "+str(e) |
972 | 326 |
self.LogMessage(1,('PyEval@0x%x(Code="%s") Exception "%s"')%(FBID,cmd,str(e))) |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
327 |
self.PLCStatus = "Stopped" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
328 |
self.StatusChange() |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
329 |
self.PythonRuntimeCall("stop") |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
330 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
331 |
def StartPLC(self): |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
332 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
333 |
c_argv = ctypes.c_char_p * len(self.argv) |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
334 |
error = None |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
335 |
res = self._startPLC(len(self.argv),c_argv(*self.argv)) |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
336 |
if res == 0: |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
337 |
self.StartSem=Semaphore(0) |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
338 |
self.PythonThread = Thread(target=self.PythonThreadProc) |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
339 |
self.PythonThread.start() |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
340 |
self.StartSem.acquire() |
917 | 341 |
self.LogMessage("PLC started") |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
342 |
else: |
972 | 343 |
self.LogMessage(0,_("Problem starting PLC : error %d" % res)) |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
344 |
self.PLCStatus = "Broken" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
345 |
self.StatusChange() |
352 | 346 |
|
229 | 347 |
def StopPLC(self): |
348 |
if self.PLCStatus == "Started": |
|
917 | 349 |
self.LogMessage("PLC stopped") |
352 | 350 |
self._stopPLC() |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
351 |
self.PythonThread.join() |
229 | 352 |
return True |
353 |
return False |
|
354 |
||
355 |
def _Reload(self): |
|
356 |
self.daemon.shutdown(True) |
|
357 |
self.daemon.sock.close() |
|
358 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
359 |
# never reached |
|
360 |
return 0 |
|
361 |
||
362 |
def ForceReload(self): |
|
363 |
# respawn python interpreter |
|
364 |
Timer(0.1,self._Reload).start() |
|
365 |
return True |
|
366 |
||
367 |
def GetPLCstatus(self): |
|
917 | 368 |
return self.PLCStatus, map(self.GetLogCount,xrange(LogLevelsCount)) |
229 | 369 |
|
370 |
def NewPLC(self, md5sum, data, extrafiles): |
|
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
371 |
if self.PLCStatus in ["Stopped", "Empty", "Broken"]: |
229 | 372 |
NewFileName = md5sum + lib_ext |
373 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
374 |
|
1045
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
375 |
self.UnLoadPLC() |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
376 |
|
1011
388777d307de
Fixed bug when transferring New PLC to a non-empty pyro connector
Laurent Bessard
parents:
972
diff
changeset
|
377 |
self.LogMessage("NewPLC (%s)"%md5sum) |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
378 |
self.PLCStatus = "Empty" |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
379 |
|
229 | 380 |
try: |
381 |
os.remove(os.path.join(self.workingdir, |
|
382 |
self.CurrentPLCFilename)) |
|
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
383 |
for filename in file(extra_files_log, "r").readlines() + [extra_files_log]: |
229 | 384 |
try: |
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
385 |
os.remove(os.path.join(self.workingdir, filename.strip())) |
229 | 386 |
except: |
387 |
pass |
|
388 |
except: |
|
389 |
pass |
|
390 |
||
391 |
try: |
|
392 |
# Create new PLC file |
|
393 |
open(os.path.join(self.workingdir,NewFileName), |
|
394 |
'wb').write(data) |
|
395 |
||
396 |
# Store new PLC filename based on md5 key |
|
397 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
398 |
||
399 |
# Then write the files |
|
400 |
log = file(extra_files_log, "w") |
|
401 |
for fname,fdata in extrafiles: |
|
402 |
fpath = os.path.join(self.workingdir,fname) |
|
403 |
open(fpath, "wb").write(fdata) |
|
404 |
log.write(fname+'\n') |
|
405 |
||
406 |
# Store new PLC filename |
|
407 |
self.CurrentPLCFilename = NewFileName |
|
408 |
except: |
|
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
409 |
self.PLCStatus = "Broken" |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
410 |
self.StatusChange() |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
411 |
PLCprint(traceback.format_exc()) |
229 | 412 |
return False |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
413 |
|
1027
4e44c2c3e081
Fixed bug when starting Beremiz_runtime.py non empty (-a)
Edouard Tisserant
parents:
1014
diff
changeset
|
414 |
if self.LoadPLC(): |
229 | 415 |
self.PLCStatus = "Stopped" |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
416 |
else: |
1035
0f905e027d18
Better mdns resolution failure signaling, added fixed bug whith runtime autostart
Edouard Tisserant
parents:
1027
diff
changeset
|
417 |
self.PLCStatus = "Broken" |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
418 |
self._FreePLC() |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
419 |
self.StatusChange() |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
420 |
|
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
421 |
return self.PLCStatus == "Stopped" |
229 | 422 |
return False |
423 |
||
424 |
def MatchMD5(self, MD5): |
|
425 |
try: |
|
426 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
427 |
return last_md5 == MD5 |
|
428 |
except: |
|
429 |
return False |
|
430 |
||
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
431 |
|
592 | 432 |
|
229 | 433 |
def SetTraceVariablesList(self, idxs): |
434 |
""" |
|
435 |
Call ctype imported function to append |
|
436 |
these indexes to registred variables in PLC debugger |
|
437 |
""" |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
438 |
if idxs: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
439 |
# suspend but dont disable |
614 | 440 |
if self._suspendDebug(False) == 0: |
441 |
# keep a copy of requested idx |
|
442 |
self._Idxs = idxs[:] |
|
443 |
self._ResetDebugVariables() |
|
444 |
for idx,iectype,force in idxs: |
|
445 |
if force !=None: |
|
446 |
c_type,unpack_func, pack_func = \ |
|
447 |
TypeTranslator.get(iectype, |
|
448 |
(None,None,None)) |
|
449 |
force = ctypes.byref(pack_func(c_type,force)) |
|
450 |
self._RegisterDebugVariable(idx, force) |
|
451 |
self._resumeDebug() |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
452 |
else: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
453 |
self._suspendDebug(True) |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
454 |
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
|
455 |
|
229 | 456 |
def GetTraceVariables(self): |
457 |
""" |
|
339 | 458 |
Return a list of variables, corresponding to the list of required idx |
229 | 459 |
""" |
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
|
460 |
if self.PLCStatus == "Started": |
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
461 |
tick = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
462 |
size = ctypes.c_uint32() |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
463 |
buff = ctypes.c_void_p() |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
464 |
TraceVariables = None |
795
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
465 |
if self.PLClibraryLock.acquire(False): |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
466 |
if self._GetDebugData(ctypes.byref(tick), |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
467 |
ctypes.byref(size), |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
468 |
ctypes.byref(buff)) == 0: |
795
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
469 |
if size.value: |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
470 |
TraceVariables = UnpackDebugBuffer(buff, size.value, self._Idxs) |
795
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
471 |
self._FreeDebugData() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
472 |
self.PLClibraryLock.release() |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
473 |
if TraceVariables is not None: |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
474 |
return self.PLCStatus, tick.value, TraceVariables |
917 | 475 |
return self.PLCStatus, None, [] |
592 | 476 |
|
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
477 |
def RemoteExec(self, script, **kwargs): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
478 |
try: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
479 |
exec script in kwargs |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
480 |
except: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
481 |
e_type, e_value, e_traceback = sys.exc_info() |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
482 |
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
|
483 |
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
|
484 |
(line_no, e_value, script.splitlines()[line_no - 1])) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
485 |
return (0, kwargs.get("returnVal", None)) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
486 |
|
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
487 |