Fixed PLC global var access from python. Added test in tests/python
authorEdouard Tisserant
Wed, 15 May 2013 18:21:20 +0900
changeset 1145 203f4eff3313
parent 1144 21475ee0e688
child 1147 71db4592beb2
Fixed PLC global var access from python. Added test in tests/python
py_ext/PythonFileCTNMixin.py
runtime/PLCObject.py
tests/python/plc.xml
tests/python/python@py_ext/pyfile.xml
--- a/py_ext/PythonFileCTNMixin.py	Wed May 15 17:13:49 2013 +0900
+++ b/py_ext/PythonFileCTNMixin.py	Wed May 15 18:21:20 2013 +0900
@@ -66,8 +66,8 @@
 _%(name)s_ctype, _%(name)s_unpack, _%(name)s_pack = \\
     TypeTranslator["%(IECtype)s"]
 _PySafeGetPLCGlob_%(name)s = PLCBinary.__SafeGetPLCGlob_%(name)s
-_PySafeGetPLCGlob_%(name)s.restype = _%(name)s_ctype 
-_PySafeGetPLCGlob_%(name)s.argtypes = []
+_PySafeGetPLCGlob_%(name)s.restype = None
+_PySafeGetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)] 
 _PySafeSetPLCGlob_%(name)s = PLCBinary.__SafeSetPLCGlob_%(name)s
 _PySafeSetPLCGlob_%(name)s.restype = None
 _PySafeSetPLCGlob_%(name)s.argtypes = [ctypes.POINTER(_%(name)s_ctype)]
@@ -127,12 +127,11 @@
 long __%(name)s_rlock = 0;
 long __%(name)s_wlock = 0;
 int __%(name)s_wbuffer_written = 0;
