Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
authorEdouard Tisserant
Tue, 17 Feb 2015 10:23:57 +0100
changeset 1447 d6b878525ceb
parent 1446 4963e3816641
child 1448 20ff66dcc31d
Fixed systematically loading PLC binary at startup even without -a parameter. Extended py_ext extensions instances variable description (PLCGlobalsDesc). Now contains list of variables organizd by extension, with extension name
Beremiz_service.py
py_ext/PythonFileCTNMixin.py
runtime/PLCObject.py
--- a/Beremiz_service.py	Mon Feb 16 16:23:51 2015 +0100
+++ b/Beremiz_service.py	Tue Feb 17 10:23:57 2015 +0100
@@ -401,8 +401,10 @@
             self.servicepublisher = ServicePublisher.ServicePublisher()
             self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port)
 
-        if self.autostart and self.plcobj.GetPLCstatus()[0] != "Empty":
-            self.plcobj.StartPLC()
+        if self.autostart :
+            self.plcobj.AutoLoad()
+            if self.plcobj.GetPLCstatus()[0] != "Empty":
+                self.plcobj.StartPLC()
 
         sys.stdout.flush()
 
--- a/py_ext/PythonFileCTNMixin.py	Mon Feb 16 16:23:51 2015 +0100
+++ b/py_ext/PythonFileCTNMixin.py	Tue Feb 17 10:23:57 2015 +0100
@@ -72,6 +72,7 @@
                                 self.GetCurrentLocation()))
         configname = self.GetCTRoot().GetProjectConfigNames()[0]
 
+        pyextname = self.CTNName()
 
         # python side PLC global variables access stub
         globalstubs = "\n".join(["""\
@@ -83,11 +84,12 @@
 _PySafeSetPLCGlob_%(name)s = PLCBinary.__SafeSetPLCGlob_%(name)s
 _PySafeSetPLCGlob_%(name)s.restype = None
 _PySafeSetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)]
-_PySafePLCGlobals.append(("%(name)s","%(IECtype)s"))
+_%(pyextname)sGlobalsDesc.append(("%(name)s","%(IECtype)s"))
 """ % { "name": variable.getname(),
         "configname": configname.upper(),
         "uppername": variable.getname().upper(),
-        "IECtype": variable.gettype()}
+        "IECtype": variable.gettype(),
+        "pyextname":pyextname}
             for variable in self.CodeFile.variables.variable])
 
         # Runtime calls (start, stop, init, and cleanup)
@@ -104,8 +106,6 @@
 
         globalsection = self.GetSection("globals")
 
-        pyextname = self.CTNName()
-
         PyFileContent = """\
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
@@ -115,8 +115,8 @@
 ## Code for PLC global variable access
 from targets.typemapping import TypeTranslator
 import ctypes
-_PyExtName = "%(pyextname)s"
-_PySafePLCGlobals = []
+_%(pyextname)sGlobalsDesc = []
+PLCGlobalsDesc.append(( "_%(pyextname)s" , _%(pyextname)sGlobalsDesc ))
 %(globalstubs)s
 
 ## User code in "global" scope
--- a/runtime/PLCObject.py	Mon Feb 16 16:23:51 2015 +0100
+++ b/runtime/PLCObject.py	Tue Feb 17 10:23:57 2015 +0100
@@ -56,7 +56,7 @@
         self.evaluator = evaluator
         self.argv = [workingdir] + argv # force argv[0] to be "path" to exec...
         self.workingdir = workingdir
-        self.PLCStatus = "Stopped"
+        self.PLCStatus = "Empty"
         self.PLClibraryHandle = None
         self.PLClibraryLock = Lock()
         self.DummyIteratorLock = None
@@ -73,6 +73,7 @@
         self.TraceWakeup = Event()
         self.Traces = []
 
+    def AutoLoad(self):
         # Get the last transfered PLC if connector must be restart
         try:
             self.CurrentPLCFilename=open(
@@ -264,11 +265,6 @@
         self.python_runtime_vars = globals().copy()
         self.python_runtime_vars.update(self.pyruntimevars)
 
-        self.python_runtime_vars["WorkingDir"] = self.workingdir
-        for methodname in MethodNames :
-            self.python_runtime_vars["_runtime_%s"%methodname] = []
-        self.python_runtime_vars["PLCObject"] = self
-        self.python_runtime_vars["PLCBinary"] = self.PLClibraryHandle
         class PLCSafeGlobals:
             def __getattr__(_self, name):
                 try :
@@ -285,9 +281,21 @@
                     raise KeyError("Try to set unknown shared global variable : %s"%name)
                 v = self.python_runtime_vars["_"+name+"_pack"](t,value)
                 self.python_runtime_vars["_PySafeSetPLCGlob_"+name](ctypes.byref(v))
-        self.python_runtime_vars["PLCGlobals"] = PLCSafeGlobals()
+
+        self.python_runtime_vars.update({
+            "PLCGlobals" : PLCSafeGlobals(),
+            "WorkingDir" : self.workingdir,
+            "PLCObject"  : self,
+            "PLCBinary"  : self.PLClibraryHandle,
+            "PLCGlobalsDesc" : []})
+
+        for methodname in MethodNames :
+            self.python_runtime_vars["_runtime_%s"%methodname] = []
+
         try:
-            for filename in os.listdir(self.workingdir):
+            filenames = os.listdir(self.workingdir)
+            filenames.sort()
+            for filename in filenames:
                 name, ext = os.path.splitext(filename)
                 if name.upper().startswith("RUNTIME") and ext.upper() == ".PY":
                     execfile(os.path.join(self.workingdir, filename), self.python_runtime_vars)