connectors/LPC/LPCObject.py
changeset 502 5343ae43f6d0
parent 453 923d036dfa90
child 508 73ecb803d8af
equal deleted inserted replaced
501:d7bf56b036a8 502:5343ae43f6d0
    31     def __init__(self,pluginsroot):
    31     def __init__(self,pluginsroot):
    32         self.PLCStatus = "Stopped"
    32         self.PLCStatus = "Stopped"
    33         self.pluginsroot = pluginsroot
    33         self.pluginsroot = pluginsroot
    34         self.PLCprint = pluginsroot.logger.write
    34         self.PLCprint = pluginsroot.logger.write
    35         self.SerialConnection = None
    35         self.SerialConnection = None
       
    36         self.StorageConnection = None
    36         self._Idxs = []
    37         self._Idxs = []
    37         
    38         
    38     def HandleSerialTransaction(self, transaction):
    39     def HandleSerialTransaction(self, transaction):
    39         if self.SerialConnection is None:
    40         if self.SerialConnection is None:
    40             try:
    41             try:
    70         pass
    71         pass
    71 
    72 
    72     def MatchMD5(self, MD5):
    73     def MatchMD5(self, MD5):
    73         status,data = self.HandleSerialTransaction(PLCIDTransaction())
    74         status,data = self.HandleSerialTransaction(PLCIDTransaction())
    74         return data == MD5
    75         return data == MD5
    75     
       
    76     def SetTraceVariablesList(self, idxs):
       
    77         self._Idxs = idxs[]
       
    78         status,data = self.HandleSerialTransaction(
       
    79                SET_TRACE_VARIABLETransaction(
       
    80                      ''.join(map(chr,idx))))
       
    81 
    76 
    82     class IEC_STRING(ctypes.Structure):
    77     class IEC_STRING(ctypes.Structure):
    83         """
    78         """
    84         Must be changed according to changes in iec_types.h
    79         Must be changed according to changes in iec_types.h
    85         """
    80         """
    86         _fields_ = [("len", ctypes.c_uint8),
    81         _fields_ = [("len", ctypes.c_uint8),
    87                     ("body", ctypes.c_char * 127)] 
    82                     ("body", ctypes.c_char * 126)] 
    88     
    83     
    89     TypeTranslator = {"BOOL" :       (ctypes.c_uint8, lambda x:x.value!=0),
    84     TypeTranslator = {"BOOL" :       (ctypes.c_uint8,  lambda x:x.value!=0,     lambda t,x:t(x)),
    90                       "STEP" :       (ctypes.c_uint8, lambda x:x.value),
    85                       "STEP" :       (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
    91                       "TRANSITION" : (ctypes.c_uint8, lambda x:x.value),
    86                       "TRANSITION" : (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
    92                       "ACTION" :     (ctypes.c_uint8, lambda x:x.value),
    87                       "ACTION" :     (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
    93                       "SINT" :       (ctypes.c_int8, lambda x:x.value),
    88                       "SINT" :       (ctypes.c_int8,   lambda x:x.value,        lambda t,x:t(x)),
    94                       "USINT" :      (ctypes.c_uint8, lambda x:x.value),
    89                       "USINT" :      (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
    95                       "BYTE" :       (ctypes.c_uint8, lambda x:x.value),
    90                       "BYTE" :       (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
    96                       "STRING" :     (IEC_STRING, lambda x:x.body[:x.len]),
    91                       "STRING" :     (IEC_STRING,      lambda x:x.body[:x.len], lambda t,x:t(len(x),x)),
    97                       "INT" :        (ctypes.c_int16, lambda x:x.value),
    92                       "INT" :        (ctypes.c_int16,  lambda x:x.value,        lambda t,x:t(x)),
    98                       "UINT" :       (ctypes.c_uint16, lambda x:x.value),
    93                       "UINT" :       (ctypes.c_uint16, lambda x:x.value,        lambda t,x:t(x)),
    99                       "WORD" :       (ctypes.c_uint16, lambda x:x.value),
    94                       "WORD" :       (ctypes.c_uint16, lambda x:x.value,        lambda t,x:t(x)),
   100                       "WSTRING" :    (None, None),#TODO
    95                       "WSTRING" :    (None,            None,                    None),#TODO
   101                       "DINT" :       (ctypes.c_int32, lambda x:x.value),
    96                       "DINT" :       (ctypes.c_int32,  lambda x:x.value,        lambda t,x:t(x)),
   102                       "UDINT" :      (ctypes.c_uint32, lambda x:x.value),
    97                       "UDINT" :      (ctypes.c_uint32, lambda x:x.value,        lambda t,x:t(x)),
   103                       "DWORD" :      (ctypes.c_uint32, lambda x:x.value),
    98                       "DWORD" :      (ctypes.c_uint32, lambda x:x.value,        lambda t,x:t(x)),
   104                       "LINT" :       (ctypes.c_int64, lambda x:x.value),
    99                       "LINT" :       (ctypes.c_int64,  lambda x:x.value,        lambda t,x:t(x)),
   105                       "ULINT" :      (ctypes.c_uint64, lambda x:x.value),
   100                       "ULINT" :      (ctypes.c_uint64, lambda x:x.value,        lambda t,x:t(x)),
   106                       "LWORD" :      (ctypes.c_uint64, lambda x:x.value),
   101                       "LWORD" :      (ctypes.c_uint64, lambda x:x.value,        lambda t,x:t(x)),
   107                       "REAL" :       (ctypes.c_float, lambda x:x.value),
   102                       "REAL" :       (ctypes.c_float,  lambda x:x.value,        lambda t,x:t(x)),
   108                       "LREAL" :      (ctypes.c_double, lambda x:x.value),
   103                       "LREAL" :      (ctypes.c_double, lambda x:x.value,        lambda t,x:t(x)),
   109                       } 
   104                       } 
   110                            
   105 
       
   106     def SetTraceVariablesList(self, idxs):
       
   107         self._Idxs = idxs[:]
       
   108         status,data = self.HandleSerialTransaction(
       
   109                SET_TRACE_VARIABLETransaction(
       
   110                      ''.join(map(chr,idx))))
       
   111 
       
   112     def SetTraceVariablesList(self, idxs):
       
   113         """
       
   114         Call ctype imported function to append 
       
   115         these indexes to registred variables in PLC debugger
       
   116         """
       
   117         if idxs:
       
   118             buff = ""
       
   119             # keep a copy of requested idx
       
   120             self._Idxs = idxs[:]
       
   121             for idx,iectype,force in idxs:
       
   122                 idxstr = ctypes.string_at(
       
   123                           ctypes.pointer(
       
   124                            ctypes.c_uint32(length)),4)
       
   125                 if force !=None:
       
   126                     c_type,unpack_func, pack_func = self.TypeTranslator.get(iectype, (None,None,None))
       
   127                     forcedsizestr = chr(ctypes.sizeof(c_type))
       
   128                     forcestr = ctypes.string_at(
       
   129                                 ctypes.pointer(
       
   130                                  pack_func(c_type,force)),
       
   131                                  forced_type_size)
       
   132                     buff += idxstr + forced_type_size_str + forcestr
       
   133                 else:
       
   134                     buff += idxstr + chr(0)
       
   135             status,data = self.HandleSerialTransaction(
       
   136                    SET_TRACE_VARIABLETransaction(buff))
       
   137         else:
       
   138             self._Idxs =  []
       
   139 
   111     def GetTraceVariables(self):
   140     def GetTraceVariables(self):
   112         """
   141         """
   113         Return a list of variables, corresponding to the list of required idx
   142         Return a list of variables, corresponding to the list of required idx
   114         """
   143         """
   115         status,data = self.HandleSerialTransaction(GET_TRACE_VARIABLETransaction())
   144         if self.PLCStatus == "Started":
   116         if data is not None:
   145             res=[]
   117             # transform serial string to real byte string in memory 
   146             tick = ctypes.c_uint32()
   118             buffer = ctypes.c_char_p(data)
   147             size = ctypes.c_uint32()
   119             # tick is first value in buffer
   148             buffer = ctypes.c_void_p()
   120             tick = ctypes.cast(buffer,ctypes.POINTER(ctypes.c_uint32)).contents
   149             offset = 0
   121             # variable data starts just after tick 
   150             if self.PLClibraryLock.acquire(False) and \
   122             cursorp = ctypes.addressof(buffer) = ctypes.sizeof(ctypes.c_uint32)
   151                self._GetDebugData(ctypes.byref(tick),ctypes.byref(size),ctypes.byref(buffer)) == 0 :
   123             endp = offset + len(data)
   152                 if size.value:
   124             for idx, iectype in self._Idxs:
   153                     for idx, iectype, forced in self._Idxs:
   125                 cursor = ctypes.c_void_p(cursorp)
   154                         cursor = ctypes.c_void_p(buffer.value + offset)
   126                 c_type,unpack_func = self.TypeTranslator.get(iectype, (None,None))
   155                         c_type,unpack_func, pack_func = self.TypeTranslator.get(iectype, (None,None,None))
   127                 if c_type is not None and cursorp < endp:
   156                         if c_type is not None and offset < size:
   128                     res.append(unpack_func(ctypes.cast(cursor,
   157                             res.append(unpack_func(ctypes.cast(cursor,
   129                                                        ctypes.POINTER(c_type)).contents))
   158                                                                ctypes.POINTER(c_type)).contents))
   130                     cursorp += ctypes.sizeof(c_type) 
   159                             offset += ctypes.sizeof(c_type)
   131                 else:
   160                         else:
   132                     PLCprint("Debug error !")
   161                             if c_type is None:
   133                         break
   162                                 PLCprint("Debug error - " + iectype + " not supported !")
   134             return self.PLCStatus, tick, res
   163                             if offset >= size:
       
   164                                 PLCprint("Debug error - buffer too small !")
       
   165                             break
       
   166                 self._FreeDebugData()
       
   167                 self.PLClibraryLock.release()
       
   168             if offset and offset == size.value:
       
   169                 return self.PLCStatus, tick.value, res
       
   170             elif size.value:
       
   171                 PLCprint("Debug error - wrong buffer unpack !")
   135         return self.PLCStatus, None, None
   172         return self.PLCStatus, None, None
   136 
   173