runtime/PLCObject.py
changeset 350 a3a5561bde1d
parent 344 25b7b7f854bc
child 352 81777d4e379c
equal deleted inserted replaced
349:24d4e48714ed 350:a3a5561bde1d
   235             runtime_cleanup()
   235             runtime_cleanup()
   236         if self.hmi_frame is not None:
   236         if self.hmi_frame is not None:
   237             self.hmi_frame.Destroy()
   237             self.hmi_frame.Destroy()
   238         self.python_threads_vars = None
   238         self.python_threads_vars = None
   239 
   239 
   240     def PythonThreadProc(self):
   240     def PythonThreadProc(self, debug):
   241         PLCprint("PythonThreadProc started")
   241         PLCprint("PythonThreadProc started")
   242         self.evaluator(self.PrepareRuntimePy)
   242         c_argv = ctypes.c_char_p * len(self.argv)
   243         res,cmd = "None","None"
   243         error = None
   244         while self.PLCStatus == "Started":
   244         if self._LoadNewPLC():
   245             #print "_PythonIterator(", res, ")",
   245             if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
   246             cmd = self._PythonIterator(res)
       
   247             #print " -> ", cmd
       
   248             if cmd is None:
       
   249                 break
       
   250             try :
       
   251                 res = str(self.evaluator(eval,cmd,self.python_threads_vars))
       
   252             except Exception,e:
       
   253                 res = "#EXCEPTION : "+str(e)
       
   254                 PLCprint(res)
       
   255         self.evaluator(self.FinishRuntimePy)
       
   256         PLCprint("PythonThreadProc interrupted")
       
   257     
       
   258     def StartPLC(self, debug=False):
       
   259         PLCprint("StartPLC")
       
   260         if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
       
   261             c_argv = ctypes.c_char_p * len(self.argv)
       
   262             if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
       
   263                 if debug:
   246                 if debug:
   264                     for idx in self._Idxs:
   247                     for idx in self._Idxs:
   265                         self._RegisterDebugVariable(idx)
   248                         self._RegisterDebugVariable(idx)
   266                     self._resumeDebug()
   249                     self._resumeDebug()
   267                 self.PLCStatus = "Started"
   250                 self.PLCStatus = "Started"
   268                 self.StatusChange()
   251                 self.StatusChange()
   269                 self.PythonThread = Thread(target=self.PythonThreadProc)
   252                 self.evaluator(self.PrepareRuntimePy)
   270                 self.PythonThread.start()
   253                 res,cmd = "None","None"
   271                 return True
   254                 while self.PLCStatus == "Started":
       
   255                     #print "_PythonIterator(", res, ")",
       
   256                     cmd = self._PythonIterator(res)
       
   257                     #print " -> ", cmd
       
   258                     if cmd is None:
       
   259                         break
       
   260                     try :
       
   261                         res = str(self.evaluator(eval,cmd,self.python_threads_vars))
       
   262                     except Exception,e:
       
   263                         res = "#EXCEPTION : "+str(e)
       
   264                         PLCprint(res)
       
   265                 self.PLCStatus = "Stopped"
       
   266                 self.StatusChange()
       
   267                 self.evaluator(self.FinishRuntimePy)
   272             else:
   268             else:
   273                 PLCprint("Problem starting PLC")
   269                 error = "starting"
   274                 self._DoStopPLC()
   270         else:
   275         return False
   271             error = "loading"
       
   272         if error is not None:
       
   273             PLCprint("Problem %s PLC"%error)
       
   274             self.PLCStatus = "Broken"
       
   275         self._DoStopPLC()
       
   276         self._FreePLC()
       
   277         PLCprint("PythonThreadProc interrupted")
       
   278     
       
   279     def StartPLC(self, debug=False):
       
   280         PLCprint("StartPLC")
       
   281         if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
       
   282             self.PythonThread = Thread(target=self.PythonThreadProc, args=[debug])
       
   283             self.PythonThread.start()
   276 
   284 
   277     def _DoStopPLC(self):
   285     def _DoStopPLC(self):
   278         self.PLCStatus = "Stopped"
       
   279         self.StatusChange()
   286         self.StatusChange()
   280         self._stopPLC()
   287         self._stopPLC()
   281         if self.PythonThread.isAlive():
       
   282             self.PythonThread.join()
       
   283         if self._FreePLC():
       
   284             self.PLCStatus = "Dirty"
       
   285         self.StatusChange()
   288         self.StatusChange()
   286         return True
   289         return True
   287 
   290 
   288     def StopPLC(self):
   291     def StopPLC(self):
       
   292         PLCprint("StopPLC")
   289         if self.PLCStatus == "Started":
   293         if self.PLCStatus == "Started":
   290             self._DoStopPLC()
   294             self._DoStopPLC()
       
   295             self.PLCStatus = "Stopped"
   291             return True
   296             return True
   292         return False
   297         return False
   293 
   298 
   294     def _Reload(self):
   299     def _Reload(self):
   295         self.daemon.shutdown(True)
   300         self.daemon.shutdown(True)
   306     def GetPLCstatus(self):
   311     def GetPLCstatus(self):
   307         return self.PLCStatus
   312         return self.PLCStatus
   308     
   313     
   309     def NewPLC(self, md5sum, data, extrafiles):
   314     def NewPLC(self, md5sum, data, extrafiles):
   310         PLCprint("NewPLC (%s)"%md5sum)
   315         PLCprint("NewPLC (%s)"%md5sum)
   311         if self.PLCStatus in ["Stopped", "Empty", "Dirty"]:
   316         if self.PLCStatus in ["Stopped", "Empty", "Dirty", "Broken"]:
   312             NewFileName = md5sum + lib_ext
   317             NewFileName = md5sum + lib_ext
   313             extra_files_log = os.path.join(self.workingdir,"extra_files.txt")
   318             extra_files_log = os.path.join(self.workingdir,"extra_files.txt")
   314             try:
   319             try:
   315                 os.remove(os.path.join(self.workingdir,
   320                 os.remove(os.path.join(self.workingdir,
   316                                        self.CurrentPLCFilename))
   321                                        self.CurrentPLCFilename))