author | greg |
Mon, 04 May 2009 09:31:16 +0200 | |
changeset 340 | 651b8fb572e7 |
parent 339 | 6dbde4a0c31d |
child 344 | 25b7b7f854bc |
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 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
26 |
from threading import Timer, Thread |
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 |
||
35 |
import os,sys,traceback |
|
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 |
|
55 |
# Creates fake C funcs proxies |
|
56 |
self._FreePLC() |
|
57 |
self.daemon = daemon |
|
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
58 |
self.statuschange = statuschange |
301 | 59 |
self.hmi_frame = None |
229 | 60 |
|
61 |
# Get the last transfered PLC if connector must be restart |
|
62 |
try: |
|
63 |
self.CurrentPLCFilename=open( |
|
64 |
self._GetMD5FileName(), |
|
65 |
"r").read().strip() + lib_ext |
|
66 |
except Exception, e: |
|
67 |
self.PLCStatus = "Empty" |
|
68 |
self.CurrentPLCFilename=None |
|
69 |
||
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
|
229 | 74 |
def _GetMD5FileName(self): |
75 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
76 |
||
77 |
def _GetLibFileName(self): |
|
78 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
79 |
||
80 |
||
81 |
def _LoadNewPLC(self): |
|
82 |
""" |
|
83 |
Load PLC library |
|
84 |
Declare all functions, arguments and return values |
|
85 |
""" |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
86 |
PLCprint("Load PLC") |
229 | 87 |
try: |
88 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
89 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
90 |
||
91 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
92 |
self._startPLC.restype = ctypes.c_int |
|
93 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
94 |
||
95 |
self._stopPLC = self.PLClibraryHandle.stopPLC |
|
96 |
self._stopPLC.restype = None |
|
97 |
||
98 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
99 |
self._ResetDebugVariables.restype = None |
|
100 |
||
235 | 101 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 102 |
self._RegisterDebugVariable.restype = None |
235 | 103 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int] |
229 | 104 |
|
105 |
self._IterDebugData = self.PLClibraryHandle.IterDebugData |
|
106 |
self._IterDebugData.restype = ctypes.c_void_p |
|
107 |
self._IterDebugData.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p)] |
|
108 |
||
109 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
110 |
self._FreeDebugData.restype = None |
|
111 |
||
112 |
self._WaitDebugData = self.PLClibraryHandle.WaitDebugData |
|
113 |
self._WaitDebugData.restype = ctypes.c_int |
|
235 | 114 |
|
115 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
116 |
self._suspendDebug.restype = None |
|
117 |
||
118 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
119 |
self._resumeDebug.restype = 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
|
120 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
121 |
self._PythonIterator = self.PLClibraryHandle.PythonIterator |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
122 |
self._PythonIterator.restype = ctypes.c_char_p |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
123 |
self._PythonIterator.argtypes = [ctypes.c_char_p] |
235 | 124 |
|
229 | 125 |
return True |
126 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
127 |
PLCprint(traceback.format_exc()) |
229 | 128 |
return False |
129 |
||
130 |
def _FreePLC(self): |
|
131 |
""" |
|
132 |
Unload PLC library. |
|
133 |
This is also called by __init__ to create dummy C func proxies |
|
134 |
""" |
|
135 |
# Forget all refs to library |
|
136 |
self._startPLC = lambda:None |
|
137 |
self._stopPLC = lambda:None |
|
138 |
self._ResetDebugVariables = lambda:None |
|
139 |
self._RegisterDebugVariable = lambda x:None |
|
140 |
self._IterDebugData = lambda x,y:None |
|
141 |
self._FreeDebugData = lambda:None |
|
235 | 142 |
self._WaitDebugData = lambda:-1 |
143 |
self._suspendDebug = lambda:None |
|
144 |
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
|
145 |
self._PythonIterator = lambda:"" |
229 | 146 |
self.PLClibraryHandle = None |
147 |
# Unload library explicitely |
|
148 |
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
|
149 |
PLCprint("Unload PLC") |
229 | 150 |
dlclose(self._PLClibraryHandle) |
151 |
res = self._DetectDirtyLibs() |
|
152 |
else: |
|
153 |
res = False |
|
154 |
||
155 |
self._PLClibraryHandle = None |
|
156 |
||
157 |
return res |
|
158 |
||
159 |
def _DetectDirtyLibs(self): |
|
160 |
# Detect dirty libs |
|
161 |
# Get lib dependencies (for dirty lib detection) |
|
162 |
if os.name == "posix": |
|
163 |
# parasiting libs listed with ldd |
|
164 |
badlibs = [ toks.split()[0] for toks in commands.getoutput( |
|
165 |
"ldd "+self._GetLibFileName()).splitlines() ] |
|
166 |
for badlib in badlibs: |
|
167 |
if badlib[:6] in ["libwx_", |
|
168 |
"libwxs", |
|
169 |
"libgtk", |
|
170 |
"libgdk", |
|
171 |
"libatk", |
|
172 |
"libpan", |
|
173 |
"libX11", |
|
174 |
]: |
|
175 |
#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
|
176 |
PLCprint("Dirty lib detected :" + badlib) |
229 | 177 |
#dlclose(badhandle) |
178 |
return True |
|
179 |
return False |
|
180 |
||
299 | 181 |
def PrepareRuntimePy(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
182 |
self.python_threads_vars = globals().copy() |
283
d0e6fc0701fb
Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents:
280
diff
changeset
|
183 |
pyfile = os.path.join(self.workingdir, "runtime.py") |
301 | 184 |
hmifile = os.path.join(self.workingdir, "hmi.py") |
322
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
185 |
if os.path.exists(hmifile): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
186 |
try: |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
187 |
execfile(hmifile, self.python_threads_vars) |
332 | 188 |
if os.path.exists(pyfile): |
189 |
try: |
|
190 |
# TODO handle exceptions in runtime.py |
|
191 |
# pyfile may redefine _runtime_cleanup |
|
192 |
# or even call _PythonThreadProc itself. |
|
193 |
execfile(pyfile, self.python_threads_vars) |
|
194 |
except: |
|
195 |
PLCprint(traceback.format_exc()) |
|
322
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
196 |
if self.python_threads_vars.has_key('wx'): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
197 |
wx = self.python_threads_vars['wx'] |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
198 |
# try to instanciate the first frame found. |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
199 |
for name, obj in self.python_threads_vars.iteritems(): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
200 |
# obj is a class |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
201 |
if type(obj)==type(type) and issubclass(obj,wx.Frame): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
202 |
def create_frame(): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
203 |
self.hmi_frame = obj(None) |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
204 |
self.python_threads_vars[name] = self.hmi_frame |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
205 |
# keep track of class... never know |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
206 |
self.python_threads_vars['Class_'+name] = obj |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
207 |
self.hmi_frame.Bind(wx.EVT_CLOSE, OnCloseFrame) |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
208 |
self.hmi_frame.Show() |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
209 |
|
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
210 |
def OnCloseFrame(evt): |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
211 |
wx.MessageBox("Please stop PLC to close") |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
212 |
create_frame() |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
213 |
break |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
214 |
except: |
b8ae0580565c
Make 'runtime.py' and 'hmi.py' executed independantly
lbessard
parents:
319
diff
changeset
|
215 |
PLCprint(traceback.format_exc()) |
332 | 216 |
elif os.path.exists(pyfile): |
217 |
try: |
|
218 |
# TODO handle exceptions in runtime.py |
|
219 |
# pyfile may redefine _runtime_cleanup |
|
220 |
# or even call _PythonThreadProc itself. |
|
221 |
execfile(pyfile, self.python_threads_vars) |
|
222 |
except: |
|
223 |
PLCprint(traceback.format_exc()) |
|
299 | 224 |
runtime_begin = self.python_threads_vars.get("_runtime_begin",None) |
225 |
if runtime_begin is not None: |
|
329 | 226 |
runtime_begin() |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
227 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
228 |
def FinishRuntimePy(self): |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
332
diff
changeset
|
229 |
runtime_cleanup = None |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
332
diff
changeset
|
230 |
if self.python_threads_vars is not None: |
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
332
diff
changeset
|
231 |
runtime_cleanup = self.python_threads_vars.get("_runtime_cleanup",None) |
299 | 232 |
if runtime_cleanup is not None: |
329 | 233 |
runtime_cleanup() |
301 | 234 |
if self.hmi_frame is not None: |
329 | 235 |
self.hmi_frame.Destroy() |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
236 |
self.python_threads_vars = None |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
237 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
238 |
def PythonThreadProc(self): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
239 |
PLCprint("PythonThreadProc started") |
329 | 240 |
self.evaluator(self.PrepareRuntimePy) |
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
|
241 |
res,cmd = "None","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
|
242 |
while self.PLCStatus == "Started": |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
243 |
#print "_PythonIterator(", res, ")", |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
244 |
cmd = self._PythonIterator(res) |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
245 |
#print " -> ", cmd |
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
|
246 |
if cmd is 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
|
247 |
break |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
248 |
try : |
301 | 249 |
res = str(self.evaluator(eval,cmd,self.python_threads_vars)) |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
250 |
except Exception,e: |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
251 |
res = "#EXCEPTION : "+str(e) |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
252 |
PLCprint(res) |
329 | 253 |
self.evaluator(self.FinishRuntimePy) |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
254 |
PLCprint("PythonThreadProc interrupted") |
229 | 255 |
|
235 | 256 |
def StartPLC(self, debug=False): |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
257 |
PLCprint("StartPLC") |
229 | 258 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
259 |
c_argv = ctypes.c_char_p * len(self.argv) |
|
260 |
if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
|
235 | 261 |
if debug: |
327
e82c422ad811
Register the current watched variable in Debug Window before starting
lbessard
parents:
322
diff
changeset
|
262 |
for idx in self._Idxs: |
e82c422ad811
Register the current watched variable in Debug Window before starting
lbessard
parents:
322
diff
changeset
|
263 |
self._RegisterDebugVariable(idx) |
235 | 264 |
self._resumeDebug() |
229 | 265 |
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
|
266 |
self.StatusChange() |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
267 |
self.PythonThread = Thread(target=self.PythonThreadProc) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
268 |
self.PythonThread.start() |
229 | 269 |
return True |
270 |
else: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
271 |
PLCprint("Problem starting PLC") |
229 | 272 |
self._DoStopPLC() |
273 |
return False |
|
274 |
||
275 |
def _DoStopPLC(self): |
|
329 | 276 |
self.PLCStatus = "Stopped" |
339 | 277 |
self.StatusChange() |
229 | 278 |
self._stopPLC() |
279 |
if self._FreePLC(): |
|
280 |
self.PLCStatus = "Dirty" |
|
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
|
281 |
self.StatusChange() |
229 | 282 |
return True |
283 |
||
284 |
def StopPLC(self): |
|
285 |
if self.PLCStatus == "Started": |
|
286 |
self._DoStopPLC() |
|
287 |
return True |
|
288 |
return False |
|
289 |
||
290 |
def _Reload(self): |
|
291 |
self.daemon.shutdown(True) |
|
292 |
self.daemon.sock.close() |
|
293 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
294 |
# never reached |
|
295 |
return 0 |
|
296 |
||
297 |
def ForceReload(self): |
|
298 |
# respawn python interpreter |
|
299 |
Timer(0.1,self._Reload).start() |
|
300 |
return True |
|
301 |
||
302 |
def GetPLCstatus(self): |
|
303 |
return self.PLCStatus |
|
304 |
||
305 |
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
|
306 |
PLCprint("NewPLC (%s)"%md5sum) |
229 | 307 |
if self.PLCStatus in ["Stopped", "Empty", "Dirty"]: |
308 |
NewFileName = md5sum + lib_ext |
|
309 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
310 |
try: |
|
311 |
os.remove(os.path.join(self.workingdir, |
|
312 |
self.CurrentPLCFilename)) |
|
313 |
for filename in file(extra_files_log, "r").readlines() + extra_files_log: |
|
314 |
try: |
|
315 |
os.remove(os.path.join(self.workingdir, filename)) |
|
316 |
except: |
|
317 |
pass |
|
318 |
except: |
|
319 |
pass |
|
320 |
||
321 |
try: |
|
322 |
# Create new PLC file |
|
323 |
open(os.path.join(self.workingdir,NewFileName), |
|
324 |
'wb').write(data) |
|
325 |
||
326 |
# Store new PLC filename based on md5 key |
|
327 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
328 |
||
329 |
# Then write the files |
|
330 |
log = file(extra_files_log, "w") |
|
331 |
for fname,fdata in extrafiles: |
|
332 |
fpath = os.path.join(self.workingdir,fname) |
|
333 |
open(fpath, "wb").write(fdata) |
|
334 |
log.write(fname+'\n') |
|
335 |
||
336 |
# Store new PLC filename |
|
337 |
self.CurrentPLCFilename = NewFileName |
|
338 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
339 |
PLCprint(traceback.format_exc()) |
229 | 340 |
return False |
341 |
if self.PLCStatus == "Empty": |
|
342 |
self.PLCStatus = "Stopped" |
|
343 |
return True |
|
344 |
return False |
|
345 |
||
346 |
def MatchMD5(self, MD5): |
|
347 |
try: |
|
348 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
349 |
return last_md5 == MD5 |
|
350 |
except: |
|
351 |
return False |
|
352 |
||
353 |
def SetTraceVariablesList(self, idxs): |
|
354 |
""" |
|
355 |
Call ctype imported function to append |
|
356 |
these indexes to registred variables in PLC debugger |
|
357 |
""" |
|
235 | 358 |
self._suspendDebug() |
229 | 359 |
# keep a copy of requested idx |
360 |
self._Idxs = idxs[:] |
|
361 |
self._ResetDebugVariables() |
|
362 |
for idx in idxs: |
|
363 |
self._RegisterDebugVariable(idx) |
|
235 | 364 |
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
|
365 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
366 |
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
|
367 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
368 |
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
|
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 |
_fields_ = [("len", ctypes.c_uint8), |
302 | 371 |
("body", ctypes.c_char * 127)] |
229 | 372 |
|
238 | 373 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
374 |
"STEP" : (ctypes.c_uint8, lambda x:x.value), |
|
375 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
|
376 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value), |
|
377 |
"SINT" : (ctypes.c_int8, lambda x:x.value), |
|
378 |
"USINT" : (ctypes.c_uint8, lambda x:x.value), |
|
379 |
"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
|
380 |
"STRING" : (IEC_STRING, lambda x:x.body[:x.len]), |
238 | 381 |
"INT" : (ctypes.c_int16, lambda x:x.value), |
382 |
"UINT" : (ctypes.c_uint16, lambda x:x.value), |
|
383 |
"WORD" : (ctypes.c_uint16, lambda x:x.value), |
|
384 |
"WSTRING" : (None, None),#TODO |
|
385 |
"DINT" : (ctypes.c_int32, lambda x:x.value), |
|
386 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value), |
|
387 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value), |
|
388 |
"LINT" : (ctypes.c_int64, lambda x:x.value), |
|
389 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value), |
|
390 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value), |
|
391 |
"REAL" : (ctypes.c_float, lambda x:x.value), |
|
392 |
"LREAL" : (ctypes.c_double, lambda x:x.value), |
|
229 | 393 |
} |
394 |
||
395 |
def GetTraceVariables(self): |
|
396 |
""" |
|
339 | 397 |
Return a list of variables, corresponding to the list of required idx |
229 | 398 |
""" |
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
|
399 |
if self.PLCStatus == "Started": |
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
|
400 |
tick = self._WaitDebugData() |
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
|
401 |
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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
|
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 |
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
|
409 |
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
|
410 |
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
|
411 |
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
|
412 |
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
|
413 |
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
|
414 |
else: |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
415 |
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
|
416 |
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
|
417 |
self._FreeDebugData() |
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 |
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
|
419 |
return -1, 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
|
420 |
|
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
|
421 |
|
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 |