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, Thread, Lock |
26 from threading import Timer, Thread, Lock, Semaphore |
27 import ctypes, os, commands, types, sys |
27 import ctypes, os, commands, types, sys |
28 from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator |
28 from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator |
29 |
29 |
30 if os.name in ("nt", "ce"): |
30 if os.name in ("nt", "ce"): |
31 from _ctypes import LoadLibrary as dlopen |
31 from _ctypes import LoadLibrary as dlopen |
215 error = None |
215 error = None |
216 if self._LoadNewPLC(): |
216 if self._LoadNewPLC(): |
217 if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
217 if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
218 self.PLCStatus = "Started" |
218 self.PLCStatus = "Started" |
219 self.StatusChange() |
219 self.StatusChange() |
|
220 self.StartSem.release() |
220 self.evaluator(self.PrepareRuntimePy) |
221 self.evaluator(self.PrepareRuntimePy) |
221 res,cmd = "None","None" |
222 res,cmd = "None","None" |
222 while True: |
223 while True: |
223 #print "_PythonIterator(", res, ")", |
224 #print "_PythonIterator(", res, ")", |
224 cmd = self._PythonIterator(res) |
225 cmd = self._PythonIterator(res) |
238 else: |
239 else: |
239 error = "loading" |
240 error = "loading" |
240 if error is not None: |
241 if error is not None: |
241 PLCprint("Problem %s PLC"%error) |
242 PLCprint("Problem %s PLC"%error) |
242 self.PLCStatus = "Broken" |
243 self.PLCStatus = "Broken" |
|
244 self.StatusChange() |
|
245 self.StartSem.release() |
243 self._FreePLC() |
246 self._FreePLC() |
244 |
247 |
245 def StartPLC(self): |
248 def StartPLC(self): |
246 PLCprint("StartPLC") |
249 PLCprint("StartPLC") |
247 if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
250 if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
248 self.PLCStatus = "Started" |
251 self.StartSem=Semaphore(0) |
249 self.PythonThread = Thread(target=self.PythonThreadProc) |
252 self.PythonThread = Thread(target=self.PythonThreadProc) |
250 self.PythonThread.start() |
253 self.PythonThread.start() |
|
254 self.StartSem.acquire() |
251 |
255 |
252 def StopPLC(self): |
256 def StopPLC(self): |
253 PLCprint("StopPLC") |
257 PLCprint("StopPLC") |
254 if self.PLCStatus == "Started": |
258 if self.PLCStatus == "Started": |
255 self.PLCStatus = "Stopped" |
259 self.PLCStatus = "Stopped" |