connectors/LPC/LPCObject.py
changeset 448 8ef035de86de
child 453 923d036dfa90
equal deleted inserted replaced
447:af3399aca7b7 448:8ef035de86de
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of Beremiz, a Integrated Development Environment for
       
     5 #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 from threading import Timer, Thread, Lock
       
    26 import ctypes, os, commands, types, sys
       
    27 import traceback
       
    28 
       
    29 class LPCObject():
       
    30     _Idxs = []
       
    31     def __init__(self,pluginsroot):
       
    32         self.PLCStatus = "Stopped"
       
    33         self.pluginsroot = pluginsroot
       
    34         self.PLCprint = pluginsroot.logger.write
       
    35     
       
    36     def StartPLC(self, debug=False):
       
    37         PLCprint("StartPLC")
       
    38         if self.CurrentPLCFilename is not None:
       
    39             self.PLCStatus = "Started"
       
    40             self.PythonThread = Thread(target=self.PythonThreadProc, args=[debug])
       
    41             self.PythonThread.start()
       
    42             
       
    43     def StopPLC(self):
       
    44         PLCprint("StopPLC")
       
    45         if self.PLCStatus == "Started":
       
    46             self._stopPLC()
       
    47             return True
       
    48         return False
       
    49 
       
    50     def ForceReload(self):
       
    51         # respawn python interpreter
       
    52         Timer(0.1,self._Reload).start()
       
    53         return True
       
    54 
       
    55     def GetPLCstatus(self):
       
    56         return self.PLCStatus
       
    57     
       
    58     def NewPLC(self, md5sum, data, extrafiles):
       
    59         PLCprint("NewPLC (%s)"%md5sum)
       
    60         if self.PLCStatus in ["Stopped", "Empty", "Broken"]:
       
    61             NewFileName = md5sum + lib_ext
       
    62             extra_files_log = os.path.join(self.workingdir,"extra_files.txt")
       
    63             try:
       
    64                 os.remove(os.path.join(self.workingdir,
       
    65                                        self.CurrentPLCFilename))
       
    66                 for filename in file(extra_files_log, "r").readlines() + [extra_files_log]:
       
    67                     try:
       
    68                         os.remove(os.path.join(self.workingdir, filename.strip()))
       
    69                     except:
       
    70                         pass
       
    71             except:
       
    72                 pass
       
    73                         
       
    74             try:
       
    75                 # Create new PLC file
       
    76                 open(os.path.join(self.workingdir,NewFileName),
       
    77                      'wb').write(data)
       
    78         
       
    79                 # Store new PLC filename based on md5 key
       
    80                 open(self._GetMD5FileName(), "w").write(md5sum)
       
    81         
       
    82                 # Then write the files
       
    83                 log = file(extra_files_log, "w")
       
    84                 for fname,fdata in extrafiles:
       
    85                     fpath = os.path.join(self.workingdir,fname)
       
    86                     open(fpath, "wb").write(fdata)
       
    87                     log.write(fname+'\n')
       
    88 
       
    89                 # Store new PLC filename
       
    90                 self.CurrentPLCFilename = NewFileName
       
    91             except:
       
    92                 PLCprint(traceback.format_exc())
       
    93                 return False
       
    94             if self.PLCStatus == "Empty":
       
    95                 self.PLCStatus = "Stopped"
       
    96             return True
       
    97         return False
       
    98 
       
    99     def MatchMD5(self, MD5):
       
   100         try:
       
   101             last_md5 = open(self._GetMD5FileName(), "r").read()
       
   102             return last_md5 == MD5
       
   103         except:
       
   104             return False
       
   105     
       
   106     def SetTraceVariablesList(self, idxs):
       
   107         """
       
   108         Call ctype imported function to append 
       
   109         these indexes to registred variables in PLC debugger
       
   110         """
       
   111         self._suspendDebug()
       
   112         # keep a copy of requested idx
       
   113         self._Idxs = idxs[:]
       
   114         self._ResetDebugVariables()
       
   115         for idx in idxs:
       
   116             self._RegisterDebugVariable(idx)
       
   117         self._resumeDebug()
       
   118 
       
   119     class IEC_STRING(ctypes.Structure):
       
   120         """
       
   121         Must be changed according to changes in iec_types.h
       
   122         """
       
   123         _fields_ = [("len", ctypes.c_uint8),
       
   124                     ("body", ctypes.c_char * 127)] 
       
   125     
       
   126     TypeTranslator = {"BOOL" :       (ctypes.c_uint8, lambda x:x.value!=0),
       
   127                       "STEP" :       (ctypes.c_uint8, lambda x:x.value),
       
   128                       "TRANSITION" : (ctypes.c_uint8, lambda x:x.value),
       
   129                       "ACTION" :     (ctypes.c_uint8, lambda x:x.value),
       
   130                       "SINT" :       (ctypes.c_int8, lambda x:x.value),
       
   131                       "USINT" :      (ctypes.c_uint8, lambda x:x.value),
       
   132                       "BYTE" :       (ctypes.c_uint8, lambda x:x.value),
       
   133                       "STRING" :     (IEC_STRING, lambda x:x.body[:x.len]),
       
   134                       "INT" :        (ctypes.c_int16, lambda x:x.value),
       
   135                       "UINT" :       (ctypes.c_uint16, lambda x:x.value),
       
   136                       "WORD" :       (ctypes.c_uint16, lambda x:x.value),
       
   137                       "WSTRING" :    (None, None),#TODO
       
   138                       "DINT" :       (ctypes.c_int32, lambda x:x.value),
       
   139                       "UDINT" :      (ctypes.c_uint32, lambda x:x.value),
       
   140                       "DWORD" :      (ctypes.c_uint32, lambda x:x.value),
       
   141                       "LINT" :       (ctypes.c_int64, lambda x:x.value),
       
   142                       "ULINT" :      (ctypes.c_uint64, lambda x:x.value),
       
   143                       "LWORD" :      (ctypes.c_uint64, lambda x:x.value),
       
   144                       "REAL" :       (ctypes.c_float, lambda x:x.value),
       
   145                       "LREAL" :      (ctypes.c_double, lambda x:x.value),
       
   146                       } 
       
   147                            
       
   148     def GetTraceVariables(self):
       
   149         """
       
   150         Return a list of variables, corresponding to the list of required idx
       
   151         """
       
   152         if self.PLCStatus == "Started":
       
   153             self.PLClibraryLock.acquire()
       
   154             tick = self._WaitDebugData()
       
   155             #PLCprint("Debug tick : %d"%tick)
       
   156             if tick == 2**32 - 1:
       
   157                 tick = -1
       
   158                 res = None
       
   159             else:
       
   160                 idx = ctypes.c_int()
       
   161                 typename = ctypes.c_char_p()
       
   162                 res = []
       
   163         
       
   164                 for given_idx in self._Idxs:
       
   165                     buffer=self._IterDebugData(ctypes.byref(idx), ctypes.byref(typename))
       
   166                     c_type,unpack_func = self.TypeTranslator.get(typename.value, (None,None))
       
   167                     if c_type is not None and given_idx == idx.value:
       
   168                         res.append(unpack_func(ctypes.cast(buffer,
       
   169                                                            ctypes.POINTER(c_type)).contents))
       
   170                     else:
       
   171                         PLCprint("Debug error idx : %d, expected_idx %d, type : %s"%(idx.value, given_idx,typename.value))
       
   172                         res.append(None)
       
   173             self._FreeDebugData()
       
   174             self.PLClibraryLock.release()
       
   175             return tick, res
       
   176         return -1, None
       
   177