runtime/PLCObject.py
changeset 592 c6408f92da0a
parent 504 688e84df3408
child 593 726f58cf97e3
equal deleted inserted replaced
591:3ece9ba72aaf 592:c6408f92da0a
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 import Pyro.core as pyro
    25 import Pyro.core as pyro
    26 from threading import Timer, Thread, Lock
    26 from threading import Timer, Thread, Lock
    27 import ctypes, os, commands, types, sys
    27 import ctypes, os, commands, types, sys
       
    28 from datetime import timedelta as td
       
    29 from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator
    28 
    30 
    29 if os.name in ("nt", "ce"):
    31 if os.name in ("nt", "ce"):
    30     from _ctypes import LoadLibrary as dlopen
    32     from _ctypes import LoadLibrary as dlopen
    31     from _ctypes import FreeLibrary as dlclose
    33     from _ctypes import FreeLibrary as dlclose
    32 elif os.name == "posix":
    34 elif os.name == "posix":
   318             return last_md5 == MD5
   320             return last_md5 == MD5
   319         except:
   321         except:
   320             return False
   322             return False
   321     
   323     
   322 
   324 
   323     class IEC_STRING(ctypes.Structure):
   325     
   324         """
       
   325         Must be changed according to changes in iec_types.h
       
   326         """
       
   327         _fields_ = [("len", ctypes.c_uint8),
       
   328                     ("body", ctypes.c_char * 126)] 
       
   329     
       
   330     TypeTranslator = {"BOOL" :       (ctypes.c_uint8,  lambda x:x.value!=0,     lambda t,x:t(x)),
       
   331                       "STEP" :       (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   332                       "TRANSITION" : (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   333                       "ACTION" :     (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   334                       "SINT" :       (ctypes.c_int8,   lambda x:x.value,        lambda t,x:t(x)),
       
   335                       "USINT" :      (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   336                       "BYTE" :       (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
       
   337                       "STRING" :     (IEC_STRING,      lambda x:x.body[:x.len], lambda t,x:t(len(x),x)),
       
   338                       "INT" :        (ctypes.c_int16,  lambda x:x.value,        lambda t,x:t(x)),
       
   339                       "UINT" :       (ctypes.c_uint16, lambda x:x.value,        lambda t,x:t(x)),
       
   340                       "WORD" :       (ctypes.c_uint16, lambda x:x.value,        lambda t,x:t(x)),
       
   341                       "WSTRING" :    (None,            None,                    None),#TODO
       
   342                       "DINT" :       (ctypes.c_int32,  lambda x:x.value,        lambda t,x:t(x)),
       
   343                       "UDINT" :      (ctypes.c_uint32, lambda x:x.value,        lambda t,x:t(x)),
       
   344                       "DWORD" :      (ctypes.c_uint32, lambda x:x.value,        lambda t,x:t(x)),
       
   345                       "LINT" :       (ctypes.c_int64,  lambda x:x.value,        lambda t,x:t(x)),
       
   346                       "ULINT" :      (ctypes.c_uint64, lambda x:x.value,        lambda t,x:t(x)),
       
   347                       "LWORD" :      (ctypes.c_uint64, lambda x:x.value,        lambda t,x:t(x)),
       
   348                       "REAL" :       (ctypes.c_float,  lambda x:x.value,        lambda t,x:t(x)),
       
   349                       "LREAL" :      (ctypes.c_double, lambda x:x.value,        lambda t,x:t(x)),
       
   350                       } 
       
   351 
       
   352     def SetTraceVariablesList(self, idxs):
   326     def SetTraceVariablesList(self, idxs):
   353         """
   327         """
   354         Call ctype imported function to append 
   328         Call ctype imported function to append 
   355         these indexes to registred variables in PLC debugger
   329         these indexes to registred variables in PLC debugger
   356         """
   330         """
   390                     for idx, iectype, forced in self._Idxs:
   364                     for idx, iectype, forced in self._Idxs:
   391                         cursor = ctypes.c_void_p(buffer.value + offset)
   365                         cursor = ctypes.c_void_p(buffer.value + offset)
   392                         c_type,unpack_func, pack_func = \
   366                         c_type,unpack_func, pack_func = \
   393                             self.TypeTranslator.get(iectype,
   367                             self.TypeTranslator.get(iectype,
   394                                                     (None,None,None))
   368                                                     (None,None,None))
   395                         if c_type is not None and offset < size:
   369                         if c_type is not None and offset < size.value:
   396                             res.append(unpack_func(
   370                             res.append(unpack_func(
   397                                         ctypes.cast(cursor,
   371                                         ctypes.cast(cursor,
   398                                          ctypes.POINTER(c_type)).contents))
   372                                          ctypes.POINTER(c_type)).contents))
   399                             offset += ctypes.sizeof(c_type)
   373                             offset += ctypes.sizeof(c_type)
   400                         else:
   374                         else:
   401                             if c_type is None:
   375                             if c_type is None:
   402                                 PLCprint("Debug error - " + iectype +
   376                                 PLCprint("Debug error - " + iectype +
   403                                          " not supported !")
   377                                          " not supported !")
   404                             if offset >= size:
   378                             #if offset >= size.value:
   405                                 PLCprint("Debug error - buffer too small !")
   379                                 #PLCprint("Debug error - buffer too small ! %d != %d"%(offset, size.value))
   406                             break
   380                             break
   407                 self._FreeDebugData()
   381                 self._FreeDebugData()
   408                 self.PLClibraryLock.release()
   382                 self.PLClibraryLock.release()
   409             if offset and offset == size.value:
   383             if offset and offset == size.value:
   410                 return self.PLCStatus, tick.value, res
   384                 return self.PLCStatus, tick.value, res
   411             elif size.value:
   385             #elif size.value:
   412                 PLCprint("Debug error - wrong buffer unpack !")
   386                 #PLCprint("Debug error - wrong buffer unpack ! %d != %d"%(offset, size.value))
   413         return self.PLCStatus, None, None
   387         return self.PLCStatus, None, []
   414 
   388