author | etisserant |
Fri, 16 Jan 2009 16:52:23 +0100 | |
changeset 298 | 732e30ac8bf3 |
parent 291 | 701c0601db02 |
child 299 | d7f8ffe18017 |
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 |
229 | 27 |
import ctypes, os, commands |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
28 |
import sys |
229 | 29 |
|
30 |
if os.name in ("nt", "ce"): |
|
31 |
from _ctypes import LoadLibrary as dlopen |
|
32 |
from _ctypes import FreeLibrary as dlclose |
|
33 |
elif os.name == "posix": |
|
34 |
from _ctypes import dlopen, dlclose |
|
35 |
||
36 |
import os,sys,traceback |
|
37 |
||
38 |
lib_ext ={ |
|
39 |
"linux2":".so", |
|
40 |
"win32":".dll", |
|
41 |
}.get(sys.platform, "") |
|
42 |
||
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
43 |
def PLCprint(message): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
44 |
sys.stdout.write("PLCobject : "+message+"\n") |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
45 |
sys.stdout.flush() |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
46 |
|
229 | 47 |
class PLCObject(pyro.ObjBase): |
235 | 48 |
_Idxs = [] |
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
49 |
def __init__(self, workingdir, daemon, argv, statuschange=None): |
229 | 50 |
pyro.ObjBase.__init__(self) |
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 |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
59 |
self.python_threads_vars = 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 |
||
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
181 |
def ExecRuntimePy(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") |
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
|
184 |
if os.path.exists(pyfile): |
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
|
185 |
# TODO handle exceptions in runtime.py |
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
|
186 |
# pyfile may redefine _runtime_cleanup |
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
|
187 |
# or even call _PythonThreadProc itself. |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
188 |
execfile(pyfile, self.python_threads_vars) |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
189 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
190 |
def FinishRuntimePy(self): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
191 |
if self.python_threads_vars.get("_runtime_cleanup",None) is not None: |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
192 |
self.python_threads_vars["_runtime_cleanup"]() |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
193 |
self.python_threads_vars = None |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
194 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
195 |
def PythonThreadProc(self): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
196 |
PLCprint("PythonThreadProc 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
|
197 |
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
|
198 |
while self.PLCStatus == "Started": |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
199 |
#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
|
200 |
cmd = self._PythonIterator(res) |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
201 |
#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
|
202 |
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
|
203 |
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
|
204 |
try : |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
205 |
res = str(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
|
206 |
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
|
207 |
res = "#EXCEPTION : "+str(e) |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
208 |
PLCprint(res) |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
209 |
PLCprint("PythonThreadProc interrupted") |
229 | 210 |
|
235 | 211 |
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
|
212 |
PLCprint("StartPLC") |
229 | 213 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
214 |
c_argv = ctypes.c_char_p * len(self.argv) |
|
215 |
if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
|
235 | 216 |
if debug: |
217 |
self._resumeDebug() |
|
229 | 218 |
self.PLCStatus = "Started" |
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
219 |
self.StatusChange() |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
220 |
self.ExecRuntimePy() |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
221 |
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
|
222 |
self.PythonThread.start() |
229 | 223 |
return True |
224 |
else: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
225 |
PLCprint("Problem starting PLC") |
229 | 226 |
self._DoStopPLC() |
227 |
return False |
|
228 |
||
229 |
def _DoStopPLC(self): |
|
230 |
self._stopPLC() |
|
231 |
self.PLCStatus = "Stopped" |
|
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
|
232 |
self.PythonThread.join(timeout=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
|
233 |
if self.PythonThread.isAlive(): |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
234 |
PLCprint("Python thread couldn't be killed") |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
235 |
self.FinishRuntimePy() |
229 | 236 |
if self._FreePLC(): |
237 |
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
|
238 |
self.StatusChange() |
229 | 239 |
return True |
240 |
||
241 |
def StopPLC(self): |
|
242 |
if self.PLCStatus == "Started": |
|
243 |
self._DoStopPLC() |
|
244 |
return True |
|
245 |
return False |
|
246 |
||
247 |
def _Reload(self): |
|
248 |
self.daemon.shutdown(True) |
|
249 |
self.daemon.sock.close() |
|
250 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
251 |
# never reached |
|
252 |
return 0 |
|
253 |
||
254 |
def ForceReload(self): |
|
255 |
# respawn python interpreter |
|
256 |
Timer(0.1,self._Reload).start() |
|
257 |
return True |
|
258 |
||
259 |
def GetPLCstatus(self): |
|
260 |
return self.PLCStatus |
|
261 |
||
262 |
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
|
263 |
PLCprint("NewPLC (%s)"%md5sum) |
229 | 264 |
if self.PLCStatus in ["Stopped", "Empty", "Dirty"]: |
265 |
NewFileName = md5sum + lib_ext |
|
266 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
267 |
try: |
|
268 |
os.remove(os.path.join(self.workingdir, |
|
269 |
self.CurrentPLCFilename)) |
|
270 |
for filename in file(extra_files_log, "r").readlines() + extra_files_log: |
|
271 |
try: |
|
272 |
os.remove(os.path.join(self.workingdir, filename)) |
|
273 |
except: |
|
274 |
pass |
|
275 |
except: |
|
276 |
pass |
|
277 |
||
278 |
try: |
|
279 |
# Create new PLC file |
|
280 |
open(os.path.join(self.workingdir,NewFileName), |
|
281 |
'wb').write(data) |
|
282 |
||
283 |
# Store new PLC filename based on md5 key |
|
284 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
285 |
||
286 |
# Then write the files |
|
287 |
log = file(extra_files_log, "w") |
|
288 |
for fname,fdata in extrafiles: |
|
289 |
fpath = os.path.join(self.workingdir,fname) |
|
290 |
open(fpath, "wb").write(fdata) |
|
291 |
log.write(fname+'\n') |
|
292 |
||
293 |
# Store new PLC filename |
|
294 |
self.CurrentPLCFilename = NewFileName |
|
295 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
296 |
PLCprint(traceback.format_exc()) |
229 | 297 |
return False |
298 |
if self.PLCStatus == "Empty": |
|
299 |
self.PLCStatus = "Stopped" |
|
300 |
return True |
|
301 |
return False |
|
302 |
||
303 |
def MatchMD5(self, MD5): |
|
304 |
try: |
|
305 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
306 |
return last_md5 == MD5 |
|
307 |
except: |
|
308 |
return False |
|
309 |
||
310 |
def SetTraceVariablesList(self, idxs): |
|
311 |
""" |
|
312 |
Call ctype imported function to append |
|
313 |
these indexes to registred variables in PLC debugger |
|
314 |
""" |
|
235 | 315 |
self._suspendDebug() |
229 | 316 |
# keep a copy of requested idx |
317 |
self._Idxs = idxs[:] |
|
318 |
self._ResetDebugVariables() |
|
319 |
for idx in idxs: |
|
320 |
self._RegisterDebugVariable(idx) |
|
235 | 321 |
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
|
322 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
323 |
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
|
324 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
325 |
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
|
326 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
327 |
_fields_ = [("len", ctypes.c_uint8), |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
328 |
("body", ctypes.c_char * 40)] |
229 | 329 |
|
238 | 330 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
331 |
"STEP" : (ctypes.c_uint8, lambda x:x.value), |
|
332 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
|
333 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value), |
|
334 |
"SINT" : (ctypes.c_int8, lambda x:x.value), |
|
335 |
"USINT" : (ctypes.c_uint8, lambda x:x.value), |
|
336 |
"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
|
337 |
"STRING" : (IEC_STRING, lambda x:x.body[:x.len]), |
238 | 338 |
"INT" : (ctypes.c_int16, lambda x:x.value), |
339 |
"UINT" : (ctypes.c_uint16, lambda x:x.value), |
|
340 |
"WORD" : (ctypes.c_uint16, lambda x:x.value), |
|
341 |
"WSTRING" : (None, None),#TODO |
|
342 |
"DINT" : (ctypes.c_int32, lambda x:x.value), |
|
343 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value), |
|
344 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value), |
|
345 |
"LINT" : (ctypes.c_int64, lambda x:x.value), |
|
346 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value), |
|
347 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value), |
|
348 |
"REAL" : (ctypes.c_float, lambda x:x.value), |
|
349 |
"LREAL" : (ctypes.c_double, lambda x:x.value), |
|
229 | 350 |
} |
351 |
||
352 |
def GetTraceVariables(self): |
|
353 |
""" |
|
354 |
Return a list of variables, corresponding to the list of requiered idx |
|
355 |
""" |
|
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
356 |
if self.PLCStatus == "Started": |
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
|
357 |
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
|
358 |
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
|
359 |
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
|
360 |
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
|
361 |
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
|
362 |
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
|
363 |
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
|
364 |
|
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
|
365 |
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
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
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
|
370 |
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
|
371 |
else: |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
372 |
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
|
373 |
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
|
374 |
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
|
375 |
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
|
376 |
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
|
377 |
|
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
|
378 |
|
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
|
379 |