targets/typemapping.py
changeset 1433 4a45f6642523
parent 1075 8078c01ae464
child 1571 486f94a8032c
--- a/targets/typemapping.py	Thu Jan 29 19:11:34 2015 +0100
+++ b/targets/typemapping.py	Fri Jan 30 10:45:11 2015 +0100
@@ -19,6 +19,11 @@
 #License along with this library; if not, write to the Free Software
 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+import ctypes
+ctypes.pythonapi.PyString_AsString.argtypes = (ctypes.c_void_p,)
+ctypes.pythonapi.PyString_AsString.restype = ctypes.POINTER(ctypes.c_char)
+
+
 from ctypes import *
 from datetime import timedelta as td
 
@@ -27,7 +32,7 @@
     Must be changed according to changes in iec_types.h
     """
     _fields_ = [("len", c_uint8),
-                ("body", c_char * 126)] 
+                ("body", c_char * 126)]
 
 class IEC_TIME(Structure):
     """
@@ -37,8 +42,8 @@
                 ("ns", c_long)] #tv_nsec
 
 def _t(t, u=lambda x:x.value, p=lambda t,x:t(x)): return  (t, u, p)
-def _ttime(): return (IEC_TIME, 
-                      lambda x:td(0, x.s, x.ns/1000), 
+def _ttime(): return (IEC_TIME,
+                      lambda x:td(0, x.s, x.ns/1000),
                       lambda t,x:t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000))
 
 SameEndianessTypeTranslator = {
@@ -49,8 +54,8 @@
     "SINT" :       _t(c_int8),
     "USINT" :      _t(c_uint8),
     "BYTE" :       _t(c_uint8),
-    "STRING" :     (IEC_STRING, 
-                      lambda x:x.body[:x.len], 
+    "STRING" :     (IEC_STRING,
+                      lambda x:x.body[:x.len],
                       lambda t,x:t(len(x),x)),
     "INT" :        _t(c_int16),
     "UINT" :       _t(c_uint16),
@@ -67,38 +72,35 @@
     "TOD" :        _ttime(),
     "DATE" :       _ttime(),
     "DT" :         _ttime(),
-    } 
+    }
 
 SwapedEndianessTypeTranslator = {
     #TODO
-    } 
+    }
 
 TypeTranslator=SameEndianessTypeTranslator
 
 # Construct debugger natively supported types
 DebugTypesSize =  dict([(key,sizeof(t)) for key,(t,p,u) in SameEndianessTypeTranslator.iteritems() if t is not None])
 
-def UnpackDebugBuffer(buff, size, indexes):
-    res = []
-    offset = 0
-    for idx, iectype, forced in indexes:
-        cursor = c_void_p(buff.value + offset)
+def UnpackDebugBuffer(buff, indexes):
+    res =  []
+    buffoffset = 0
+    buffsize = len(buff)
+    buffptr = cast(ctypes.pythonapi.PyString_AsString(id(buff)),c_void_p).value
+    for iectype in indexes:
         c_type,unpack_func, pack_func = \
             TypeTranslator.get(iectype,
                                     (None,None,None))
-        if c_type is not None and offset < size:
-            res.append(unpack_func(
-                        cast(cursor,
-                         POINTER(c_type)).contents))
-            offset += sizeof(c_type) if iectype != "STRING" else len(res[-1])+1
+        if c_type is not None and buffoffset < buffsize:
+            cursor = c_void_p( buffptr + buffoffset)
+            value = unpack_func( cast(cursor,
+                         POINTER(c_type)).contents)
+            buffoffset += sizeof(c_type) if iectype != "STRING" else len(value)+1
+            res.append(value)
         else:
-            #if c_type is None:
-            #    PLCprint("Debug error - " + iectype +
-            #             " not supported !")
-            #if offset >= size:
-            #    PLCprint("Debug error - buffer too small ! %d != %d"%(offset, size))
             break
-    if offset and offset == size:
+    if buffoffset and buffoffset == buffsize:
         return res
     return None