21 #You should have received a copy of the GNU General Public |
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 |
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 |
23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
24 |
24 |
25 import Pyro.core as pyro |
25 import Pyro.core as pyro |
26 from threading import Timer |
26 from threading import Timer, Thread |
27 import ctypes, os, commands |
27 import ctypes, os, commands |
|
28 import time |
28 |
29 |
29 if os.name in ("nt", "ce"): |
30 if os.name in ("nt", "ce"): |
30 from _ctypes import LoadLibrary as dlopen |
31 from _ctypes import LoadLibrary as dlopen |
31 from _ctypes import FreeLibrary as dlclose |
32 from _ctypes import FreeLibrary as dlclose |
32 elif os.name == "posix": |
33 elif os.name == "posix": |
105 self._suspendDebug = self.PLClibraryHandle.suspendDebug |
106 self._suspendDebug = self.PLClibraryHandle.suspendDebug |
106 self._suspendDebug.restype = None |
107 self._suspendDebug.restype = None |
107 |
108 |
108 self._resumeDebug = self.PLClibraryHandle.resumeDebug |
109 self._resumeDebug = self.PLClibraryHandle.resumeDebug |
109 self._resumeDebug.restype = None |
110 self._resumeDebug.restype = None |
|
111 |
|
112 self._PythonIterator = self.PLClibraryHandle.PythonIterator |
|
113 self._PythonIterator.restype = ctypes.c_char_p |
|
114 self._PythonIterator.argtypes = [ctypes.c_char_p] |
110 |
115 |
111 return True |
116 return True |
112 except: |
117 except: |
113 print traceback.format_exc() |
118 print traceback.format_exc() |
114 return False |
119 return False |
126 self._IterDebugData = lambda x,y:None |
131 self._IterDebugData = lambda x,y:None |
127 self._FreeDebugData = lambda:None |
132 self._FreeDebugData = lambda:None |
128 self._WaitDebugData = lambda:-1 |
133 self._WaitDebugData = lambda:-1 |
129 self._suspendDebug = lambda:None |
134 self._suspendDebug = lambda:None |
130 self._resumeDebug = lambda:None |
135 self._resumeDebug = lambda:None |
|
136 self._PythonIterator = lambda:"" |
131 self.PLClibraryHandle = None |
137 self.PLClibraryHandle = None |
132 # Unload library explicitely |
138 # Unload library explicitely |
133 if getattr(self,"_PLClibraryHandle",None) is not None: |
139 if getattr(self,"_PLClibraryHandle",None) is not None: |
134 print "Unload PLC" |
140 print "Unload PLC" |
135 dlclose(self._PLClibraryHandle) |
141 dlclose(self._PLClibraryHandle) |
161 print "Dirty lib detected :" + badlib |
167 print "Dirty lib detected :" + badlib |
162 #dlclose(badhandle) |
168 #dlclose(badhandle) |
163 return True |
169 return True |
164 return False |
170 return False |
165 |
171 |
|
172 def PythonThreadProc(self): |
|
173 res = "" |
|
174 print "PythonThreadProc started" |
|
175 while self.PLCStatus == "Started": |
|
176 cmd = self._PythonIterator(res) |
|
177 print "_PythonIterator(", res, ") -> ", cmd |
|
178 try : |
|
179 res = eval(cmd) |
|
180 except Exception,e: |
|
181 res = "#EXCEPTION : "+str(e) |
|
182 print res |
|
183 print "PythonThreadProc finished" |
166 |
184 |
167 def StartPLC(self, debug=False): |
185 def StartPLC(self, debug=False): |
168 print "StartPLC" |
186 print "StartPLC" |
169 if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
187 if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
170 c_argv = ctypes.c_char_p * len(self.argv) |
188 c_argv = ctypes.c_char_p * len(self.argv) |
268 self._Idxs = idxs[:] |
288 self._Idxs = idxs[:] |
269 self._ResetDebugVariables() |
289 self._ResetDebugVariables() |
270 for idx in idxs: |
290 for idx in idxs: |
271 self._RegisterDebugVariable(idx) |
291 self._RegisterDebugVariable(idx) |
272 self._resumeDebug() |
292 self._resumeDebug() |
|
293 |
|
294 class IEC_STRING(ctypes.Structure): |
|
295 """ |
|
296 Must be changed according to changes in iec_types.h |
|
297 """ |
|
298 _fields_ = [("len", ctypes.c_uint8), |
|
299 ("body", ctypes.c_char * 40)] |
273 |
300 |
274 TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
301 TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
275 "STEP" : (ctypes.c_uint8, lambda x:x.value), |
302 "STEP" : (ctypes.c_uint8, lambda x:x.value), |
276 "TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
303 "TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
277 "ACTION" : (ctypes.c_uint8, lambda x:x.value), |
304 "ACTION" : (ctypes.c_uint8, lambda x:x.value), |
278 "SINT" : (ctypes.c_int8, lambda x:x.value), |
305 "SINT" : (ctypes.c_int8, lambda x:x.value), |
279 "USINT" : (ctypes.c_uint8, lambda x:x.value), |
306 "USINT" : (ctypes.c_uint8, lambda x:x.value), |
280 "BYTE" : (ctypes.c_uint8, lambda x:x.value), |
307 "BYTE" : (ctypes.c_uint8, lambda x:x.value), |
281 "STRING" : (None, None),#TODO |
308 "STRING" : (IEC_STRING, lambda x:x.body[:x.len]), |
282 "INT" : (ctypes.c_int16, lambda x:x.value), |
309 "INT" : (ctypes.c_int16, lambda x:x.value), |
283 "UINT" : (ctypes.c_uint16, lambda x:x.value), |
310 "UINT" : (ctypes.c_uint16, lambda x:x.value), |
284 "WORD" : (ctypes.c_uint16, lambda x:x.value), |
311 "WORD" : (ctypes.c_uint16, lambda x:x.value), |
285 "WSTRING" : (None, None),#TODO |
312 "WSTRING" : (None, None),#TODO |
286 "DINT" : (ctypes.c_int32, lambda x:x.value), |
313 "DINT" : (ctypes.c_int32, lambda x:x.value), |