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