49 self.PLClibraryHandle = None |
49 self.PLClibraryHandle = None |
50 # Creates fake C funcs proxies |
50 # Creates fake C funcs proxies |
51 self._FreePLC() |
51 self._FreePLC() |
52 self.daemon = daemon |
52 self.daemon = daemon |
53 self.statuschange = statuschange |
53 self.statuschange = statuschange |
|
54 self.python_threads_vars = None |
54 |
55 |
55 # Get the last transfered PLC if connector must be restart |
56 # Get the last transfered PLC if connector must be restart |
56 try: |
57 try: |
57 self.CurrentPLCFilename=open( |
58 self.CurrentPLCFilename=open( |
58 self._GetMD5FileName(), |
59 self._GetMD5FileName(), |
172 return True |
173 return True |
173 return False |
174 return False |
174 |
175 |
175 def PythonThreadProc(self): |
176 def PythonThreadProc(self): |
176 print "PythonThreadProc started" |
177 print "PythonThreadProc started" |
177 my_globs = globals().copy() |
178 self.python_threads_vars = globals().copy() |
178 pyfile = os.path.join(self.workingdir, "runtime.py") |
179 pyfile = os.path.join(self.workingdir, "runtime.py") |
179 if os.path.exists(pyfile): |
180 if os.path.exists(pyfile): |
180 # TODO handle exceptions in runtime.py |
181 # TODO handle exceptions in runtime.py |
181 # pyfile may redefine _runtime_cleanup |
182 # pyfile may redefine _runtime_cleanup |
182 # or even call _PythonThreadProc itself. |
183 # or even call _PythonThreadProc itself. |
183 execfile(pyfile, my_globs) |
184 execfile(pyfile, self.python_threads_vars) |
184 res,cmd = "None","None" |
185 res,cmd = "None","None" |
185 while self.PLCStatus == "Started": |
186 while self.PLCStatus == "Started": |
186 print "_PythonIterator(", res, ")", |
187 #print "_PythonIterator(", res, ")", |
187 cmd = self._PythonIterator(res) |
188 cmd = self._PythonIterator(res) |
188 print " -> ", cmd |
189 #print " -> ", cmd |
189 if cmd is None: |
190 if cmd is None: |
190 break |
191 break |
191 try : |
192 try : |
192 res = str(eval(cmd,my_globs)) |
193 res = str(eval(cmd,self.python_threads_vars)) |
193 except Exception,e: |
194 except Exception,e: |
194 res = "#EXCEPTION : "+str(e) |
195 res = "#EXCEPTION : "+str(e) |
195 print res |
196 print res |
196 print "PythonThreadProc interrupted" |
197 print "PythonThreadProc interrupted" |
197 if my_globs.get("_runtime_cleanup",None) is not None: |
198 if self.python_threads_vars.get("_runtime_cleanup",None) is not None: |
198 my_globs["_runtime_cleanup"]() |
199 self.python_threads_vars["_runtime_cleanup"]() |
|
200 self.python_threads_vars = None |
199 print "PythonThreadProc cleaned up" |
201 print "PythonThreadProc cleaned up" |
200 |
202 |
201 def StartPLC(self, debug=False): |
203 def StartPLC(self, debug=False): |
202 print "StartPLC" |
204 print "StartPLC" |
203 if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
205 if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |