author | Laurent Bessard |
Sun, 24 Feb 2013 00:28:49 +0100 | |
changeset 940 | 0c68d1af821d |
parent 921 | a8db48ec2c31 |
child 956 | c838c50f8946 |
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 |
917 | 28 |
from targets.typemapping import LogLevelsDefault, LogLevelsCount, SameEndianessTypeTranslator as TypeTranslator |
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 |
229 | 70 |
|
71 |
# Get the last transfered PLC if connector must be restart |
|
72 |
try: |
|
73 |
self.CurrentPLCFilename=open( |
|
74 |
self._GetMD5FileName(), |
|
75 |
"r").read().strip() + lib_ext |
|
76 |
except Exception, e: |
|
77 |
self.PLCStatus = "Empty" |
|
78 |
self.CurrentPLCFilename=None |
|
79 |
||
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
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
|
917 | 84 |
def LogMessage(self, *args): |
85 |
if len(args) == 2: |
|
86 |
level, msg = args |
|
87 |
else: |
|
88 |
level = LogLevelsDefault |
|
89 |
msg, = args |
|
90 |
return self._LogMessage(level, msg, len(msg)) |
|
91 |
||
92 |
||
93 |
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
|
94 |
if self._GetLogCount is not None : |
917 | 95 |
return int(self._GetLogCount(level)) |
96 |
elif self._loading_error is not None and level==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
|
97 |
return 1; |
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
|
98 |
|
917 | 99 |
def GetLogMessage(self, level, msgid): |
921 | 100 |
tick = ctypes.c_uint32() |
101 |
tv_sec = ctypes.c_uint32() |
|
102 |
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
|
103 |
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
|
104 |
maxsz = len(self._log_read_buffer)-1 |
921 | 105 |
sz = self._GetLogMessage(level, msgid, |
106 |
self._log_read_buffer, maxsz, |
|
107 |
ctypes.byref(tick), |
|
108 |
ctypes.byref(tv_sec), |
|
109 |
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
|
110 |
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
|
111 |
self._log_read_buffer[sz] = '\x00' |
921 | 112 |
return self._log_read_buffer.value,tick.value,tv_sec.value,tv_nsec.value |
917 | 113 |
elif self._loading_error is not None and level==0: |
921 | 114 |
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
|
115 |
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
|
116 |
|
229 | 117 |
def _GetMD5FileName(self): |
118 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
119 |
||
120 |
def _GetLibFileName(self): |
|
121 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
122 |
||
123 |
||
124 |
def _LoadNewPLC(self): |
|
125 |
""" |
|
126 |
Load PLC library |
|
127 |
Declare all functions, arguments and return values |
|
128 |
""" |
|
129 |
try: |
|
130 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
131 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
132 |
||
133 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
134 |
self._startPLC.restype = ctypes.c_int |
|
135 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
136 |
||
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
137 |
self._stopPLC_real = self.PLClibraryHandle.stopPLC |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
138 |
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
|
139 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
145 |
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
|
146 |
else: |
717 | 147 |
# 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
|
148 |
# 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
|
149 |
self.PythonIteratorLock = Lock() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
150 |
self.PythonIteratorLock.acquire() |
868
7e5da4962bea
Fix bug of PythonIterator signature in PLCObject when not using PythonLibrary
Edouard Tisserant
parents:
867
diff
changeset
|
151 |
def PythonIterator(res, blkid): |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
152 |
self.PythonIteratorLock.acquire() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
153 |
self.PythonIteratorLock.release() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
154 |
return None |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
155 |
self._PythonIterator = PythonIterator |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
156 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
157 |
def __StopPLC(): |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
158 |
self._stopPLC_real() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
159 |
self.PythonIteratorLock.release() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
160 |
self._stopPLC = __StopPLC |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
161 |
|
229 | 162 |
|
163 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
164 |
self._ResetDebugVariables.restype = None |
|
165 |
||
235 | 166 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 167 |
self._RegisterDebugVariable.restype = None |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
168 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int, ctypes.c_void_p] |
229 | 169 |
|
170 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
171 |
self._FreeDebugData.restype = None |
|
172 |
||
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
173 |
self._GetDebugData = self.PLClibraryHandle.GetDebugData |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
174 |
self._GetDebugData.restype = ctypes.c_int |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
175 |
self._GetDebugData.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p)] |
235 | 176 |
|
177 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
614 | 178 |
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
|
179 |
self._suspendDebug.argtypes = [ctypes.c_int] |
235 | 180 |
|
181 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
182 |
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
|
183 |
|
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
|
184 |
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
|
185 |
self._GetLogCount.restype = ctypes.c_uint32 |
917 | 186 |
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
|
187 |
|
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
188 |
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
|
189 |
self._LogMessage.restype = ctypes.c_int |
917 | 190 |
self._LogMessage.argtypes = [ctypes.c_uint8, ctypes.c_char_p, 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
|
191 |
|
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
192 |
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
|
193 |
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
|
194 |
self._GetLogMessage.restype = ctypes.c_uint32 |
921 | 195 |
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
|
196 |
|
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
|
197 |
self._loading_error = None |
229 | 198 |
return True |
199 |
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
|
200 |
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
|
201 |
PLCprint(self._loading_error) |
229 | 202 |
return False |
203 |
||
204 |
def _FreePLC(self): |
|
205 |
""" |
|
206 |
Unload PLC library. |
|
207 |
This is also called by __init__ to create dummy C func proxies |
|
208 |
""" |
|
352 | 209 |
self.PLClibraryLock.acquire() |
229 | 210 |
# Forget all refs to library |
211 |
self._startPLC = lambda:None |
|
212 |
self._stopPLC = lambda:None |
|
213 |
self._ResetDebugVariables = lambda:None |
|
479
c28f40b27798
Bug on RegisterDebugVariable when no PLC running fixed
laurent
parents:
477
diff
changeset
|
214 |
self._RegisterDebugVariable = lambda x, y:None |
229 | 215 |
self._IterDebugData = lambda x,y:None |
216 |
self._FreeDebugData = lambda:None |
|
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
217 |
self._GetDebugData = lambda:-1 |
614 | 218 |
self._suspendDebug = lambda x:-1 |
235 | 219 |
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
|
220 |
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
|
221 |
self._GetLogCount = None |
917 | 222 |
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
|
223 |
self._GetLogMessage = None |
229 | 224 |
self.PLClibraryHandle = None |
225 |
# Unload library explicitely |
|
226 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
227 |
dlclose(self._PLClibraryHandle) |
|
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
228 |
self._PLClibraryHandle = None |
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
229 |
|
352 | 230 |
self.PLClibraryLock.release() |
229 | 231 |
return False |
232 |
||
299 | 233 |
def PrepareRuntimePy(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
234 |
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
|
235 |
self.python_threads_vars["WorkingDir"] = self.workingdir |
368 | 236 |
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
|
237 |
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
|
238 |
self.python_threads_vars["_runtime_cleanup"] = [] |
734 | 239 |
self.python_threads_vars["PLCObject"] = self |
240 |
self.python_threads_vars["PLCBinary"] = self.PLClibraryHandle |
|
368 | 241 |
|
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
242 |
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
|
243 |
name, ext = os.path.splitext(filename) |
391
9b1801ef99b5
fix runtime.py filename case to avoid problem on multi-platform
greg
parents:
368
diff
changeset
|
244 |
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
|
245 |
try: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
246 |
# 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
|
247 |
# pyfile may redefine _runtime_cleanup |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
248 |
# or even call _PythonThreadProc itself. |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
249 |
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
|
250 |
except: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
251 |
PLCprint(traceback.format_exc()) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
252 |
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
|
253 |
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
|
254 |
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
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
259 |
for runtime_begin in self.python_threads_vars.get("_runtime_begin", []): |
329 | 260 |
runtime_begin() |
368 | 261 |
|
262 |
if self.website is not None: |
|
263 |
self.website.PLCStarted() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
264 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
265 |
def FinishRuntimePy(self): |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
266 |
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
|
267 |
runtime_cleanup() |
368 | 268 |
if self.website is not None: |
269 |
self.website.PLCStopped() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
270 |
self.python_threads_vars = None |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
271 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
272 |
def PythonThreadProc(self): |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
273 |
self.PLCStatus = "Started" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
274 |
self.StatusChange() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
275 |
self.StartSem.release() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
276 |
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
|
277 |
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
|
278 |
compile_cache={} |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
279 |
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
|
280 |
# 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
|
281 |
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
|
282 |
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
|
283 |
# print " -> ", cmd, blkid |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
284 |
if cmd is None: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
285 |
break |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
286 |
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
|
287 |
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
|
288 |
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
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
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
|
293 |
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
|
294 |
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
|
295 |
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
|
296 |
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
|
297 |
self.python_threads_vars["FBID"]=None |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
298 |
except Exception,e: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
299 |
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
|
300 |
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
|
301 |
'| 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
|
302 |
'| 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
|
303 |
'| Exception : "%s"')%(FBID,cmd,str(e))) |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
304 |
self.PLCStatus = "Stopped" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
305 |
self.StatusChange() |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
306 |
self.evaluator(self.FinishRuntimePy) |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
307 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
308 |
def StartPLC(self): |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
309 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
310 |
c_argv = ctypes.c_char_p * len(self.argv) |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
311 |
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
|
312 |
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
|
313 |
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
|
314 |
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
|
315 |
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
|
316 |
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
|
317 |
self.StartSem.acquire() |
917 | 318 |
self.LogMessage("PLC started") |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
319 |
else: |
917 | 320 |
self.LogMessage(_("Problem starting PLC : error %d" % res)) |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
321 |
self.PLCStatus = "Broken" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
322 |
self.StatusChange() |
352 | 323 |
|
229 | 324 |
def StopPLC(self): |
325 |
if self.PLCStatus == "Started": |
|
917 | 326 |
self.LogMessage("PLC stopped") |
352 | 327 |
self._stopPLC() |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
328 |
self.PythonThread.join() |
229 | 329 |
return True |
330 |
return False |
|
331 |
||
332 |
def _Reload(self): |
|
333 |
self.daemon.shutdown(True) |
|
334 |
self.daemon.sock.close() |
|
335 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
336 |
# never reached |
|
337 |
return 0 |
|
338 |
||
339 |
def ForceReload(self): |
|
340 |
# respawn python interpreter |
|
341 |
Timer(0.1,self._Reload).start() |
|
342 |
return True |
|
343 |
||
344 |
def GetPLCstatus(self): |
|
917 | 345 |
return self.PLCStatus, map(self.GetLogCount,xrange(LogLevelsCount)) |
229 | 346 |
|
347 |
def NewPLC(self, md5sum, data, extrafiles): |
|
917 | 348 |
self.LogMessage("NewPLC (%s)"%md5sum) |
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
349 |
if self.PLCStatus in ["Stopped", "Empty", "Broken"]: |
229 | 350 |
NewFileName = md5sum + lib_ext |
351 |
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
|
352 |
|
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
|
353 |
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
|
354 |
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
|
355 |
|
229 | 356 |
try: |
357 |
os.remove(os.path.join(self.workingdir, |
|
358 |
self.CurrentPLCFilename)) |
|
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
359 |
for filename in file(extra_files_log, "r").readlines() + [extra_files_log]: |
229 | 360 |
try: |
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
361 |
os.remove(os.path.join(self.workingdir, filename.strip())) |
229 | 362 |
except: |
363 |
pass |
|
364 |
except: |
|
365 |
pass |
|
366 |
||
367 |
try: |
|
368 |
# Create new PLC file |
|
369 |
open(os.path.join(self.workingdir,NewFileName), |
|
370 |
'wb').write(data) |
|
371 |
||
372 |
# Store new PLC filename based on md5 key |
|
373 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
374 |
||
375 |
# Then write the files |
|
376 |
log = file(extra_files_log, "w") |
|
377 |
for fname,fdata in extrafiles: |
|
378 |
fpath = os.path.join(self.workingdir,fname) |
|
379 |
open(fpath, "wb").write(fdata) |
|
380 |
log.write(fname+'\n') |
|
381 |
||
382 |
# Store new PLC filename |
|
383 |
self.CurrentPLCFilename = NewFileName |
|
384 |
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
|
385 |
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
|
386 |
self.StatusChange() |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
387 |
PLCprint(traceback.format_exc()) |
229 | 388 |
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
|
389 |
|
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
|
390 |
if self._LoadNewPLC(): |
229 | 391 |
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
|
392 |
else: |
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
|
393 |
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
|
394 |
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
|
395 |
|
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
|
396 |
return self.PLCStatus == "Stopped" |
229 | 397 |
return False |
398 |
||
399 |
def MatchMD5(self, MD5): |
|
400 |
try: |
|
401 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
402 |
return last_md5 == MD5 |
|
403 |
except: |
|
404 |
return False |
|
405 |
||
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
406 |
|
592 | 407 |
|
229 | 408 |
def SetTraceVariablesList(self, idxs): |
409 |
""" |
|
410 |
Call ctype imported function to append |
|
411 |
these indexes to registred variables in PLC debugger |
|
412 |
""" |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
413 |
if idxs: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
414 |
# suspend but dont disable |
614 | 415 |
if self._suspendDebug(False) == 0: |
416 |
# keep a copy of requested idx |
|
417 |
self._Idxs = idxs[:] |
|
418 |
self._ResetDebugVariables() |
|
419 |
for idx,iectype,force in idxs: |
|
420 |
if force !=None: |
|
421 |
c_type,unpack_func, pack_func = \ |
|
422 |
TypeTranslator.get(iectype, |
|
423 |
(None,None,None)) |
|
424 |
force = ctypes.byref(pack_func(c_type,force)) |
|
425 |
self._RegisterDebugVariable(idx, force) |
|
426 |
self._resumeDebug() |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
427 |
else: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
428 |
self._suspendDebug(True) |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
429 |
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
|
430 |
|
229 | 431 |
def GetTraceVariables(self): |
432 |
""" |
|
339 | 433 |
Return a list of variables, corresponding to the list of required idx |
229 | 434 |
""" |
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
|
435 |
if self.PLCStatus == "Started": |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
436 |
res=[] |
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
437 |
tick = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
438 |
size = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
439 |
buffer = ctypes.c_void_p() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
440 |
offset = 0 |
795
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
441 |
if self.PLClibraryLock.acquire(False): |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
442 |
if self._GetDebugData(ctypes.byref(tick), |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
443 |
ctypes.byref(size), |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
444 |
ctypes.byref(buffer)) == 0: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
445 |
if size.value: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
446 |
for idx, iectype, forced in self._Idxs: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
447 |
cursor = ctypes.c_void_p(buffer.value + offset) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
448 |
c_type,unpack_func, pack_func = \ |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
449 |
TypeTranslator.get(iectype, |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
450 |
(None,None,None)) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
451 |
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
|
452 |
res.append(unpack_func( |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
453 |
ctypes.cast(cursor, |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
454 |
ctypes.POINTER(c_type)).contents)) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
455 |
offset += ctypes.sizeof(c_type) |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
456 |
else: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
457 |
if c_type is None: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
458 |
PLCprint("Debug error - " + iectype + |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
459 |
" not supported !") |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
460 |
#if offset >= size.value: |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
461 |
#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
|
462 |
break |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
463 |
self._FreeDebugData() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
464 |
self.PLClibraryLock.release() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
465 |
if offset and offset == size.value: |
917 | 466 |
return self.PLCStatus, tick.value, res |
592 | 467 |
#elif size.value: |
468 |
#PLCprint("Debug error - wrong buffer unpack ! %d != %d"%(offset, size.value)) |
|
917 | 469 |
return self.PLCStatus, None, [] |
592 | 470 |
|
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
471 |
def RemoteExec(self, script, **kwargs): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
472 |
try: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
473 |
exec script in kwargs |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
474 |
except: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
475 |
e_type, e_value, e_traceback = sys.exc_info() |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
476 |
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
|
477 |
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
|
478 |
(line_no, e_value, script.splitlines()[line_no - 1])) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
479 |
return (0, kwargs.get("returnVal", None)) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
480 |
|
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
481 |