runtime/PLCObject.py
changeset 477 f66a092b6e74
parent 467 11638e373a65
child 479 c28f40b27798
equal deleted inserted replaced
476:92f449479084 477:f66a092b6e74
   130             self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables
   130             self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables
   131             self._ResetDebugVariables.restype = None
   131             self._ResetDebugVariables.restype = None
   132     
   132     
   133             self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable
   133             self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable
   134             self._RegisterDebugVariable.restype = None
   134             self._RegisterDebugVariable.restype = None
   135             self._RegisterDebugVariable.argtypes = [ctypes.c_int]
   135             self._RegisterDebugVariable.argtypes = [ctypes.c_int, ctypes.c_void_p]
   136     
   136     
   137             self._FreeDebugData = self.PLClibraryHandle.FreeDebugData
   137             self._FreeDebugData = self.PLClibraryHandle.FreeDebugData
   138             self._FreeDebugData.restype = None
   138             self._FreeDebugData.restype = None
   139             
   139             
   140             self._GetDebugData = self.PLClibraryHandle.GetDebugData
   140             self._GetDebugData = self.PLClibraryHandle.GetDebugData
   325             last_md5 = open(self._GetMD5FileName(), "r").read()
   325             last_md5 = open(self._GetMD5FileName(), "r").read()
   326             return last_md5 == MD5
   326             return last_md5 == MD5
   327         except:
   327         except:
   328             return False
   328             return False
   329     
   329     
       
   330 
       
   331     class IEC_STRING(ctypes.Structure):
       
   332         """
       
   333         Must be changed according to changes in iec_types.h
       
   334         """
       
   335         _fields_ = [("len", ctypes.c_uint8),
       
   336                     ("body", ctypes.c_char * 126)] 
       
   337     
       
   338     TypeTranslator = {"BOOL" :       (ctypes.c_uint8,  lambda x:x.value!=0,     lambda t,x:t(x)),
       
   339                       "STEP" :       (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   340                       "TRANSITION" : (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   341                       "ACTION" :     (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   342                       "SINT" :       (ctypes.c_int8,   lambda x:x.value,        lambda t,x:t(x)),
       
   343                       "USINT" :      (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   344                       "BYTE" :       (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   345                       "STRING" :     (IEC_STRING,      lambda x:x.body[:x.len], lambda t,x:t(len(x),x)),
       
   346                       "INT" :        (ctypes.c_int16,  lambda x:x.value,        lambda t,x:t(x)),
       
   347                       "UINT" :       (ctypes.c_uint16, lambda x:x.value,        lambda t,x:t(x)),
       
   348                       "WORD" :       (ctypes.c_uint16, lambda x:x.value,        lambda t,x:t(x)),
       
   349                       "WSTRING" :    (None,            None,                    None),#TODO
       
   350                       "DINT" :       (ctypes.c_int32,  lambda x:x.value,        lambda t,x:t(x)),
       
   351                       "UDINT" :      (ctypes.c_uint32, lambda x:x.value,        lambda t,x:t(x)),
       
   352                       "DWORD" :      (ctypes.c_uint32, lambda x:x.value,        lambda t,x:t(x)),
       
   353                       "LINT" :       (ctypes.c_int64,  lambda x:x.value,        lambda t,x:t(x)),
       
   354                       "ULINT" :      (ctypes.c_uint64, lambda x:x.value,        lambda t,x:t(x)),
       
   355                       "LWORD" :      (ctypes.c_uint64, lambda x:x.value,        lambda t,x:t(x)),
       
   356                       "REAL" :       (ctypes.c_float,  lambda x:x.value,        lambda t,x:t(x)),
       
   357                       "LREAL" :      (ctypes.c_double, lambda x:x.value,        lambda t,x:t(x)),
       
   358                       } 
       
   359 
   330     def SetTraceVariablesList(self, idxs):
   360     def SetTraceVariablesList(self, idxs):
   331         """
   361         """
   332         Call ctype imported function to append 
   362         Call ctype imported function to append 
   333         these indexes to registred variables in PLC debugger
   363         these indexes to registred variables in PLC debugger
   334         """
   364         """
   336             # suspend but dont disable
   366             # suspend but dont disable
   337             self._suspendDebug(False)
   367             self._suspendDebug(False)
   338             # keep a copy of requested idx
   368             # keep a copy of requested idx
   339             self._Idxs = idxs[:]
   369             self._Idxs = idxs[:]
   340             self._ResetDebugVariables()
   370             self._ResetDebugVariables()
   341             for idx,iectype in idxs:
   371             for idx,iectype,force in idxs:
   342                 self._RegisterDebugVariable(idx)
   372                 if force !=None:
       
   373                     c_type,unpack_func, pack_func = self.TypeTranslator.get(iectype, (None,None,None))
       
   374                     force = ctypes.byref(pack_func(c_type,force)) 
       
   375                 self._RegisterDebugVariable(idx, force)
   343             self._resumeDebug()
   376             self._resumeDebug()
   344         else:
   377         else:
   345             self._suspendDebug(True)
   378             self._suspendDebug(True)
   346             self._Idxs =  []
   379             self._Idxs =  []
   347 
   380 
   348     class IEC_STRING(ctypes.Structure):
       
   349         """
       
   350         Must be changed according to changes in iec_types.h
       
   351         """
       
   352         _fields_ = [("len", ctypes.c_uint8),
       
   353                     ("body", ctypes.c_char * 126)] 
       
   354     
       
   355     TypeTranslator = {"BOOL" :       (ctypes.c_uint8, lambda x:x.value!=0),
       
   356                       "STEP" :       (ctypes.c_uint8, lambda x:x.value),
       
   357                       "TRANSITION" : (ctypes.c_uint8, lambda x:x.value),
       
   358                       "ACTION" :     (ctypes.c_uint8, lambda x:x.value),
       
   359                       "SINT" :       (ctypes.c_int8, lambda x:x.value),
       
   360                       "USINT" :      (ctypes.c_uint8, lambda x:x.value),
       
   361                       "BYTE" :       (ctypes.c_uint8, lambda x:x.value),
       
   362                       "STRING" :     (IEC_STRING, lambda x:x.body[:x.len]),
       
   363                       "INT" :        (ctypes.c_int16, lambda x:x.value),
       
   364                       "UINT" :       (ctypes.c_uint16, lambda x:x.value),
       
   365                       "WORD" :       (ctypes.c_uint16, lambda x:x.value),
       
   366                       "WSTRING" :    (None, None),#TODO
       
   367                       "DINT" :       (ctypes.c_int32, lambda x:x.value),
       
   368                       "UDINT" :      (ctypes.c_uint32, lambda x:x.value),
       
   369                       "DWORD" :      (ctypes.c_uint32, lambda x:x.value),
       
   370                       "LINT" :       (ctypes.c_int64, lambda x:x.value),
       
   371                       "ULINT" :      (ctypes.c_uint64, lambda x:x.value),
       
   372                       "LWORD" :      (ctypes.c_uint64, lambda x:x.value),
       
   373                       "REAL" :       (ctypes.c_float, lambda x:x.value),
       
   374                       "LREAL" :      (ctypes.c_double, lambda x:x.value),
       
   375                       } 
       
   376                            
       
   377     def GetTraceVariables(self):
   381     def GetTraceVariables(self):
   378         """
   382         """
   379         Return a list of variables, corresponding to the list of required idx
   383         Return a list of variables, corresponding to the list of required idx
   380         """
   384         """
   381         if self.PLCStatus == "Started":
   385         if self.PLCStatus == "Started":
   384             tick = ctypes.c_uint32()
   388             tick = ctypes.c_uint32()
   385             size = ctypes.c_uint32()
   389             size = ctypes.c_uint32()
   386             buffer = ctypes.c_void_p()
   390             buffer = ctypes.c_void_p()
   387             offset = 0
   391             offset = 0
   388             if self._GetDebugData(ctypes.byref(tick),ctypes.byref(size),ctypes.byref(buffer)) == 0 :
   392             if self._GetDebugData(ctypes.byref(tick),ctypes.byref(size),ctypes.byref(buffer)) == 0 :
   389                 for idx, iectype in self._Idxs:
   393                 for idx, iectype, forced in self._Idxs:
   390                     cursor = ctypes.c_void_p(buffer.value + offset)
   394                     cursor = ctypes.c_void_p(buffer.value + offset)
   391                     c_type,unpack_func = self.TypeTranslator.get(iectype, (None,None))
   395                     c_type,unpack_func, pack_func = self.TypeTranslator.get(iectype, (None,None,None))
   392                     if c_type is not None and offset < size:
   396                     if c_type is not None and offset < size:
   393                         res.append(unpack_func(ctypes.cast(cursor,
   397                         res.append(unpack_func(ctypes.cast(cursor,
   394                                                            ctypes.POINTER(c_type)).contents))
   398                                                            ctypes.POINTER(c_type)).contents))
   395                         offset += ctypes.sizeof(c_type)
   399                         offset += ctypes.sizeof(c_type)
   396                     else:
   400                     else: