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