author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 06 Oct 2016 11:30:04 +0300 | |
changeset 1540 | cf1df00e6f86 |
parent 1463 | de311ffe3961 |
child 1570 | 0925da818853 |
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 |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
229 | 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 |
|
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
26 |
from threading import Timer, Thread, Lock, Semaphore, Event |
301 | 27 |
import ctypes, os, commands, types, sys |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
28 |
from targets.typemapping import LogLevelsDefault, LogLevelsCount, TypeTranslator, UnpackDebugBuffer |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
29 |
from time import time |
917 | 30 |
|
301 | 31 |
|
229 | 32 |
if os.name in ("nt", "ce"): |
33 |
from _ctypes import LoadLibrary as dlopen |
|
34 |
from _ctypes import FreeLibrary as dlclose |
|
35 |
elif os.name == "posix": |
|
36 |
from _ctypes import dlopen, dlclose |
|
37 |
||
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
38 |
import traceback |
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
39 |
def get_last_traceback(tb): |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
40 |
while tb.tb_next: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
41 |
tb = tb.tb_next |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
42 |
return tb |
229 | 43 |
|
44 |
lib_ext ={ |
|
45 |
"linux2":".so", |
|
46 |
"win32":".dll", |
|
47 |
}.get(sys.platform, "") |
|
48 |
||
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
49 |
def PLCprint(message): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
50 |
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
|
51 |
sys.stdout.flush() |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
52 |
|
229 | 53 |
class PLCObject(pyro.ObjBase): |
1438
19ebe96b41c0
Moved twisted/nevow/athena away from Berermiz_service.py + some minor cleanup
Edouard Tisserant
parents:
1435
diff
changeset
|
54 |
def __init__(self, workingdir, daemon, argv, statuschange, evaluator, pyruntimevars): |
229 | 55 |
pyro.ObjBase.__init__(self) |
301 | 56 |
self.evaluator = evaluator |
229 | 57 |
self.argv = [workingdir] + argv # force argv[0] to be "path" to exec... |
58 |
self.workingdir = workingdir |
|
1447
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
59 |
self.PLCStatus = "Empty" |
229 | 60 |
self.PLClibraryHandle = None |
352 | 61 |
self.PLClibraryLock = Lock() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
62 |
self.DummyIteratorLock = None |
229 | 63 |
# Creates fake C funcs proxies |
64 |
self._FreePLC() |
|
65 |
self.daemon = daemon |
|
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
66 |
self.statuschange = statuschange |
301 | 67 |
self.hmi_frame = None |
1438
19ebe96b41c0
Moved twisted/nevow/athena away from Berermiz_service.py + some minor cleanup
Edouard Tisserant
parents:
1435
diff
changeset
|
68 |
self.pyruntimevars = pyruntimevars |
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
69 |
self._loading_error = None |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
70 |
self.python_runtime_vars = None |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
71 |
self.TraceThread = None |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
72 |
self.TraceLock = Lock() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
73 |
self.TraceWakeup = Event() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
74 |
self.Traces = [] |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
75 |
|
1447
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
76 |
def AutoLoad(self): |
229 | 77 |
# Get the last transfered PLC if connector must be restart |
78 |
try: |
|
79 |
self.CurrentPLCFilename=open( |
|
80 |
self._GetMD5FileName(), |
|
81 |
"r").read().strip() + lib_ext |
|
1121
d3838e8f1b90
Fixed Beremiz_service not closing on Windows
Laurent Bessard
parents:
1093
diff
changeset
|
82 |
self.LoadPLC() |
229 | 83 |
except Exception, e: |
84 |
self.PLCStatus = "Empty" |
|
85 |
self.CurrentPLCFilename=None |
|
86 |
||
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
|
87 |
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
|
88 |
if self.statuschange is not None: |
1438
19ebe96b41c0
Moved twisted/nevow/athena away from Berermiz_service.py + some minor cleanup
Edouard Tisserant
parents:
1435
diff
changeset
|
89 |
for callee in self.statuschange: |
19ebe96b41c0
Moved twisted/nevow/athena away from Berermiz_service.py + some minor cleanup
Edouard Tisserant
parents:
1435
diff
changeset
|
90 |
callee(self.PLCStatus) |
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
|
91 |
|
917 | 92 |
def LogMessage(self, *args): |
93 |
if len(args) == 2: |
|
94 |
level, msg = args |
|
95 |
else: |
|
96 |
level = LogLevelsDefault |
|
97 |
msg, = args |
|
98 |
return self._LogMessage(level, msg, len(msg)) |
|
99 |
||
1093 | 100 |
def ResetLogCount(self): |
101 |
if self._ResetLogCount is not None: |
|
102 |
self._ResetLogCount() |
|
917 | 103 |
|
104 |
def GetLogCount(self, level): |
|
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
105 |
if self._GetLogCount is not None : |
917 | 106 |
return int(self._GetLogCount(level)) |
107 |
elif self._loading_error is not None and level==0: |
|
1093 | 108 |
return 1 |
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
109 |
|
917 | 110 |
def GetLogMessage(self, level, msgid): |
921 | 111 |
tick = ctypes.c_uint32() |
112 |
tv_sec = ctypes.c_uint32() |
|
113 |
tv_nsec = ctypes.c_uint32() |
|
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
114 |
if self._GetLogMessage is not None: |
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
115 |
maxsz = len(self._log_read_buffer)-1 |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
116 |
sz = self._GetLogMessage(level, msgid, |
921 | 117 |
self._log_read_buffer, maxsz, |
118 |
ctypes.byref(tick), |
|
119 |
ctypes.byref(tv_sec), |
|
120 |
ctypes.byref(tv_nsec)) |
|
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
121 |
if sz and sz <= maxsz: |
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
122 |
self._log_read_buffer[sz] = '\x00' |
921 | 123 |
return self._log_read_buffer.value,tick.value,tv_sec.value,tv_nsec.value |
917 | 124 |
elif self._loading_error is not None and level==0: |
921 | 125 |
return self._loading_error,0,0,0 |
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
126 |
return None |
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
127 |
|
229 | 128 |
def _GetMD5FileName(self): |
129 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
130 |
||
131 |
def _GetLibFileName(self): |
|
132 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
133 |
||
134 |
||
1027
4e44c2c3e081
Fixed bug when starting Beremiz_runtime.py non empty (-a)
Edouard Tisserant
parents:
1014
diff
changeset
|
135 |
def LoadPLC(self): |
229 | 136 |
""" |
137 |
Load PLC library |
|
138 |
Declare all functions, arguments and return values |
|
139 |
""" |
|
1457
ff7cfce737ca
Added PLCID variable accessible from C side, set with binarie's MD5. Added retain init and cleanup calls. Extended tests/python to test PLCID
Edouard Tisserant
parents:
1447
diff
changeset
|
140 |
md5 = open(self._GetMD5FileName(), "r").read() |
229 | 141 |
try: |
142 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
143 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
144 |
|
1463
de311ffe3961
Changed runtime's global PLCID to PLC_ID, working around redefinition in windoze' headers.
Edouard Tisserant
parents:
1457
diff
changeset
|
145 |
self.PLC_ID = ctypes.c_char_p.in_dll(self.PLClibraryHandle, "PLC_ID") |
1457
ff7cfce737ca
Added PLCID variable accessible from C side, set with binarie's MD5. Added retain init and cleanup calls. Extended tests/python to test PLCID
Edouard Tisserant
parents:
1447
diff
changeset
|
146 |
if len(md5) == 32 : |
1463
de311ffe3961
Changed runtime's global PLCID to PLC_ID, working around redefinition in windoze' headers.
Edouard Tisserant
parents:
1457
diff
changeset
|
147 |
self.PLC_ID.value = md5 |
1457
ff7cfce737ca
Added PLCID variable accessible from C side, set with binarie's MD5. Added retain init and cleanup calls. Extended tests/python to test PLCID
Edouard Tisserant
parents:
1447
diff
changeset
|
148 |
|
229 | 149 |
self._startPLC = self.PLClibraryHandle.startPLC |
150 |
self._startPLC.restype = ctypes.c_int |
|
151 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
152 |
|
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
153 |
self._stopPLC_real = self.PLClibraryHandle.stopPLC |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
154 |
self._stopPLC_real.restype = None |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
155 |
|
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
156 |
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
|
157 |
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
|
158 |
self._PythonIterator.restype = ctypes.c_char_p |
851
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
159 |
self._PythonIterator.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_void_p)] |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
160 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
161 |
self._stopPLC = self._stopPLC_real |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
162 |
else: |
717 | 163 |
# If python confnode is not enabled, we reuse _PythonIterator |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
164 |
# as a call that block pythonthread until StopPLC |
1442
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
165 |
self.PlcStopping = Event() |
868
7e5da4962bea
Fix bug of PythonIterator signature in PLCObject when not using PythonLibrary
Edouard Tisserant
parents:
867
diff
changeset
|
166 |
def PythonIterator(res, blkid): |
1442
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
167 |
self.PlcStopping.clear() |
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
168 |
self.PlcStopping.wait() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
169 |
return None |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
170 |
self._PythonIterator = PythonIterator |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
171 |
|
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
172 |
def __StopPLC(): |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
173 |
self._stopPLC_real() |
1442
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
174 |
self.PlcStopping.set() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
175 |
self._stopPLC = __StopPLC |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
176 |
|
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
177 |
|
229 | 178 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
179 |
self._ResetDebugVariables.restype = None |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
180 |
|
235 | 181 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 182 |
self._RegisterDebugVariable.restype = None |
477
f66a092b6e74
Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
467
diff
changeset
|
183 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int, ctypes.c_void_p] |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
184 |
|
229 | 185 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
186 |
self._FreeDebugData.restype = None |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
187 |
|
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
188 |
self._GetDebugData = self.PLClibraryHandle.GetDebugData |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
189 |
self._GetDebugData.restype = ctypes.c_int |
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
190 |
self._GetDebugData.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p)] |
235 | 191 |
|
192 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
614 | 193 |
self._suspendDebug.restype = ctypes.c_int |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
194 |
self._suspendDebug.argtypes = [ctypes.c_int] |
235 | 195 |
|
196 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
197 |
self._resumeDebug.restype = None |
|
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
198 |
|
1093 | 199 |
self._ResetLogCount = self.PLClibraryHandle.ResetLogCount |
200 |
self._ResetLogCount.restype = None |
|
201 |
||
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
202 |
self._GetLogCount = self.PLClibraryHandle.GetLogCount |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
203 |
self._GetLogCount.restype = ctypes.c_uint32 |
917 | 204 |
self._GetLogCount.argtypes = [ctypes.c_uint8] |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
205 |
|
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
206 |
self._LogMessage = self.PLClibraryHandle.LogMessage |
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
207 |
self._LogMessage.restype = ctypes.c_int |
971
c4550f76ae05
reverted PLCObject.py. ctypes.POINTER(ctypes.c_uint8) != string
Edouard Tisserant
parents:
969
diff
changeset
|
208 |
self._LogMessage.argtypes = [ctypes.c_uint8, ctypes.c_char_p, ctypes.c_uint32] |
1093 | 209 |
|
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
210 |
self._log_read_buffer = ctypes.create_string_buffer(1<<14) #16K |
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
211 |
self._GetLogMessage = self.PLClibraryHandle.GetLogMessage |
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
212 |
self._GetLogMessage.restype = ctypes.c_uint32 |
921 | 213 |
self._GetLogMessage.argtypes = [ctypes.c_uint8, ctypes.c_uint32, ctypes.c_char_p, ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32)] |
911
ffa24427396a
Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents:
906
diff
changeset
|
214 |
|
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
215 |
self._loading_error = None |
1035
0f905e027d18
Better mdns resolution failure signaling, added fixed bug whith runtime autostart
Edouard Tisserant
parents:
1027
diff
changeset
|
216 |
|
0f905e027d18
Better mdns resolution failure signaling, added fixed bug whith runtime autostart
Edouard Tisserant
parents:
1027
diff
changeset
|
217 |
self.PythonRuntimeInit() |
0f905e027d18
Better mdns resolution failure signaling, added fixed bug whith runtime autostart
Edouard Tisserant
parents:
1027
diff
changeset
|
218 |
|
229 | 219 |
return True |
220 |
except: |
|
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
221 |
self._loading_error = traceback.format_exc() |
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
222 |
PLCprint(self._loading_error) |
229 | 223 |
return False |
224 |
||
1045
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
225 |
def UnLoadPLC(self): |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
226 |
self.PythonRuntimeCleanup() |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
227 |
self._FreePLC() |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
228 |
|
229 | 229 |
def _FreePLC(self): |
230 |
""" |
|
231 |
Unload PLC library. |
|
232 |
This is also called by __init__ to create dummy C func proxies |
|
233 |
""" |
|
352 | 234 |
self.PLClibraryLock.acquire() |
229 | 235 |
# Forget all refs to library |
1027
4e44c2c3e081
Fixed bug when starting Beremiz_runtime.py non empty (-a)
Edouard Tisserant
parents:
1014
diff
changeset
|
236 |
self._startPLC = lambda x,y:None |
229 | 237 |
self._stopPLC = lambda:None |
238 |
self._ResetDebugVariables = lambda:None |
|
479
c28f40b27798
Bug on RegisterDebugVariable when no PLC running fixed
laurent
parents:
477
diff
changeset
|
239 |
self._RegisterDebugVariable = lambda x, y:None |
229 | 240 |
self._IterDebugData = lambda x,y:None |
241 |
self._FreeDebugData = lambda:None |
|
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
242 |
self._GetDebugData = lambda:-1 |
614 | 243 |
self._suspendDebug = lambda x:-1 |
235 | 244 |
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
|
245 |
self._PythonIterator = lambda:"" |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
246 |
self._GetLogCount = None |
917 | 247 |
self._LogMessage = lambda l,m,s:PLCprint("OFF LOG :"+m) |
914
94436558f0ce
More stable logging. Added small one-entry log for loading errors. Test now include python side concurrent logging
Edouard Tisserant
parents:
911
diff
changeset
|
248 |
self._GetLogMessage = None |
229 | 249 |
self.PLClibraryHandle = None |
250 |
# Unload library explicitely |
|
251 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
252 |
dlclose(self._PLClibraryHandle) |
|
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
253 |
self._PLClibraryHandle = None |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
254 |
|
352 | 255 |
self.PLClibraryLock.release() |
229 | 256 |
return False |
257 |
||
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
258 |
def PythonRuntimeCall(self, methodname): |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
259 |
""" |
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
260 |
Calls init, start, stop or cleanup method provided by |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
261 |
runtime python files, loaded when new PLC uploaded |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
262 |
""" |
1051
847d68c3e7ff
Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents:
1045
diff
changeset
|
263 |
for method in self.python_runtime_vars.get("_runtime_%s"%methodname, []): |
847d68c3e7ff
Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents:
1045
diff
changeset
|
264 |
res,exp = self.evaluator(method) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
265 |
if exp is not None: |
1052
fa7c5034c1d2
Better display of Python exceptions from Py_Eval
Edouard Tisserant
parents:
1051
diff
changeset
|
266 |
self.LogMessage(0,'\n'.join(traceback.format_exception(*exp))) |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
267 |
|
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
268 |
def PythonRuntimeInit(self): |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
269 |
MethodNames = ["init", "start", "stop", "cleanup"] |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
270 |
self.python_runtime_vars = globals().copy() |
1438
19ebe96b41c0
Moved twisted/nevow/athena away from Berermiz_service.py + some minor cleanup
Edouard Tisserant
parents:
1435
diff
changeset
|
271 |
self.python_runtime_vars.update(self.pyruntimevars) |
19ebe96b41c0
Moved twisted/nevow/athena away from Berermiz_service.py + some minor cleanup
Edouard Tisserant
parents:
1435
diff
changeset
|
272 |
|
1144
21475ee0e688
Added stub code and declarations for bidirectional access to PLC globals from python code (untested)
Edouard Tisserant
parents:
1132
diff
changeset
|
273 |
class PLCSafeGlobals: |
1145
203f4eff3313
Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents:
1144
diff
changeset
|
274 |
def __getattr__(_self, name): |
1156
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
275 |
try : |
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
276 |
t = self.python_runtime_vars["_"+name+"_ctype"] |
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
277 |
except KeyError: |
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
278 |
raise KeyError("Try to get unknown shared global variable : %s"%name) |
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
279 |
v = t() |
1145
203f4eff3313
Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents:
1144
diff
changeset
|
280 |
r = self.python_runtime_vars["_PySafeGetPLCGlob_"+name](ctypes.byref(v)) |
203f4eff3313
Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents:
1144
diff
changeset
|
281 |
return self.python_runtime_vars["_"+name+"_unpack"](v) |
203f4eff3313
Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents:
1144
diff
changeset
|
282 |
def __setattr__(_self, name, value): |
1156
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
283 |
try : |
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
284 |
t = self.python_runtime_vars["_"+name+"_ctype"] |
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
285 |
except KeyError: |
9708ed2a4ac2
Added more clear error message in case of access to non declared PLC global from python code
Edouard Tisserant
parents:
1145
diff
changeset
|
286 |
raise KeyError("Try to set unknown shared global variable : %s"%name) |
1145
203f4eff3313
Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents:
1144
diff
changeset
|
287 |
v = self.python_runtime_vars["_"+name+"_pack"](t,value) |
203f4eff3313
Fixed PLC global var access from python. Added test in tests/python
Edouard Tisserant
parents:
1144
diff
changeset
|
288 |
self.python_runtime_vars["_PySafeSetPLCGlob_"+name](ctypes.byref(v)) |
1447
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
289 |
|
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
290 |
self.python_runtime_vars.update({ |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
291 |
"PLCGlobals" : PLCSafeGlobals(), |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
292 |
"WorkingDir" : self.workingdir, |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
293 |
"PLCObject" : self, |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
294 |
"PLCBinary" : self.PLClibraryHandle, |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
295 |
"PLCGlobalsDesc" : []}) |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
296 |
|
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
297 |
for methodname in MethodNames : |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
298 |
self.python_runtime_vars["_runtime_%s"%methodname] = [] |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
299 |
|
972 | 300 |
try: |
1447
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
301 |
filenames = os.listdir(self.workingdir) |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
302 |
filenames.sort() |
d6b878525ceb
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Edouard Tisserant
parents:
1442
diff
changeset
|
303 |
for filename in filenames: |
972 | 304 |
name, ext = os.path.splitext(filename) |
305 |
if name.upper().startswith("RUNTIME") and ext.upper() == ".PY": |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
306 |
execfile(os.path.join(self.workingdir, filename), self.python_runtime_vars) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
307 |
for methodname in MethodNames: |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
308 |
method = self.python_runtime_vars.get("_%s_%s" % (name, methodname), None) |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
309 |
if method is not None: |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
310 |
self.python_runtime_vars["_runtime_%s"%methodname].append(method) |
972 | 311 |
except: |
312 |
self.LogMessage(0,traceback.format_exc()) |
|
313 |
raise |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
314 |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
315 |
self.PythonRuntimeCall("init") |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
316 |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
317 |
|
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
318 |
|
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
319 |
def PythonRuntimeCleanup(self): |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
320 |
if self.python_runtime_vars is not None: |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
321 |
self.PythonRuntimeCall("cleanup") |
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
322 |
|
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
323 |
self.python_runtime_vars = None |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
324 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
325 |
def PythonThreadProc(self): |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
326 |
self.StartSem.release() |
851
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
327 |
res,cmd,blkid = "None","None",ctypes.c_void_p() |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
328 |
compile_cache={} |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
329 |
while True: |
851
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
330 |
# print "_PythonIterator(", res, ")", |
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
331 |
cmd = self._PythonIterator(res,blkid) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
332 |
FBID = blkid.value |
851
666f5bdad301
Added FBID variable to PY_EVAL evaluation context. FBID does identify uniquely py_eval block instance triggering execution
Edouard Tisserant
parents:
798
diff
changeset
|
333 |
# print " -> ", cmd, blkid |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
334 |
if cmd is None: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
335 |
break |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
336 |
try : |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
337 |
self.python_runtime_vars["FBID"]=FBID |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
338 |
ccmd,AST =compile_cache.get(FBID, (None,None)) |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
339 |
if ccmd is None or ccmd!=cmd: |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
340 |
AST = compile(cmd, '<plc>', 'eval') |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
341 |
compile_cache[FBID]=(cmd,AST) |
1288
adc79fc44079
Fixed two typos in py_ext : FBID was not current but previous py_eval block FBID, and compiled AST cache was filled buy never used.
Edouard Tisserant
parents:
1156
diff
changeset
|
342 |
result,exp = self.evaluator(eval,AST,self.python_runtime_vars) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
343 |
if exp is not None: |
1052
fa7c5034c1d2
Better display of Python exceptions from Py_Eval
Edouard Tisserant
parents:
1051
diff
changeset
|
344 |
res = "#EXCEPTION : "+str(exp[1]) |
fa7c5034c1d2
Better display of Python exceptions from Py_Eval
Edouard Tisserant
parents:
1051
diff
changeset
|
345 |
self.LogMessage(1,('PyEval@0x%x(Code="%s") Exception "%s"')%(FBID,cmd, |
fa7c5034c1d2
Better display of Python exceptions from Py_Eval
Edouard Tisserant
parents:
1051
diff
changeset
|
346 |
'\n'.join(traceback.format_exception(*exp)))) |
867
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
347 |
else: |
06495975e8a4
Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents:
851
diff
changeset
|
348 |
res=str(result) |
1014
e2f7d6c95db0
Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents:
1011
diff
changeset
|
349 |
self.python_runtime_vars["FBID"]=None |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
350 |
except Exception,e: |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
351 |
res = "#EXCEPTION : "+str(e) |
972 | 352 |
self.LogMessage(1,('PyEval@0x%x(Code="%s") Exception "%s"')%(FBID,cmd,str(e))) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
353 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
354 |
def StartPLC(self): |
465
67d32a91d70b
Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
462
diff
changeset
|
355 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
356 |
c_argv = ctypes.c_char_p * len(self.argv) |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
357 |
error = None |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
358 |
res = self._startPLC(len(self.argv),c_argv(*self.argv)) |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
359 |
if res == 0: |
1442
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
360 |
self.PLCStatus = "Started" |
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
361 |
self.StatusChange() |
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
362 |
self.PythonRuntimeCall("start") |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
363 |
self.StartSem=Semaphore(0) |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
364 |
self.PythonThread = Thread(target=self.PythonThreadProc) |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
365 |
self.PythonThread.start() |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
366 |
self.StartSem.acquire() |
917 | 367 |
self.LogMessage("PLC started") |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
368 |
else: |
972 | 369 |
self.LogMessage(0,_("Problem starting PLC : error %d" % res)) |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
370 |
self.PLCStatus = "Broken" |
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
371 |
self.StatusChange() |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
372 |
|
229 | 373 |
def StopPLC(self): |
374 |
if self.PLCStatus == "Started": |
|
917 | 375 |
self.LogMessage("PLC stopped") |
352 | 376 |
self._stopPLC() |
798
0d2423f283a6
Fix bug segmentation fault while cleanup extensions
laurent
parents:
795
diff
changeset
|
377 |
self.PythonThread.join() |
1442
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
378 |
self.PLCStatus = "Stopped" |
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
379 |
self.StatusChange() |
ad9a7853dea2
Fixed race condition preventing to stop PLC through WAMP
Edouard Tisserant
parents:
1440
diff
changeset
|
380 |
self.PythonRuntimeCall("stop") |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
381 |
if self.TraceThread is not None : |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
382 |
self.TraceWakeup.set() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
383 |
self.TraceThread.join() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
384 |
self.TraceThread = None |
229 | 385 |
return True |
386 |
return False |
|
387 |
||
388 |
def _Reload(self): |
|
389 |
self.daemon.shutdown(True) |
|
390 |
self.daemon.sock.close() |
|
391 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
392 |
# never reached |
|
393 |
return 0 |
|
394 |
||
395 |
def ForceReload(self): |
|
396 |
# respawn python interpreter |
|
397 |
Timer(0.1,self._Reload).start() |
|
398 |
return True |
|
399 |
||
400 |
def GetPLCstatus(self): |
|
917 | 401 |
return self.PLCStatus, map(self.GetLogCount,xrange(LogLevelsCount)) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
402 |
|
229 | 403 |
def NewPLC(self, md5sum, data, extrafiles): |
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
404 |
if self.PLCStatus in ["Stopped", "Empty", "Broken"]: |
229 | 405 |
NewFileName = md5sum + lib_ext |
406 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
407 |
|
1045
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
408 |
self.UnLoadPLC() |
a220a27defe5
Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents:
1035
diff
changeset
|
409 |
|
1011
388777d307de
Fixed bug when transferring New PLC to a non-empty pyro connector
Laurent Bessard
parents:
972
diff
changeset
|
410 |
self.LogMessage("NewPLC (%s)"%md5sum) |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
411 |
self.PLCStatus = "Empty" |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
412 |
|
229 | 413 |
try: |
414 |
os.remove(os.path.join(self.workingdir, |
|
415 |
self.CurrentPLCFilename)) |
|
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
416 |
for filename in file(extra_files_log, "r").readlines() + [extra_files_log]: |
229 | 417 |
try: |
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
418 |
os.remove(os.path.join(self.workingdir, filename.strip())) |
229 | 419 |
except: |
420 |
pass |
|
421 |
except: |
|
422 |
pass |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
423 |
|
229 | 424 |
try: |
425 |
# Create new PLC file |
|
426 |
open(os.path.join(self.workingdir,NewFileName), |
|
427 |
'wb').write(data) |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
428 |
|
229 | 429 |
# Store new PLC filename based on md5 key |
430 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
431 |
|
229 | 432 |
# Then write the files |
433 |
log = file(extra_files_log, "w") |
|
434 |
for fname,fdata in extrafiles: |
|
435 |
fpath = os.path.join(self.workingdir,fname) |
|
436 |
open(fpath, "wb").write(fdata) |
|
437 |
log.write(fname+'\n') |
|
438 |
||
439 |
# Store new PLC filename |
|
440 |
self.CurrentPLCFilename = NewFileName |
|
441 |
except: |
|
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
442 |
self.PLCStatus = "Broken" |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
443 |
self.StatusChange() |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
444 |
PLCprint(traceback.format_exc()) |
229 | 445 |
return False |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
446 |
|
1027
4e44c2c3e081
Fixed bug when starting Beremiz_runtime.py non empty (-a)
Edouard Tisserant
parents:
1014
diff
changeset
|
447 |
if self.LoadPLC(): |
229 | 448 |
self.PLCStatus = "Stopped" |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
449 |
else: |
1035
0f905e027d18
Better mdns resolution failure signaling, added fixed bug whith runtime autostart
Edouard Tisserant
parents:
1027
diff
changeset
|
450 |
self.PLCStatus = "Broken" |
906
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
451 |
self._FreePLC() |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
452 |
self.StatusChange() |
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
453 |
|
de452d65865c
Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents:
868
diff
changeset
|
454 |
return self.PLCStatus == "Stopped" |
229 | 455 |
return False |
456 |
||
457 |
def MatchMD5(self, MD5): |
|
458 |
try: |
|
459 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
460 |
return last_md5 == MD5 |
|
461 |
except: |
|
1440
e8daabf2c438
Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents:
1438
diff
changeset
|
462 |
pass |
e8daabf2c438
Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents:
1438
diff
changeset
|
463 |
return False |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
464 |
|
229 | 465 |
def SetTraceVariablesList(self, idxs): |
466 |
""" |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
467 |
Call ctype imported function to append |
229 | 468 |
these indexes to registred variables in PLC debugger |
469 |
""" |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
470 |
if idxs: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
471 |
# suspend but dont disable |
614 | 472 |
if self._suspendDebug(False) == 0: |
473 |
# keep a copy of requested idx |
|
474 |
self._ResetDebugVariables() |
|
475 |
for idx,iectype,force in idxs: |
|
476 |
if force !=None: |
|
477 |
c_type,unpack_func, pack_func = \ |
|
478 |
TypeTranslator.get(iectype, |
|
479 |
(None,None,None)) |
|
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
480 |
force = ctypes.byref(pack_func(c_type,force)) |
614 | 481 |
self._RegisterDebugVariable(idx, force) |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
482 |
self._TracesSwap() |
614 | 483 |
self._resumeDebug() |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
484 |
else: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
485 |
self._suspendDebug(True) |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
486 |
|
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
487 |
def _TracesPush(self, trace): |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
488 |
self.TraceLock.acquire() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
489 |
lT = len(self.Traces) |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
490 |
if lT != 0 and lT * len(self.Traces[0]) > 1024 * 1024 : |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
491 |
self.Traces.pop(0) |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
492 |
self.Traces.append(trace) |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
493 |
self.TraceLock.release() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
494 |
|
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
495 |
def _TracesSwap(self): |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
496 |
self.LastSwapTrace = time() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
497 |
if self.TraceThread is None and self.PLCStatus == "Started": |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
498 |
self.TraceThread = Thread(target=self.TraceThreadProc) |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
499 |
self.TraceThread.start() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
500 |
self.TraceLock.acquire() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
501 |
Traces = self.Traces |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
502 |
self.Traces = [] |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
503 |
self.TraceLock.release() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
504 |
self.TraceWakeup.set() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
505 |
return Traces |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
506 |
|
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
507 |
def _TracesAutoSuspend(self): |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
508 |
# TraceProc stops here if Traces not polled for 3 seconds |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
509 |
traces_age = time() - self.LastSwapTrace |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
510 |
if traces_age > 3: |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
511 |
self.TraceLock.acquire() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
512 |
self.Traces = [] |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
513 |
self.TraceLock.release() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
514 |
self._suspendDebug(True) # Disable debugger |
1435
291a17b755d1
Fixed python runtime trace thread auto suspend. Now suspends after 3 seconds when no trace is requested
Edouard Tisserant
parents:
1434
diff
changeset
|
515 |
self.TraceWakeup.clear() |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
516 |
self.TraceWakeup.wait() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
517 |
self._resumeDebug() # Re-enable debugger |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
518 |
|
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
519 |
def _TracesFlush(self): |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
520 |
self.TraceLock.acquire() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
521 |
self.Traces = [] |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
522 |
self.TraceLock.release() |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
523 |
|
229 | 524 |
def GetTraceVariables(self): |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
525 |
return self.PLCStatus, self._TracesSwap() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
526 |
|
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
527 |
def TraceThreadProc(self): |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
528 |
""" |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
529 |
Return a list of traces, corresponding to the list of required idx |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
530 |
""" |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
531 |
while self.PLCStatus == "Started" : |
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
532 |
tick = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
533 |
size = ctypes.c_uint32() |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
534 |
buff = ctypes.c_void_p() |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
535 |
TraceBuffer = None |
795
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
536 |
if self.PLClibraryLock.acquire(False): |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
537 |
if self._GetDebugData(ctypes.byref(tick), |
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
538 |
ctypes.byref(size), |
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
1074
diff
changeset
|
539 |
ctypes.byref(buff)) == 0: |
795
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
540 |
if size.value: |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
541 |
TraceBuffer = ctypes.string_at(buff.value, size.value) |
795
afcc13faecd5
Fix bug debugger unable to restart after stopping PLC
laurent
parents:
734
diff
changeset
|
542 |
self._FreeDebugData() |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
479
diff
changeset
|
543 |
self.PLClibraryLock.release() |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
544 |
if TraceBuffer is not None: |
1434
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
545 |
self._TracesPush((tick.value, TraceBuffer)) |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
546 |
self._TracesAutoSuspend() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
547 |
self._TracesFlush() |
6e0cd0ceabb7
Added runtime side trace buffer, handled in a separate thread, limited to 1MB, and dropped after 3 seconds if not used by IDE. GetTraceVariables is not anymore blocking on next PLC cycle
Edouard Tisserant
parents:
1433
diff
changeset
|
548 |
|
592 | 549 |
|
1440
e8daabf2c438
Runtime : Added PLCobject methods registring. IDE : Added WAMP connector. Still need some fixes
Edouard Tisserant
parents:
1438
diff
changeset
|
550 |
def RemoteExec(self, script, *kwargs): |
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
551 |
try: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
552 |
exec script in kwargs |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
553 |
except: |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
554 |
e_type, e_value, e_traceback = sys.exc_info() |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
555 |
line_no = traceback.tb_lineno(get_last_traceback(e_traceback)) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
556 |
return (-1, "RemoteExec script failed!\n\nLine %d: %s\n\t%s" % |
699
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
557 |
(line_no, e_value, script.splitlines()[line_no - 1])) |
6ff64cadb1ff
Adding support for executing python scripts on remote runtime
laurent
parents:
690
diff
changeset
|
558 |
return (0, kwargs.get("returnVal", None)) |
1433
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
559 |
|
4a45f6642523
Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents:
1288
diff
changeset
|
560 |