author | laurent |
Thu, 03 Sep 2009 11:28:46 +0200 | |
changeset 387 | 2cc48e356902 |
parent 368 | 86ecd8374dae |
child 391 | 9b1801ef99b5 |
child 393 | af20e07e53c5 |
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 = [] |
368 | 48 |
def __init__(self, workingdir, daemon, argv, statuschange, evaluator, website): |
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 |
368 | 62 |
self.website = website |
229 | 63 |
|
64 |
# Get the last transfered PLC if connector must be restart |
|
65 |
try: |
|
66 |
self.CurrentPLCFilename=open( |
|
67 |
self._GetMD5FileName(), |
|
68 |
"r").read().strip() + lib_ext |
|
69 |
except Exception, e: |
|
70 |
self.PLCStatus = "Empty" |
|
71 |
self.CurrentPLCFilename=None |
|
72 |
||
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
|
229 | 77 |
def _GetMD5FileName(self): |
78 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
79 |
||
80 |
def _GetLibFileName(self): |
|
81 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
82 |
||
83 |
||
84 |
def _LoadNewPLC(self): |
|
85 |
""" |
|
86 |
Load PLC library |
|
87 |
Declare all functions, arguments and return values |
|
88 |
""" |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
89 |
PLCprint("Load PLC") |
229 | 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 |
||
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
98 |
self.DummyIteratorLock = Lock() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
99 |
self.DummyIteratorLock.acquire() |
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 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
106 |
def StopPLCLock(): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
107 |
self.PLClibraryLock.acquire() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
108 |
self.PLClibraryHandle.stopPLC() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
109 |
self.PLClibraryLock.release() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
110 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
111 |
else: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
112 |
def DummyIterator(res): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
113 |
self.DummyIteratorLock.acquire() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
114 |
return None |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
115 |
self._PythonIterator = DummyIterator |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
116 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
117 |
def StopPLCLock(): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
118 |
self.PLClibraryLock.acquire() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
119 |
self.PLClibraryHandle.stopPLC() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
120 |
self.DummyIteratorLock.release() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
121 |
self.PLClibraryLock.release() |
352 | 122 |
|
123 |
self._stopPLC = StopPLCLock |
|
229 | 124 |
self._stopPLC.restype = None |
125 |
||
126 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
127 |
self._ResetDebugVariables.restype = None |
|
128 |
||
235 | 129 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 130 |
self._RegisterDebugVariable.restype = None |
235 | 131 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int] |
229 | 132 |
|
133 |
self._IterDebugData = self.PLClibraryHandle.IterDebugData |
|
134 |
self._IterDebugData.restype = ctypes.c_void_p |
|
135 |
self._IterDebugData.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p)] |
|
136 |
||
137 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
138 |
self._FreeDebugData.restype = None |
|
139 |
||
140 |
self._WaitDebugData = self.PLClibraryHandle.WaitDebugData |
|
141 |
self._WaitDebugData.restype = ctypes.c_int |
|
235 | 142 |
|
143 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
144 |
self._suspendDebug.restype = None |
|
145 |
||
146 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
147 |
self._resumeDebug.restype = None |
|
148 |
||
229 | 149 |
return True |
150 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
151 |
PLCprint(traceback.format_exc()) |
229 | 152 |
return False |
153 |
||
154 |
def _FreePLC(self): |
|
155 |
""" |
|
156 |
Unload PLC library. |
|
157 |
This is also called by __init__ to create dummy C func proxies |
|
158 |
""" |
|
352 | 159 |
self.PLClibraryLock.acquire() |
229 | 160 |
# Forget all refs to library |
161 |
self._startPLC = lambda:None |
|
162 |
self._stopPLC = lambda:None |
|
163 |
self._ResetDebugVariables = lambda:None |
|
164 |
self._RegisterDebugVariable = lambda x:None |
|
165 |
self._IterDebugData = lambda x,y:None |
|
166 |
self._FreeDebugData = lambda:None |
|
235 | 167 |
self._WaitDebugData = lambda:-1 |
168 |
self._suspendDebug = lambda:None |
|
169 |
self._resumeDebug = lambda:None |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
170 |
self._PythonIterator = lambda:"" |
229 | 171 |
self.PLClibraryHandle = None |
172 |
# Unload library explicitely |
|
173 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
174 |
PLCprint("Unload PLC") |
229 | 175 |
dlclose(self._PLClibraryHandle) |
176 |
res = self._DetectDirtyLibs() |
|
177 |
else: |
|
178 |
res = False |
|
179 |
||
180 |
self._PLClibraryHandle = None |
|
352 | 181 |
self.PLClibraryLock.release() |
229 | 182 |
return res |
183 |
||
184 |
def _DetectDirtyLibs(self): |
|
185 |
# Detect dirty libs |
|
186 |
# Get lib dependencies (for dirty lib detection) |
|
187 |
if os.name == "posix": |
|
188 |
# parasiting libs listed with ldd |
|
189 |
badlibs = [ toks.split()[0] for toks in commands.getoutput( |
|
190 |
"ldd "+self._GetLibFileName()).splitlines() ] |
|
191 |
for badlib in badlibs: |
|
192 |
if badlib[:6] in ["libwx_", |
|
193 |
"libwxs", |
|
194 |
"libgtk", |
|
195 |
"libgdk", |
|
196 |
"libatk", |
|
197 |
"libpan", |
|
198 |
"libX11", |
|
199 |
]: |
|
200 |
#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
|
201 |
PLCprint("Dirty lib detected :" + badlib) |
229 | 202 |
#dlclose(badhandle) |
203 |
return True |
|
204 |
return False |
|
205 |
||
299 | 206 |
def PrepareRuntimePy(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
207 |
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
|
208 |
self.python_threads_vars["WorkingDir"] = self.workingdir |
368 | 209 |
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
|
210 |
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
|
211 |
self.python_threads_vars["_runtime_cleanup"] = [] |
368 | 212 |
|
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
213 |
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
|
214 |
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
|
215 |
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
|
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(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
|
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 |
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
|
224 |
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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
230 |
for runtime_begin in self.python_threads_vars.get("_runtime_begin", []): |
329 | 231 |
runtime_begin() |
368 | 232 |
|
233 |
if self.website is not None: |
|
234 |
self.website.PLCStarted() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
235 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
236 |
def FinishRuntimePy(self): |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
237 |
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
|
238 |
runtime_cleanup() |
368 | 239 |
if self.website is not None: |
240 |
self.website.PLCStopped() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
241 |
self.python_threads_vars = None |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
242 |
|
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
243 |
def PythonThreadProc(self, debug): |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
244 |
PLCprint("PythonThreadProc started") |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
245 |
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
|
246 |
error = None |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
247 |
if self._LoadNewPLC(): |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
248 |
if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
235 | 249 |
if debug: |
327
e82c422ad811
Register the current watched variable in Debug Window before starting
lbessard
parents:
322
diff
changeset
|
250 |
for idx in self._Idxs: |
e82c422ad811
Register the current watched variable in Debug Window before starting
lbessard
parents:
322
diff
changeset
|
251 |
self._RegisterDebugVariable(idx) |
235 | 252 |
self._resumeDebug() |
229 | 253 |
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
|
254 |
self.StatusChange() |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
255 |
self.evaluator(self.PrepareRuntimePy) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
256 |
res,cmd = "None","None" |
352 | 257 |
while True: |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
258 |
#print "_PythonIterator(", res, ")", |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
259 |
cmd = self._PythonIterator(res) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
260 |
#print " -> ", cmd |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
261 |
if cmd is None: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
262 |
break |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
263 |
try : |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
264 |
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
|
265 |
except Exception,e: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
266 |
res = "#EXCEPTION : "+str(e) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
267 |
PLCprint(res) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
268 |
self.PLCStatus = "Stopped" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
269 |
self.StatusChange() |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
270 |
self.evaluator(self.FinishRuntimePy) |
229 | 271 |
else: |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
272 |
error = "starting" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
273 |
else: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
274 |
error = "loading" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
275 |
if error is not None: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
276 |
PLCprint("Problem %s PLC"%error) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
277 |
self.PLCStatus = "Broken" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
278 |
self._FreePLC() |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
279 |
PLCprint("PythonThreadProc interrupted") |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
280 |
|
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
281 |
def StartPLC(self, debug=False): |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
282 |
PLCprint("StartPLC") |
352 | 283 |
if self.CurrentPLCFilename is not None: |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
284 |
self.PythonThread = Thread(target=self.PythonThreadProc, args=[debug]) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
285 |
self.PythonThread.start() |
352 | 286 |
|
229 | 287 |
def StopPLC(self): |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
288 |
PLCprint("StopPLC") |
229 | 289 |
if self.PLCStatus == "Started": |
352 | 290 |
self._stopPLC() |
229 | 291 |
return True |
292 |
return False |
|
293 |
||
294 |
def _Reload(self): |
|
295 |
self.daemon.shutdown(True) |
|
296 |
self.daemon.sock.close() |
|
297 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
298 |
# never reached |
|
299 |
return 0 |
|
300 |
||
301 |
def ForceReload(self): |
|
302 |
# respawn python interpreter |
|
303 |
Timer(0.1,self._Reload).start() |
|
304 |
return True |
|
305 |
||
306 |
def GetPLCstatus(self): |
|
307 |
return self.PLCStatus |
|
308 |
||
309 |
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
|
310 |
PLCprint("NewPLC (%s)"%md5sum) |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
311 |
if self.PLCStatus in ["Stopped", "Empty", "Dirty", "Broken"]: |
229 | 312 |
NewFileName = md5sum + lib_ext |
313 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
314 |
try: |
|
315 |
os.remove(os.path.join(self.workingdir, |
|
316 |
self.CurrentPLCFilename)) |
|
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
317 |
for filename in file(extra_files_log, "r").readlines() + [extra_files_log]: |
229 | 318 |
try: |
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
319 |
os.remove(os.path.join(self.workingdir, filename.strip())) |
229 | 320 |
except: |
321 |
pass |
|
322 |
except: |
|
323 |
pass |
|
324 |
||
325 |
try: |
|
326 |
# Create new PLC file |
|
327 |
open(os.path.join(self.workingdir,NewFileName), |
|
328 |
'wb').write(data) |
|
329 |
||
330 |
# Store new PLC filename based on md5 key |
|
331 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
332 |
||
333 |
# Then write the files |
|
334 |
log = file(extra_files_log, "w") |
|
335 |
for fname,fdata in extrafiles: |
|
336 |
fpath = os.path.join(self.workingdir,fname) |
|
337 |
open(fpath, "wb").write(fdata) |
|
338 |
log.write(fname+'\n') |
|
339 |
||
340 |
# Store new PLC filename |
|
341 |
self.CurrentPLCFilename = NewFileName |
|
342 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
343 |
PLCprint(traceback.format_exc()) |
229 | 344 |
return False |
345 |
if self.PLCStatus == "Empty": |
|
346 |
self.PLCStatus = "Stopped" |
|
347 |
return True |
|
348 |
return False |
|
349 |
||
350 |
def MatchMD5(self, MD5): |
|
351 |
try: |
|
352 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
353 |
return last_md5 == MD5 |
|
354 |
except: |
|
355 |
return False |
|
356 |
||
357 |
def SetTraceVariablesList(self, idxs): |
|
358 |
""" |
|
359 |
Call ctype imported function to append |
|
360 |
these indexes to registred variables in PLC debugger |
|
361 |
""" |
|
235 | 362 |
self._suspendDebug() |
229 | 363 |
# keep a copy of requested idx |
364 |
self._Idxs = idxs[:] |
|
365 |
self._ResetDebugVariables() |
|
366 |
for idx in idxs: |
|
367 |
self._RegisterDebugVariable(idx) |
|
235 | 368 |
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
|
369 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
370 |
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
|
371 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
372 |
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
|
373 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
374 |
_fields_ = [("len", ctypes.c_uint8), |
302 | 375 |
("body", ctypes.c_char * 127)] |
229 | 376 |
|
238 | 377 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
378 |
"STEP" : (ctypes.c_uint8, lambda x:x.value), |
|
379 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
|
380 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value), |
|
381 |
"SINT" : (ctypes.c_int8, lambda x:x.value), |
|
382 |
"USINT" : (ctypes.c_uint8, lambda x:x.value), |
|
383 |
"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
|
384 |
"STRING" : (IEC_STRING, lambda x:x.body[:x.len]), |
238 | 385 |
"INT" : (ctypes.c_int16, lambda x:x.value), |
386 |
"UINT" : (ctypes.c_uint16, lambda x:x.value), |
|
387 |
"WORD" : (ctypes.c_uint16, lambda x:x.value), |
|
388 |
"WSTRING" : (None, None),#TODO |
|
389 |
"DINT" : (ctypes.c_int32, lambda x:x.value), |
|
390 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value), |
|
391 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value), |
|
392 |
"LINT" : (ctypes.c_int64, lambda x:x.value), |
|
393 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value), |
|
394 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value), |
|
395 |
"REAL" : (ctypes.c_float, lambda x:x.value), |
|
396 |
"LREAL" : (ctypes.c_double, lambda x:x.value), |
|
229 | 397 |
} |
398 |
||
399 |
def GetTraceVariables(self): |
|
400 |
""" |
|
339 | 401 |
Return a list of variables, corresponding to the list of required idx |
229 | 402 |
""" |
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
|
403 |
if self.PLCStatus == "Started": |
353 | 404 |
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
|
405 |
tick = self._WaitDebugData() |
353 | 406 |
#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
|
407 |
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
|
408 |
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
|
409 |
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
|
410 |
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
|
411 |
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
|
412 |
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
|
413 |
|
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
|
414 |
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
|
415 |
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
|
416 |
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
|
417 |
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
|
418 |
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
|
419 |
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
|
420 |
else: |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
421 |
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
|
422 |
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
|
423 |
self._FreeDebugData() |
353 | 424 |
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
|
425 |
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
|
426 |
return -1, None |
353 | 427 |