-%(IECtype)s __SafeGetPLCGlob_%(name)s(){
+void __SafeGetPLCGlob_%(name)s(%(IECtype)s *pvalue){
     %(IECtype)s res;
     while(AtomicCompareExchange(&__%(name)s_rlock, 0, 1));
-    res = __%(name)s_rbuffer;
+    *pvalue = __%(name)s_rbuffer;
     AtomicCompareExchange((long*)&__%(name)s_rlock, 1, 0);
-    return res;
 }
 __SafeSetPLCGlob_%(name)s(%(IECtype)s *value){
     while(AtomicCompareExchange(&__%(name)s_wlock, 0, 1));
@@ -207,6 +206,6 @@
         
         return ([(Gen_PyCfile_path, matiec_flags)],
                 "",
-                False,
+                True,
                 ("runtime_%s.py"%location_str, file(runtimefile_path,"rb")))
 
--- a/runtime/PLCObject.py	Wed May 15 17:13:49 2013 +0900
+++ b/runtime/PLCObject.py	Wed May 15 18:21:20 2013 +0900
@@ -265,12 +265,14 @@
         self.python_runtime_vars["PLCObject"] = self
         self.python_runtime_vars["PLCBinary"] = self.PLClibraryHandle
         class PLCSafeGlobals:
-            def __getattr__(self, name):
-                r = globals()["_PySafeGetPLCGlob_"+name]()
-                return globals()["_"+name+"_unpack"](r)
-            def __setattr__(self, name, value):
-                v = globals()["_"+name+"_pack"](c_type,value)
-                globals()["_PySafeSetPLCGlob_"+name](ctypes.byref(v))
+            def __getattr__(_self, name):
+                v = self.python_runtime_vars["_"+name+"_ctype"]()
+                r = self.python_runtime_vars["_PySafeGetPLCGlob_"+name](ctypes.byref(v))
+                return self.python_runtime_vars["_"+name+"_unpack"](v)
+            def __setattr__(_self, name, value):
+                t = self.python_runtime_vars["_"+name+"_ctype"]
+                v = self.python_runtime_vars["_"+name+"_pack"](t,value)
+                self.python_runtime_vars["_PySafeSetPLCGlob_"+name](ctypes.byref(v))
         self.python_runtime_vars["PLCGlobals"] = PLCSafeGlobals()
         try:
             for filename in os.listdir(self.workingdir):
--- a/tests/python/plc.xml	Wed May 15 17:13:49 2013 +0900
+++ b/tests/python/plc.xml	Wed May 15 18:21:20 2013 +0900
@@ -8,7 +8,7 @@
               productVersion="0.0"
               creationDateTime="2008-12-14T16:21:19"/>
   <contentHeader name="Beremiz Python Support Tests"
-                 modificationDateTime="2013-05-08T22:43:14">
+                 modificationDateTime="2013-05-15T18:19:52">
     <coordinateInfo>
       <pageSize x="1024" y="1024"/>
       <fbd>
@@ -191,6 +191,16 @@
                 <INT/>
               </type>
             </variable>
+            <variable name="Test_Python_Var">
+              <type>
+                <INT/>
+              </type>
+            </variable>
+            <variable name="Second_Python_Var">
+              <type>
+                <INT/>
+              </type>
+            </variable>
           </externalVars>
         </interface>
         <body>
@@ -857,15 +867,15 @@
               </connectionPointOut>
               <expression>'True'</expression>
             </inVariable>
-            <block localId="46" width="130" height="45" typeName="BYTE_TO_STRING">
+            <block localId="46" width="130" height="45" typeName="INT_TO_STRING">
               <position x="900" y="970"/>
               <inputVariables>
                 <variable formalParameter="IN">
                   <connectionPointIn>
                     <relPosition x="0" y="30"/>
-                    <connection refLocalId="47">
+                    <connection refLocalId="58">
                       <position x="900" y="1000"/>
-                      <position x="850" y="1000"/>
+                      <position x="840" y="1000"/>
                     </connection>
                   </connectionPointIn>
                 </variable>
@@ -879,13 +889,6 @@
                 </variable>
               </outputVariables>
             </block>
-            <inVariable localId="47" height="30" width="80">
-              <position x="770" y="985"/>
-              <connectionPointOut>
-                <relPosition x="80" y="15"/>
-              </connectionPointOut>
-              <expression>BYTE#145</expression>
-            </inVariable>
             <inVariable localId="50" height="30" width="105">
               <position x="200" y="1085"/>
               <connectionPointOut>
@@ -1089,6 +1092,31 @@
               </connectionPointIn>
               <expression>TUTU</expression>
             </outVariable>
+            <inVariable localId="58" height="30" width="145">
+              <position x="715" y="985"/>
+              <connectionPointOut>
+                <relPosition x="145" y="15"/>
+              </connectionPointOut>
+              <expression>Second_Python_Var</expression>
+            </inVariable>
+            <outVariable localId="47" height="30" width="125">
+              <position x="400" y="975"/>
+              <connectionPointIn>
+                <relPosition x="0" y="15"/>
+                <connection refLocalId="59">
+                  <position x="400" y="990"/>
+                  <position x="330" y="990"/>
+                </connection>
+              </connectionPointIn>
+              <expression>Test_Python_Var</expression>
+            </outVariable>
+            <inVariable localId="59" height="30" width="30">
+              <position x="300" y="975"/>
+              <connectionPointOut>
+                <relPosition x="30" y="15"/>
+              </connectionPointOut>
+              <expression>23</expression>
+            </inVariable>
           </FBD>
         </body>
       </pou>
--- a/tests/python/python@py_ext/pyfile.xml	Wed May 15 17:13:49 2013 +0900
+++ b/tests/python/python@py_ext/pyfile.xml	Wed May 15 18:21:20 2013 +0900
@@ -1,7 +1,8 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <PyFile>
   <variables>
-    <variable name="Test_Python_Var" type="INT" initial="0"/>
+    <variable name="Test_Python_Var" type="INT" initial="4"/>
+    <variable name="Second_Python_Var" type="INT" initial="5"/>
   </variables>
   <globals>
 <![CDATA[
@@ -18,6 +19,8 @@
     else:
         print "Failed Python_to_C_Call failed"
         res = None
+    print "Python read PLC global :",PLCGlobals.Test_Python_Var
+    PLCGlobals.Second_Python_Var = 789
     sys.stdout.flush()
     return res