author | laurent |
Sun, 06 Dec 2009 21:20:21 +0100 | |
changeset 463 | 961bddcfc913 |
parent 462 | 274e83a5534e |
child 465 | 67d32a91d70b |
permissions | -rwxr-xr-x |
229 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import Pyro.core as pyro |
|
352 | 26 |
from threading import Timer, Thread, Lock |
301 | 27 |
import ctypes, os, commands, types, sys |
28 |
||
229 | 29 |
if os.name in ("nt", "ce"): |
30 |
from _ctypes import LoadLibrary as dlopen |
|
31 |
from _ctypes import FreeLibrary as dlclose |
|
32 |
elif os.name == "posix": |
|
33 |
from _ctypes import dlopen, dlclose |
|
34 |
||
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
35 |
import traceback |
229 | 36 |
|
37 |
lib_ext ={ |
|
38 |
"linux2":".so", |
|
39 |
"win32":".dll", |
|
40 |
}.get(sys.platform, "") |
|
41 |
||
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
42 |
def PLCprint(message): |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
43 |
sys.stdout.write("PLCobject : "+message+"\n") |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
44 |
sys.stdout.flush() |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
45 |
|
229 | 46 |
class PLCObject(pyro.ObjBase): |
235 | 47 |
_Idxs = [] |
368 | 48 |
def __init__(self, workingdir, daemon, argv, statuschange, evaluator, website): |
229 | 49 |
pyro.ObjBase.__init__(self) |
301 | 50 |
self.evaluator = evaluator |
229 | 51 |
self.argv = [workingdir] + argv # force argv[0] to be "path" to exec... |
52 |
self.workingdir = workingdir |
|
53 |
self.PLCStatus = "Stopped" |
|
54 |
self.PLClibraryHandle = None |
|
352 | 55 |
self.PLClibraryLock = Lock() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
56 |
self.DummyIteratorLock = None |
229 | 57 |
# Creates fake C funcs proxies |
58 |
self._FreePLC() |
|
59 |
self.daemon = daemon |
|
269
d29c5f71574f
add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents:
239
diff
changeset
|
60 |
self.statuschange = statuschange |
301 | 61 |
self.hmi_frame = None |
368 | 62 |
self.website = website |
229 | 63 |
|
64 |
# Get the last transfered PLC if connector must be restart |
|
65 |
try: |
|
66 |
self.CurrentPLCFilename=open( |
|
67 |
self._GetMD5FileName(), |
|
68 |
"r").read().strip() + lib_ext |
|
69 |
except Exception, e: |
|
70 |
self.PLCStatus = "Empty" |
|
71 |
self.CurrentPLCFilename=None |
|
72 |
||
286
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
73 |
def StatusChange(self): |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
74 |
if self.statuschange is not None: |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
75 |
self.statuschange(self.PLCStatus) |
a2a8a52b0d4f
Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents:
283
diff
changeset
|
76 |
|
229 | 77 |
def _GetMD5FileName(self): |
78 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
79 |
||
80 |
def _GetLibFileName(self): |
|
81 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
82 |
||
83 |
||
84 |
def _LoadNewPLC(self): |
|
85 |
""" |
|
86 |
Load PLC library |
|
87 |
Declare all functions, arguments and return values |
|
88 |
""" |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
89 |
PLCprint("Load PLC") |
229 | 90 |
try: |
91 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
92 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
93 |
||
94 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
95 |
self._startPLC.restype = ctypes.c_int |
|
96 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
97 |
||
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
98 |
self._stopPLC_real = self.PLClibraryHandle.stopPLC |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
99 |
self._stopPLC_real.restype = None |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
100 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
101 |
self._PythonIterator = getattr(self.PLClibraryHandle, "PythonIterator", None) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
102 |
if self._PythonIterator is not None: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
103 |
self._PythonIterator.restype = ctypes.c_char_p |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
104 |
self._PythonIterator.argtypes = [ctypes.c_char_p] |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
105 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
106 |
def StopPLCLock(): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
107 |
self.PLClibraryLock.acquire() |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
108 |
self._stopPLC_real() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
109 |
self.PLClibraryLock.release() |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
110 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
111 |
else: |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
112 |
# If python plugin is not enabled, we reuse _PythonIterator |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
113 |
# as a call that block pythonthread until StopPLC |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
114 |
self.PythonIteratorLock = Lock() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
115 |
self.PythonIteratorLock.acquire() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
116 |
def PythonIterator(res): |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
117 |
self.PythonIteratorLock.acquire() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
118 |
self.PythonIteratorLock.release() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
119 |
return None |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
120 |
self._PythonIterator = PythonIterator |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
121 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
122 |
def StopPLCLock(): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
123 |
self.PLClibraryLock.acquire() |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
124 |
self._stopPLC_real() |
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
125 |
self.PythonIteratorLock.release() |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
126 |
self.PLClibraryLock.release() |
352 | 127 |
|
128 |
self._stopPLC = StopPLCLock |
|
229 | 129 |
|
130 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
131 |
self._ResetDebugVariables.restype = None |
|
132 |
||
235 | 133 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 134 |
self._RegisterDebugVariable.restype = None |
235 | 135 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int] |
229 | 136 |
|
137 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
138 |
self._FreeDebugData.restype = None |
|
139 |
||
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
140 |
self._GetDebugData = self.PLClibraryHandle.GetDebugData |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
141 |
self._GetDebugData.restype = ctypes.c_int |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
142 |
self._GetDebugData.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p)] |
235 | 143 |
|
144 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
145 |
self._suspendDebug.restype = None |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
146 |
self._suspendDebug.argtypes = [ctypes.c_int] |
235 | 147 |
|
148 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
149 |
self._resumeDebug.restype = None |
|
150 |
||
229 | 151 |
return True |
152 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
153 |
PLCprint(traceback.format_exc()) |
229 | 154 |
return False |
155 |
||
156 |
def _FreePLC(self): |
|
157 |
""" |
|
158 |
Unload PLC library. |
|
159 |
This is also called by __init__ to create dummy C func proxies |
|
160 |
""" |
|
352 | 161 |
self.PLClibraryLock.acquire() |
229 | 162 |
# Forget all refs to library |
163 |
self._startPLC = lambda:None |
|
164 |
self._stopPLC = lambda:None |
|
165 |
self._ResetDebugVariables = lambda:None |
|
166 |
self._RegisterDebugVariable = lambda x:None |
|
167 |
self._IterDebugData = lambda x,y:None |
|
168 |
self._FreeDebugData = lambda:None |
|
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
169 |
self._GetDebugData = lambda:-1 |
235 | 170 |
self._suspendDebug = lambda:None |
171 |
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
|
172 |
self._PythonIterator = lambda:"" |
229 | 173 |
self.PLClibraryHandle = None |
174 |
# Unload library explicitely |
|
175 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
176 |
dlclose(self._PLClibraryHandle) |
|
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
177 |
self._PLClibraryHandle = None |
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
178 |
|
352 | 179 |
self.PLClibraryLock.release() |
229 | 180 |
return False |
181 |
||
299 | 182 |
def PrepareRuntimePy(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
286
diff
changeset
|
183 |
self.python_threads_vars = globals().copy() |
344
25b7b7f854bc
Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents:
339
diff
changeset
|
184 |
self.python_threads_vars["WorkingDir"] = self.workingdir |
368 | 185 |
self.python_threads_vars["website"] = self.website |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
186 |
self.python_threads_vars["_runtime_begin"] = [] |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
187 |
self.python_threads_vars["_runtime_cleanup"] = [] |
368 | 188 |
|
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
189 |
for filename in os.listdir(self.workingdir): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
190 |
name, ext = os.path.splitext(filename) |
391
9b1801ef99b5
fix runtime.py filename case to avoid problem on multi-platform
greg
parents:
368
diff
changeset
|
191 |
if name.upper().startswith("RUNTIME") and ext.upper() == ".PY": |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
192 |
try: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
193 |
# TODO handle exceptions in runtime.py |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
194 |
# pyfile may redefine _runtime_cleanup |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
195 |
# or even call _PythonThreadProc itself. |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
196 |
execfile(os.path.join(self.workingdir, filename), self.python_threads_vars) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
197 |
except: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
198 |
PLCprint(traceback.format_exc()) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
199 |
runtime_begin = self.python_threads_vars.get("_%s_begin" % name, None) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
200 |
if runtime_begin is not None: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
201 |
self.python_threads_vars["_runtime_begin"].append(runtime_begin) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
202 |
runtime_cleanup = self.python_threads_vars.get("_%s_cleanup" % name, None) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
203 |
if runtime_cleanup is not None: |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
204 |
self.python_threads_vars["_runtime_cleanup"].append(runtime_cleanup) |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
205 |
|
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
206 |
for runtime_begin in self.python_threads_vars.get("_runtime_begin", []): |
329 | 207 |
runtime_begin() |
368 | 208 |
|
209 |
if self.website is not None: |
|
210 |
self.website.PLCStarted() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
211 |
|
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
212 |
def FinishRuntimePy(self): |
366
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
213 |
for runtime_cleanup in self.python_threads_vars.get("_runtime_cleanup", []): |
cd90e4c10261
Move python evaluator to create a python plugin containing any related python module
laurent
parents:
365
diff
changeset
|
214 |
runtime_cleanup() |
368 | 215 |
if self.website is not None: |
216 |
self.website.PLCStopped() |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
217 |
self.python_threads_vars = None |
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
218 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
219 |
def PythonThreadProc(self): |
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
220 |
PLCprint("PythonThreadProc started") |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
221 |
c_argv = ctypes.c_char_p * len(self.argv) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
222 |
error = None |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
223 |
if self._LoadNewPLC(): |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
224 |
if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
229 | 225 |
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
|
226 |
self.StatusChange() |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
227 |
self.evaluator(self.PrepareRuntimePy) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
228 |
res,cmd = "None","None" |
352 | 229 |
while True: |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
230 |
#print "_PythonIterator(", res, ")", |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
231 |
cmd = self._PythonIterator(res) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
232 |
#print " -> ", cmd |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
233 |
if cmd is None: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
234 |
break |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
235 |
try : |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
236 |
res = str(self.evaluator(eval,cmd,self.python_threads_vars)) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
237 |
except Exception,e: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
238 |
res = "#EXCEPTION : "+str(e) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
239 |
PLCprint(res) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
240 |
self.PLCStatus = "Stopped" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
241 |
self.StatusChange() |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
242 |
self.evaluator(self.FinishRuntimePy) |
229 | 243 |
else: |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
244 |
error = "starting" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
245 |
else: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
246 |
error = "loading" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
247 |
if error is not None: |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
248 |
PLCprint("Problem %s PLC"%error) |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
249 |
self.PLCStatus = "Broken" |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
250 |
self._FreePLC() |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
251 |
PLCprint("PythonThreadProc interrupted") |
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
252 |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
253 |
def StartPLC(self): |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
254 |
PLCprint("StartPLC") |
352 | 255 |
if self.CurrentPLCFilename is not None: |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
256 |
self.PLCStatus = "Started" |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
257 |
self.PythonThread = Thread(target=self.PythonThreadProc) |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
258 |
self.PythonThread.start() |
352 | 259 |
|
229 | 260 |
def StopPLC(self): |
350
a3a5561bde1d
- now call load, start, free PLC from the python Thread
greg
parents:
344
diff
changeset
|
261 |
PLCprint("StopPLC") |
229 | 262 |
if self.PLCStatus == "Started": |
352 | 263 |
self._stopPLC() |
229 | 264 |
return True |
265 |
return False |
|
266 |
||
267 |
def _Reload(self): |
|
268 |
self.daemon.shutdown(True) |
|
269 |
self.daemon.sock.close() |
|
270 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
271 |
# never reached |
|
272 |
return 0 |
|
273 |
||
274 |
def ForceReload(self): |
|
275 |
# respawn python interpreter |
|
276 |
Timer(0.1,self._Reload).start() |
|
277 |
return True |
|
278 |
||
279 |
def GetPLCstatus(self): |
|
280 |
return self.PLCStatus |
|
281 |
||
282 |
def NewPLC(self, md5sum, data, extrafiles): |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
283 |
PLCprint("NewPLC (%s)"%md5sum) |
393
af20e07e53c5
Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents:
368
diff
changeset
|
284 |
if self.PLCStatus in ["Stopped", "Empty", "Broken"]: |
229 | 285 |
NewFileName = md5sum + lib_ext |
286 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
287 |
try: |
|
288 |
os.remove(os.path.join(self.workingdir, |
|
289 |
self.CurrentPLCFilename)) |
|
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
290 |
for filename in file(extra_files_log, "r").readlines() + [extra_files_log]: |
229 | 291 |
try: |
364
27ea6a6747fc
Bug extra_files deletion in working directory fixed
laurent
parents:
361
diff
changeset
|
292 |
os.remove(os.path.join(self.workingdir, filename.strip())) |
229 | 293 |
except: |
294 |
pass |
|
295 |
except: |
|
296 |
pass |
|
297 |
||
298 |
try: |
|
299 |
# Create new PLC file |
|
300 |
open(os.path.join(self.workingdir,NewFileName), |
|
301 |
'wb').write(data) |
|
302 |
||
303 |
# Store new PLC filename based on md5 key |
|
304 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
305 |
||
306 |
# Then write the files |
|
307 |
log = file(extra_files_log, "w") |
|
308 |
for fname,fdata in extrafiles: |
|
309 |
fpath = os.path.join(self.workingdir,fname) |
|
310 |
open(fpath, "wb").write(fdata) |
|
311 |
log.write(fname+'\n') |
|
312 |
||
313 |
# Store new PLC filename |
|
314 |
self.CurrentPLCFilename = NewFileName |
|
315 |
except: |
|
291
701c0601db02
Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents:
290
diff
changeset
|
316 |
PLCprint(traceback.format_exc()) |
229 | 317 |
return False |
318 |
if self.PLCStatus == "Empty": |
|
319 |
self.PLCStatus = "Stopped" |
|
320 |
return True |
|
321 |
return False |
|
322 |
||
323 |
def MatchMD5(self, MD5): |
|
324 |
try: |
|
325 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
326 |
return last_md5 == MD5 |
|
327 |
except: |
|
328 |
return False |
|
329 |
||
330 |
def SetTraceVariablesList(self, idxs): |
|
331 |
""" |
|
332 |
Call ctype imported function to append |
|
333 |
these indexes to registred variables in PLC debugger |
|
334 |
""" |
|
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
335 |
if idxs: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
336 |
# suspend but dont disable |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
337 |
self._suspendDebug(False) |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
338 |
# keep a copy of requested idx |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
339 |
self._Idxs = idxs[:] |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
340 |
self._ResetDebugVariables() |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
341 |
for idx,iectype in idxs: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
342 |
self._RegisterDebugVariable(idx) |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
343 |
self._resumeDebug() |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
344 |
else: |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
345 |
self._suspendDebug(True) |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
461
diff
changeset
|
346 |
self._Idxs = [] |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
347 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
348 |
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
|
349 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
350 |
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
|
351 |
""" |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
269
diff
changeset
|
352 |
_fields_ = [("len", ctypes.c_uint8), |
459
af7350d24ab5
Fix debug crash
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
455
diff
changeset
|
353 |
("body", ctypes.c_char * 126)] |
229 | 354 |
|
238 | 355 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
356 |
"STEP" : (ctypes.c_uint8, lambda x:x.value), |
|
357 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
|
358 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value), |
|
359 |
"SINT" : (ctypes.c_int8, lambda x:x.value), |
|
360 |
"USINT" : (ctypes.c_uint8, lambda x:x.value), |
|
361 |
"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
|
362 |
"STRING" : (IEC_STRING, lambda x:x.body[:x.len]), |
238 | 363 |
"INT" : (ctypes.c_int16, lambda x:x.value), |
364 |
"UINT" : (ctypes.c_uint16, lambda x:x.value), |
|
365 |
"WORD" : (ctypes.c_uint16, lambda x:x.value), |
|
366 |
"WSTRING" : (None, None),#TODO |
|
367 |
"DINT" : (ctypes.c_int32, lambda x:x.value), |
|
368 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value), |
|
369 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value), |
|
370 |
"LINT" : (ctypes.c_int64, lambda x:x.value), |
|
371 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value), |
|
372 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value), |
|
373 |
"REAL" : (ctypes.c_float, lambda x:x.value), |
|
374 |
"LREAL" : (ctypes.c_double, lambda x:x.value), |
|
229 | 375 |
} |
376 |
||
377 |
def GetTraceVariables(self): |
|
378 |
""" |
|
339 | 379 |
Return a list of variables, corresponding to the list of required idx |
229 | 380 |
""" |
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
|
381 |
if self.PLCStatus == "Started": |
455
e050ef5bd285
Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents:
450
diff
changeset
|
382 |
res=[] |
353 | 383 |
self.PLClibraryLock.acquire() |
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
384 |
tick = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
385 |
size = ctypes.c_uint32() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
386 |
buffer = ctypes.c_void_p() |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
387 |
if self._GetDebugData(ctypes.byref(tick),ctypes.byref(size),ctypes.byref(buffer)) == 0 : |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
388 |
offset = 0 |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
389 |
for idx, iectype in self._Idxs: |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
390 |
cursor = ctypes.c_void_p(buffer.value + offset) |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
391 |
c_type,unpack_func = self.TypeTranslator.get(iectype, (None,None)) |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
392 |
if c_type is not None and offset < size: |
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
393 |
res.append(unpack_func(ctypes.cast(cursor, |
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
|
394 |
ctypes.POINTER(c_type)).contents)) |
459
af7350d24ab5
Fix debug crash
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
455
diff
changeset
|
395 |
offset += ctypes.sizeof(c_type) |
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
|
396 |
else: |
460
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
397 |
if c_type is None: |
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
398 |
PLCprint("Debug error - " + iectype + " not supported !") |
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
399 |
if offset >= size: |
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
400 |
PLCprint("Debug error - buffer too small !") |
450
18583d13f0fa
Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
401 |
break |
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
|
402 |
self._FreeDebugData() |
353 | 403 |
self.PLClibraryLock.release() |
461
bcbc472c0ba8
Safer debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
460
diff
changeset
|
404 |
if offset == size.value: |
460
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
405 |
return self.PLCStatus, tick.value, res |
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
406 |
else: |
73a53278833b
Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
459
diff
changeset
|
407 |
PLCprint("Debug error - bad buffer unpack !") |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
408 |
return self.PLCStatus, None, None |
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
411
diff
changeset
|
409 |