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