author | lbessard |
Fri, 09 Jan 2009 17:07:56 +0100 | |
changeset 288 | 7ee96191c476 |
parent 286 | a2a8a52b0d4f |
child 290 | 3bd617ae7a05 |
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 |
28 |
||
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 |
||
42 |
class PLCObject(pyro.ObjBase): |
|
235 | 43 |
_Idxs = [] |
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
44 |
def __init__(self, workingdir, daemon, argv, statuschange=None): |
229 | 45 |
pyro.ObjBase.__init__(self) |
46 |
self.argv = [workingdir] + argv # force argv[0] to be "path" to exec... |
|
47 |
self.workingdir = workingdir |
|
48 |
self.PLCStatus = "Stopped" |
|
49 |
self.PLClibraryHandle = None |
|
50 |
# Creates fake C funcs proxies |
|
51 |
self._FreePLC() |
|
52 |
self.daemon = daemon |
|
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
53 |
self.statuschange = statuschange |
229 | 54 |
|
55 |
# Get the last transfered PLC if connector must be restart |
|
56 |
try: |
|
57 |
self.CurrentPLCFilename=open( |
|
58 |
self._GetMD5FileName(), |
|
59 |
"r").read().strip() + lib_ext |
|
60 |
except Exception, e: |
|
61 |
self.PLCStatus = "Empty" |
|
62 |
self.CurrentPLCFilename=None |
|
63 |
||
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
|
229 | 68 |
def _GetMD5FileName(self): |
69 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
70 |
||
71 |
def _GetLibFileName(self): |
|
72 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
73 |
||
74 |
||
75 |
def _LoadNewPLC(self): |
|
76 |
""" |
|
77 |
Load PLC library |
|
78 |
Declare all functions, arguments and return values |
|
79 |
""" |
|
80 |
print "Load PLC" |
|
81 |
try: |
|
82 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
83 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
84 |
||
85 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
86 |
self._startPLC.restype = ctypes.c_int |
|
87 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
88 |
||
89 |
self._stopPLC = self.PLClibraryHandle.stopPLC |
|
90 |
self._stopPLC.restype = None |
|
91 |
||
92 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
93 |
self._ResetDebugVariables.restype = None |
|
94 |
||
235 | 95 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 96 |
self._RegisterDebugVariable.restype = None |
235 | 97 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int] |
229 | 98 |
|
99 |
self._IterDebugData = self.PLClibraryHandle.IterDebugData |
|
100 |
self._IterDebugData.restype = ctypes.c_void_p |
|
101 |
self._IterDebugData.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p)] |
|
102 |
||
103 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
104 |
self._FreeDebugData.restype = None |
|
105 |
||
106 |
self._WaitDebugData = self.PLClibraryHandle.WaitDebugData |
|
107 |
self._WaitDebugData.restype = ctypes.c_int |
|
235 | 108 |
|
109 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
110 |
self._suspendDebug.restype = None |
|
111 |
||
112 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
113 |
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
|
114 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
115 |
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
|
116 |
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
|
117 |
self._PythonIterator.argtypes = [ctypes.c_char_p] |
235 | 118 |
|
229 | 119 |
return True |
120 |
except: |
|
121 |
print traceback.format_exc() |
|
122 |
return False |
|
123 |
||
124 |
def _FreePLC(self): |
|
125 |
""" |
|
126 |
Unload PLC library. |
|
127 |
This is also called by __init__ to create dummy C func proxies |
|
128 |
""" |
|
129 |
# Forget all refs to library |
|
130 |
self._startPLC = lambda:None |
|
131 |
self._stopPLC = lambda:None |
|
132 |
self._ResetDebugVariables = lambda:None |
|
133 |
self._RegisterDebugVariable = lambda x:None |
|
134 |
self._IterDebugData = lambda x,y:None |
|
135 |
self._FreeDebugData = lambda:None |
|
235 | 136 |
self._WaitDebugData = lambda:-1 |
137 |
self._suspendDebug = lambda:None |
|
138 |
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
|
139 |
self._PythonIterator = lambda:"" |
229 | 140 |
self.PLClibraryHandle = None |
141 |
# Unload library explicitely |
|
142 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
143 |
print "Unload PLC" |
|
144 |
dlclose(self._PLClibraryHandle) |
|
145 |
res = self._DetectDirtyLibs() |
|
146 |
else: |
|
147 |
res = False |
|
148 |
||
149 |
self._PLClibraryHandle = None |
|
150 |
||
151 |
return res |
|
152 |
||
153 |
def _DetectDirtyLibs(self): |
|
154 |
# Detect dirty libs |
|
155 |
# Get lib dependencies (for dirty lib detection) |
|
156 |
if os.name == "posix": |
|
157 |
# parasiting libs listed with ldd |
|
158 |
badlibs = [ toks.split()[0] for toks in commands.getoutput( |
|
159 |
"ldd "+self._GetLibFileName()).splitlines() ] |
|
160 |
for badlib in badlibs: |
|
161 |
if badlib[:6] in ["libwx_", |
|
162 |
"libwxs", |
|
163 |
"libgtk", |
|
164 |
"libgdk", |
|
165 |
"libatk", |
|
166 |
"libpan", |
|
167 |
"libX11", |
|
168 |
]: |
|
169 |
#badhandle = dlopen(badlib, dl.RTLD_NOLOAD) |
|
170 |
print "Dirty lib detected :" + badlib |
|
171 |
#dlclose(badhandle) |
|
172 |
return True |
|
173 |
return False |
|
174 |
||
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
175 |
def PythonThreadProc(self): |
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
|
176 |
print "PythonThreadProc 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
|
177 |
my_globs = 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
|
178 |
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
|
179 |
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
|
180 |
# 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
|
181 |
# 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
|
182 |
# or even call _PythonThreadProc itself. |
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
|
183 |
execfile(pyfile, my_globs) |
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
|
184 |
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
|
185 |
while 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
|
186 |
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
|
187 |
cmd = self._PythonIterator(res) |
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
|
188 |
print " -> ", cmd |
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
|
189 |
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
|
190 |
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
|
191 |
try : |
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
|
192 |
res = str(eval(cmd,my_globs)) |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
193 |
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
|
194 |
res = "#EXCEPTION : "+str(e) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
195 |
print res |
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
|
196 |
print "PythonThreadProc interrupted" |
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 |
if my_globs.get("_runtime_cleanup",None) 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
|
198 |
my_globs["_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
|
199 |
print "PythonThreadProc cleaned up" |
229 | 200 |
|
235 | 201 |
def StartPLC(self, debug=False): |
229 | 202 |
print "StartPLC" |
203 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
|
204 |
c_argv = ctypes.c_char_p * len(self.argv) |
|
205 |
if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
|
235 | 206 |
if debug: |
207 |
self._resumeDebug() |
|
229 | 208 |
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
|
209 |
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
|
210 |
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
|
211 |
self.PythonThread.start() |
229 | 212 |
return True |
213 |
else: |
|
214 |
print "_StartPLC did not return 0 !" |
|
215 |
self._DoStopPLC() |
|
216 |
return False |
|
217 |
||
218 |
def _DoStopPLC(self): |
|
219 |
self._stopPLC() |
|
220 |
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
|
221 |
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
|
222 |
if self.PythonThread.isAlive(): |
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
|
223 |
print "Python thread couldn't be killed" |
229 | 224 |
if self._FreePLC(): |
225 |
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
|
226 |
self.StatusChange() |
229 | 227 |
return True |
228 |
||
229 |
def StopPLC(self): |
|
230 |
if self.PLCStatus == "Started": |
|
231 |
self._DoStopPLC() |
|
232 |
return True |
|
233 |
return False |
|
234 |
||
235 |
def _Reload(self): |
|
236 |
self.daemon.shutdown(True) |
|
237 |
self.daemon.sock.close() |
|
238 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
239 |
# never reached |
|
240 |
return 0 |
|
241 |
||
242 |
def ForceReload(self): |
|
243 |
# respawn python interpreter |
|
244 |
Timer(0.1,self._Reload).start() |
|
245 |
return True |
|
246 |
||
247 |
def GetPLCstatus(self): |
|
248 |
return self.PLCStatus |
|
249 |
||
250 |
def NewPLC(self, md5sum, data, extrafiles): |
|
251 |
print "NewPLC (%s)"%md5sum |
|
252 |
if self.PLCStatus in ["Stopped", "Empty", "Dirty"]: |
|
253 |
NewFileName = md5sum + lib_ext |
|
254 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
255 |
try: |
|
256 |
os.remove(os.path.join(self.workingdir, |
|
257 |
self.CurrentPLCFilename)) |
|
258 |
for filename in file(extra_files_log, "r").readlines() + extra_files_log: |
|
259 |
try: |
|
260 |
os.remove(os.path.join(self.workingdir, filename)) |
|
261 |
except: |
|
262 |
pass |
|
263 |
except: |
|
264 |
pass |
|
265 |
||
266 |
try: |
|
267 |
# Create new PLC file |
|
268 |
open(os.path.join(self.workingdir,NewFileName), |
|
269 |
'wb').write(data) |
|
270 |
||
271 |
# Store new PLC filename based on md5 key |
|
272 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
273 |
||
274 |
# Then write the files |
|
275 |
log = file(extra_files_log, "w") |
|
276 |
for fname,fdata in extrafiles: |
|
277 |
fpath = os.path.join(self.workingdir,fname) |
|
278 |
open(fpath, "wb").write(fdata) |
|
279 |
log.write(fname+'\n') |
|
280 |
||
281 |
# Store new PLC filename |
|
282 |
self.CurrentPLCFilename = NewFileName |
|
283 |
except: |
|
284 |
print traceback.format_exc() |
|
285 |
return False |
|
286 |
if self.PLCStatus == "Empty": |
|
287 |
self.PLCStatus = "Stopped" |
|
288 |
return True |
|
289 |
return False |
|
290 |
||
291 |
def MatchMD5(self, MD5): |
|
292 |
try: |
|
293 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
294 |
return last_md5 == MD5 |
|
295 |
except: |
|
296 |
return False |
|
297 |
||
298 |
def SetTraceVariablesList(self, idxs): |
|
299 |
""" |
|
300 |
Call ctype imported function to append |
|
301 |
these indexes to registred variables in PLC debugger |
|
302 |
""" |
|
235 | 303 |
self._suspendDebug() |
229 | 304 |
# keep a copy of requested idx |
305 |
self._Idxs = idxs[:] |
|
306 |
self._ResetDebugVariables() |
|
307 |
for idx in idxs: |
|
308 |
self._RegisterDebugVariable(idx) |
|
235 | 309 |
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
|
310 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
311 |
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
|
312 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
313 |
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
|
314 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
315 |
_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
|
316 |
("body", ctypes.c_char * 40)] |
229 | 317 |
|
238 | 318 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
319 |
"STEP" : (ctypes.c_uint8, lambda x:x.value), |
|
320 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
|
321 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value), |
|
322 |
"SINT" : (ctypes.c_int8, lambda x:x.value), |
|
323 |
"USINT" : (ctypes.c_uint8, lambda x:x.value), |
|
324 |
"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
|
325 |
"STRING" : (IEC_STRING, lambda x:x.body[:x.len]), |
238 | 326 |
"INT" : (ctypes.c_int16, lambda x:x.value), |
327 |
"UINT" : (ctypes.c_uint16, lambda x:x.value), |
|
328 |
"WORD" : (ctypes.c_uint16, lambda x:x.value), |
|
329 |
"WSTRING" : (None, None),#TODO |
|
330 |
"DINT" : (ctypes.c_int32, lambda x:x.value), |
|
331 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value), |
|
332 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value), |
|
333 |
"LINT" : (ctypes.c_int64, lambda x:x.value), |
|
334 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value), |
|
335 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value), |
|
336 |
"REAL" : (ctypes.c_float, lambda x:x.value), |
|
337 |
"LREAL" : (ctypes.c_double, lambda x:x.value), |
|
229 | 338 |
} |
339 |
||
340 |
def GetTraceVariables(self): |
|
341 |
""" |
|
342 |
Return a list of variables, corresponding to the list of requiered idx |
|
343 |
""" |
|
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
|
344 |
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
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
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
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
|
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
|
353 |
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
|
354 |
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
|
355 |
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
|
356 |
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
|
357 |
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
|
358 |
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
|
359 |
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
|
360 |
print "Debug error idx : %d, expected_idx %d, type : %s"%(idx.value, given_idx,typename.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
|
361 |
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
|
362 |
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
|
363 |
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
|
364 |
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
|
365 |
|
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 |
|
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 |