author | Edouard Tisserant |
Thu, 23 Feb 2012 17:45:11 +0100 | |
changeset 695 | f378fe028f6c |
parent 690 | ef60d7e188e6 |
child 699 | 6ff64cadb1ff |
permissions | -rwxr-xr-x |
229 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import Pyro.core as pyro |
|
690
ef60d7e188e6
Added a semaphore when starting runtime's python thread to make sure startPLC doesn't return before PLC is really initialized.
Edouard Tisserant
parents:
614
diff
changeset
|
26 |
from threading import Timer, Thread, Lock, Semaphore |
301 | 27 |
import ctypes, os, commands, types, sys |
592 | 28 |
from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator |
301 | 29 |
|
229 | 30 |
if os.name in ("nt", "ce"): |
31 |
from _ctypes import LoadLibrary as dlopen |
|
32 |
from _ctypes import FreeLibrary as dlclose |
|
33 |
elif os.name == "posix": |
|
34 |
from _ctypes import dlopen, dlclose |
|
35 |
||
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
36 |
import traceback |
229 | 37 |
|
38 |
lib_ext ={ |
|
39 |
"linux2":".so", |
|
40 |
"win32":".dll", |
|
41 |
}.get(sys.platform, "") |
|
42 |
||
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
43 |
def PLCprint(message): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
44 |
sys.stdout.write("PLCobject : "+message+"\n") |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
45 |
sys.stdout.flush() |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
46 |
|
229 | 47 |
class PLCObject(pyro.ObjBase): |
235 | 48 |
_Idxs = [] |
368 | 49 |
def __init__(self, workingdir, daemon, argv, statuschange, evaluator, website): |
229 | 50 |
pyro.ObjBase.__init__(self) |
301 | 51 |
self.evaluator = evaluator |
229 | 52 |
self.argv = [workingdir] + argv # force argv[0] to be "path" to exec... |
53 |
self.workingdir = workingdir |
|
54 |
self.PLCStatus = "Stopped" |
|
55 |
self.PLClibraryHandle = None |
|
352 | 56 |
self.PLClibraryLock = Lock() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
57 |
self.DummyIteratorLock = None |
229 | 58 |
# Creates fake C funcs proxies |
59 |
self._FreePLC() |
|
60 |
self.daemon = daemon |
|
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
61 |
self.statuschange = statuschange |
301 | 62 |
self.hmi_frame = None |
368 | 63 |
self.website = website |
229 | 64 |
|
65 |
# Get the last transfered PLC if connector must be restart |
|
66 |
try: |
|
67 |
self.CurrentPLCFilename=open( |
|
68 |
self._GetMD5FileName(), |
|
69 |
"r").read().strip() + lib_ext |
|
70 |
except Exception, e: |
|
71 |
self.PLCStatus = "Empty" |
|
72 |
self.CurrentPLCFilename=None |
|
73 |
||
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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
|
229 | 78 |
def _GetMD5FileName(self): |
79 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
80 |
||
81 |
def _GetLibFileName(self): |
|
82 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
83 |
||
84 |
||
85 |
def _LoadNewPLC(self): |
|
86 |
""" |
|
87 |
Load PLC library |
|
88 |
Declare all functions, arguments and return values |
|
89 |
""" |
|
90 |
try: |
|
91 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
92 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
93 |
||
94 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
95 |
self._startPLC.restype = ctypes.c_int |
|
96 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
97 |
||
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
98 |
self._stopPLC_real = self.PLClibraryHandle.stopPLC |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
99 |
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
|
100 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
self._PythonIterator.restype = ctypes.c_char_p |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
104 |
self._PythonIterator.argtypes = [ctypes.c_char_p] |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
105 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
106 |
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
|
107 |
else: |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
108 |
# If python plugin is not enabled, we reuse _PythonIterator |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
109 |
# 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
|
110 |
self.PythonIteratorLock = Lock() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
111 |
self.PythonIteratorLock.acquire() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
112 |
def PythonIterator(res): |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
113 |
self.PythonIteratorLock.acquire() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
114 |
self.PythonIteratorLock.release() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
115 |
return None |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
116 |
self._PythonIterator = PythonIterator |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
117 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
118 |
def __StopPLC(): |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
119 |
self._stopPLC_real() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
120 |
self.PythonIteratorLock.release() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
121 |
self._stopPLC = __StopPLC |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
122 |
|
229 | 123 |
|
124 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
125 |
self._ResetDebugVariables.restype = None |
|
126 |
||
235 | 127 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 128 |
self._RegisterDebugVariable.restype = None |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
129 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int, ctypes.c_void_p] |
229 | 130 |
|
131 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
132 |
self._FreeDebugData.restype = None |
|
133 |
||
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
134 |
self._GetDebugData = self.PLClibraryHandle.GetDebugData |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
135 |
self._GetDebugData.restype = ctypes.c_int |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
136 |
self._GetDebugData.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p)] |
235 | 137 |
|
138 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
614 | 139 |
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
|
140 |
self._suspendDebug.argtypes = [ctypes.c_int] |
235 | 141 |
|
142 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
143 |
self._resumeDebug.restype = None |
|
144 |
||
229 | 145 |
return True |
146 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
147 |
PLCprint(traceback.format_exc()) |
229 | 148 |
return False |
149 |
||
150 |
def _FreePLC(self): |
|
151 |
""" |
|
152 |
Unload PLC library. |
|
153 |
This is also called by __init__ to create dummy C func proxies |
|
154 |
""" |
|
352 | 155 |
self.PLClibraryLock.acquire() |
229 | 156 |
# Forget all refs to library |
157 |
self._startPLC = lambda:None |
|
158 |
self._stopPLC = lambda:None |
|
159 |
self._ResetDebugVariables = lambda:None |
|
479
c28f40b27798
Bug on RegisterDebugVariable when no PLC running fixed
laurent
parents:
477
diff
changeset
|
160 |
self._RegisterDebugVariable = lambda x, y:None |
229 | 161 |
self._IterDebugData = lambda x,y:None |
162 |
self._FreeDebugData = lambda:None |
|
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
163 |
self._GetDebugData = lambda:-1 |
614 | 164 |
self._suspendDebug = lambda x:-1 |
235 | 165 |
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
|
166 |
self._PythonIterator = lambda:"" |
229 | 167 |
self.PLClibraryHandle = None |
168 |
# Unload library explicitely |
|
169 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
170 |
dlclose(self._PLClibraryHandle) |
|
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
171 |
self._PLClibraryHandle = None |
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
172 |
|
352 | 173 |
self.PLClibraryLock.release() |
229 | 174 |
return False |
175 |
||
299 | 176 |
def PrepareRuntimePy(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
177 |
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
|
178 |
self.python_threads_vars["WorkingDir"] = self.workingdir |
368 | 179 |
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
|
180 |
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
|
181 |
self.python_threads_vars["_runtime_cleanup"] = [] |
368 | 182 |
|
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
183 |
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
|
184 |
name, ext = os.path.splitext(filename) |
391
9b1801ef99b5
fix runtime.py filename case to avoid problem on multi-platform
greg
parents:
368
diff
changeset
|
185 |
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
|
186 |
try: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
187 |
# 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
|
188 |
# pyfile may redefine _runtime_cleanup |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
189 |
# or even call _PythonThreadProc itself. |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
190 |
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
|
191 |
except: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
192 |
PLCprint(traceback.format_exc()) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
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
|
199 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
200 |
for runtime_begin in self.python_threads_vars.get("_runtime_begin", []): |
329 | 201 |
runtime_begin() |
368 | 202 |
|
203 |
if self.website is not None: |
|
204 |
self.website.PLCStarted() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
205 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
206 |
def FinishRuntimePy(self): |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
207 |
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
|
208 |
runtime_cleanup() |
368 | 209 |
if self.website is not None: |
210 |
self.website.PLCStopped() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
211 |
self.python_threads_vars = None |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
212 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
213 |
def PythonThreadProc(self): |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
214 |
c_argv = ctypes.c_char_p * len(self.argv) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
215 |
error = None |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
216 |
if self._LoadNewPLC(): |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
217 |
if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
229 | 218 |
self.PLCStatus = "Started" |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
219 |
self.StatusChange() |
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
|
220 |
self.StartSem.release() |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
221 |
self.evaluator(self.PrepareRuntimePy) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
222 |
res,cmd = "None","None" |
352 | 223 |
while True: |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
224 |
#print "_PythonIterator(", res, ")", |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
225 |
cmd = self._PythonIterator(res) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
226 |
#print " -> ", cmd |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
227 |
if cmd is None: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
228 |
break |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
229 |
try : |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
230 |
res = str(self.evaluator(eval,cmd,self.python_threads_vars)) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
231 |
except Exception,e: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
232 |
res = "#EXCEPTION : "+str(e) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
233 |
PLCprint(res) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
234 |
self.PLCStatus = "Stopped" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
235 |
self.StatusChange() |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
236 |
self.evaluator(self.FinishRuntimePy) |
229 | 237 |
else: |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
238 |
error = "starting" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
239 |
else: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
240 |
error = "loading" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
241 |
if error is not None: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
242 |
PLCprint("Problem %s PLC"%error) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
243 |
self.PLCStatus = "Broken" |
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
|
244 |
self.StatusChange() |
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
|
245 |
self.StartSem.release() |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
246 |
self._FreePLC() |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
247 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
248 |
def StartPLC(self): |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
249 |
PLCprint("StartPLC") |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
250 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
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
|
251 |
self.StartSem=Semaphore(0) |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
252 |
self.PythonThread = Thread(target=self.PythonThreadProc) |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
253 |
self.PythonThread.start() |
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
|
254 |
self.StartSem.acquire() |
352 | 255 |
|
229 | 256 |
def StopPLC(self): |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
257 |
PLCprint("StopPLC") |
229 | 258 |
if self.PLCStatus == "Started": |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
259 |
self.PLCStatus = "Stopped" |
352 | 260 |
self._stopPLC() |
229 | 261 |
return True |
262 |
return False |
|
263 |
||
264 |
def _Reload(self): |
|
265 |
self.daemon.shutdown(True) |
|
266 |
self.daemon.sock.close() |
|
267 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
268 |
# never reached |
|
269 |
return 0 |
|
270 |
||
271 |
def ForceReload(self): |
|
272 |
# respawn python interpreter |
|
273 |
Timer(0.1,self._Reload).start() |
|
274 |
return True |
|
275 |
||
276 |
def GetPLCstatus(self): |
|
277 |
return self.PLCStatus |
|
278 |
||
279 |
def NewPLC(self, md5sum, data, extrafiles): |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
280 |
PLCprint("NewPLC (%s)"%md5sum) |
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
281 |
if self.PLCStatus in ["Stopped", "Empty", "Broken"]: |
229 | 282 |
NewFileName = md5sum + lib_ext |
283 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
284 |
try: |
|
285 |
os.remove(os.path.join(self.workingdir, |
|
286 |
self.CurrentPLCFilename)) |
|
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
287 |
for filename in file(extra_files_log, "r").readlines() + [extra_files_log]: |
229 | 288 |
try: |
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
289 |
os.remove(os.path.join(self.workingdir, filename.strip())) |
229 | 290 |
except: |
291 |
pass |
|
292 |
except: |
|
293 |
pass |
|
294 |
||
295 |
try: |
|
296 |
# Create new PLC file |
|
297 |
open(os.path.join(self.workingdir,NewFileName), |
|
298 |
'wb').write(data) |
|
299 |
||
300 |
# Store new PLC filename based on md5 key |
|
301 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
302 |
||
303 |
# Then write the files |
|
304 |
log = file(extra_files_log, "w") |
|
305 |
for fname,fdata in extrafiles: |
|
306 |
fpath = os.path.join(self.workingdir,fname) |
|
307 |
open(fpath, "wb").write(fdata) |
|
308 |
log.write(fname+'\n') |
|
309 |
||
310 |
# Store new PLC filename |
|
311 |
self.CurrentPLCFilename = NewFileName |
|
312 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
313 |
PLCprint(traceback.format_exc()) |
229 | 314 |
return False |
315 |
if self.PLCStatus == "Empty": |
|
316 |
self.PLCStatus = "Stopped" |
|
317 |
return True |
|
318 |
return False |
|
319 |
||
320 |
def MatchMD5(self, MD5): |
|
321 |
try: |
|
322 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
323 |
return last_md5 == MD5 |
|
324 |
except: |
|
325 |
return False |
|
326 |
||
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
327 |
|
592 | 328 |
|
229 | 329 |
def SetTraceVariablesList(self, idxs): |
330 |
""" |
|
331 |
Call ctype imported function to append |
|
332 |
these indexes to registred variables in PLC debugger |
|
333 |
""" |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
334 |
if idxs: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
335 |
# suspend but dont disable |
614 | 336 |
if self._suspendDebug(False) == 0: |
337 |
# keep a copy of requested idx |
|
338 |
self._Idxs = idxs[:] |
|
339 |
self._ResetDebugVariables() |
|
340 |
for idx,iectype,force in idxs: |
|
341 |
if force !=None: |
|
342 |
c_type,unpack_func, pack_func = \ |
|
343 |
TypeTranslator.get(iectype, |
|
344 |
(None,None,None)) |
|
345 |
force = ctypes.byref(pack_func(c_type,force)) |
|
346 |
self._RegisterDebugVariable(idx, force) |
|
347 |
self._resumeDebug() |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
348 |
else: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
349 |
self._suspendDebug(True) |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
350 |
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
|
351 |
|
229 | 352 |
def GetTraceVariables(self): |
353 |
""" |
|
339 | 354 |
Return a list of variables, corresponding to the list of required idx |
229 | 355 |
""" |
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
|
356 |
if self.PLCStatus == "Started": |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
357 |
res=[] |
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
358 |
tick = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
359 |
size = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
360 |
buffer = ctypes.c_void_p() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
361 |
offset = 0 |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
362 |
if self.PLClibraryLock.acquire(False) and \ |
504 | 363 |
self._GetDebugData(ctypes.byref(tick), |
364 |
ctypes.byref(size), |
|
365 |
ctypes.byref(buffer)) == 0 : |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
366 |
if size.value: |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
367 |
for idx, iectype, forced in self._Idxs: |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
368 |
cursor = ctypes.c_void_p(buffer.value + offset) |
504 | 369 |
c_type,unpack_func, pack_func = \ |
593 | 370 |
TypeTranslator.get(iectype, |
504 | 371 |
(None,None,None)) |
592 | 372 |
if c_type is not None and offset < size.value: |
504 | 373 |
res.append(unpack_func( |
374 |
ctypes.cast(cursor, |
|
375 |
ctypes.POINTER(c_type)).contents)) |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
376 |
offset += ctypes.sizeof(c_type) |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
377 |
else: |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
378 |
if c_type is None: |
504 | 379 |
PLCprint("Debug error - " + iectype + |
380 |
" not supported !") |
|
592 | 381 |
#if offset >= size.value: |
382 |
#PLCprint("Debug error - buffer too small ! %d != %d"%(offset, size.value)) |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
383 |
break |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
384 |
self._FreeDebugData() |
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
385 |
self.PLClibraryLock.release() |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
386 |
if offset and offset == size.value: |
460
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
387 |
return self.PLCStatus, tick.value, res |
592 | 388 |
#elif size.value: |
389 |
#PLCprint("Debug error - wrong buffer unpack ! %d != %d"%(offset, size.value)) |
|
390 |
return self.PLCStatus, None, [] |
|
391 |