Made better ctypes unpacking of PLC data
authorEdouard Tisserant
Wed, 20 Mar 2013 11:28:38 +0900
changeset 31 a9c9d1fc97d3
parent 30 9cd6bc93ed89
child 33 961cf0f80c25
Made better ctypes unpacking of PLC data
LPCconnector/LPCAppObject.py
LPCtarget/plc_LPC_main.c
--- a/LPCconnector/LPCAppObject.py	Tue Mar 19 23:19:43 2013 +0100
+++ b/LPCconnector/LPCAppObject.py	Wed Mar 20 11:28:38 2013 +0900
@@ -25,7 +25,7 @@
 import ctypes
 from LPCAppProto import *
 from LPCObject import *
-from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator
+from targets.typemapping import  LogLevelsCount, SameEndianessTypeTranslator as TypeTranslator
 
 class LPCAppObject(LPCObject):
     def connect(self,comport):
@@ -46,10 +46,13 @@
 
     def GetPLCstatus(self):
         strcounts = self.HandleSerialTransaction(GET_LOGCOUNTSTransaction())
-        ccounts = ctypes.cast(
-                      ctypes.c_char_p(strcounts),
-                      ctypes.POINTER(ctypes.c_uint32))
-        return self.PLCStatus, tuple(int(ccounts[idx]) for idx in range(4))
+        if len(strcounts) == LogLevelsCount * 4:
+            cstrcounts = ctypes.create_string_buffer(strcounts)
+            ccounts = ctypes.cast(cstrcounts, ctypes.POINTER(ctypes.c_uint32))
+            counts = [int(ccounts[idx]) for idx in xrange(LogLevelsCount)]
+        else :
+            counts = [0]*LogLevelsCount
+        return self.PLCStatus, counts 
 
     def MatchMD5(self, MD5):
         data = self.HandleSerialTransaction(GET_PLCIDTransaction())
@@ -97,12 +100,10 @@
         if strbuf is not None and len(strbuf) > 4 and self.PLCStatus == "Started":
             res=[]
             size = len(strbuf) - 4
-            tick = ctypes.cast(
-                    ctypes.c_char_p(strbuf[:4]),
-                    ctypes.POINTER(ctypes.c_int)).contents
-            buff = ctypes.cast(
-                      ctypes.c_char_p(strbuf[4:]),
-                      ctypes.c_void_p)
+            ctick = ctypes.create_string_buffer(strbuf[:4])
+            tick = ctypes.cast(ctick, ctypes.POINTER(ctypes.c_int)).contents
+            cbuff = ctypes.create_string_buffer(strbuf[4:])
+            buff = ctypes.cast(cbuff, ctypes.c_void_p)
             for idx, iectype, forced in self._Idxs:
                 cursor = ctypes.c_void_p(buff.value + offset)
                 c_type,unpack_func, pack_func = TypeTranslator.get(iectype, (None,None,None))
--- a/LPCtarget/plc_LPC_main.c	Tue Mar 19 23:19:43 2013 +0100
+++ b/LPCtarget/plc_LPC_main.c	Wed Mar 20 11:28:38 2013 +0900
@@ -32,7 +32,7 @@
 	return res;
 }
 
-long AtomicCompareExchange64(long long* atomicvar,long long compared, long long exchange)
+long long AtomicCompareExchange64(long long* atomicvar,long long compared, long long exchange)
 {
 	/* No need for real atomic op on LPC,
 	 * no possible preemption between debug and PLC */