runtime/PLCObject.py
author etisserant
Tue, 30 Dec 2008 22:43:48 +0100
changeset 283 d0e6fc0701fb
parent 280 f2ef79f3dba0
child 286 a2a8a52b0d4f
permissions -rwxr-xr-x
Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     1
#!/usr/bin/env python
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     2
# -*- coding: utf-8 -*-
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     3
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     6
#
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     8
#
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
     9
#See COPYING file for copyrights details.
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    10
#
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    11
#This library is free software; you can redistribute it and/or
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    12
#modify it under the terms of the GNU General Public
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    13
#License as published by the Free Software Foundation; either
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    15
#
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    16
#This library is distributed in the hope that it will be useful,
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    19
#General Public License for more details.
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    20
#
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    21
#You should have received a copy of the GNU General Public
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    22
#License along with this library; if not, write to the Free Software
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    24
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    25
import Pyro.core as pyro
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
    26
from threading import Timer, Thread
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    27
import ctypes, os, commands
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    28
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    29
if os.name in ("nt", "ce"):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    30
    from _ctypes import LoadLibrary as dlopen
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    31
    from _ctypes import FreeLibrary as dlclose
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    32
elif os.name == "posix":
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    33
    from _ctypes import dlopen, dlclose
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    34
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    35
import os,sys,traceback
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    36
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    37
lib_ext ={
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    38
     "linux2":".so",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    39
     "win32":".dll",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    40
     }.get(sys.platform, "")
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    41
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    42
class PLCObject(pyro.ObjBase):
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
    43
    _Idxs = []
269
d29c5f71574f add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents: 239
diff changeset
    44
    def __init__(self, workingdir, daemon, argv, statuschange=None):
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    45
        pyro.ObjBase.__init__(self)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    46
        self.argv = [workingdir] + argv # force argv[0] to be "path" to exec...
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    47
        self.workingdir = workingdir
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    48
        self.PLCStatus = "Stopped"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    49
        self.PLClibraryHandle = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    50
        # Creates fake C funcs proxies
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    51
        self._FreePLC()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    52
        self.daemon = daemon
269
d29c5f71574f add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents: 239
diff changeset
    53
        self.statuschange = statuschange
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    54
        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    55
        # Get the last transfered PLC if connector must be restart
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    56
        try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    57
            self.CurrentPLCFilename=open(
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    58
                             self._GetMD5FileName(),
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    59
                             "r").read().strip() + lib_ext
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    60
        except Exception, e:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    61
            self.PLCStatus = "Empty"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    62
            self.CurrentPLCFilename=None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    63
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    64
    def _GetMD5FileName(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    65
        return os.path.join(self.workingdir, "lasttransferedPLC.md5")
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    66
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    67
    def _GetLibFileName(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    68
        return os.path.join(self.workingdir,self.CurrentPLCFilename)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    69
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    70
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    71
    def _LoadNewPLC(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    72
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    73
        Load PLC library
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    74
        Declare all functions, arguments and return values
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    75
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    76
        print "Load PLC"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    77
        try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    78
            self._PLClibraryHandle = dlopen(self._GetLibFileName())
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    79
            self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    80
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    81
            self._startPLC = self.PLClibraryHandle.startPLC
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    82
            self._startPLC.restype = ctypes.c_int
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    83
            self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)]
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    84
            
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    85
            self._stopPLC = self.PLClibraryHandle.stopPLC
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    86
            self._stopPLC.restype = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    87
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    88
            self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    89
            self._ResetDebugVariables.restype = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    90
    
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
    91
            self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    92
            self._RegisterDebugVariable.restype = None
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
    93
            self._RegisterDebugVariable.argtypes = [ctypes.c_int]
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    94
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    95
            self._IterDebugData = self.PLClibraryHandle.IterDebugData
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    96
            self._IterDebugData.restype = ctypes.c_void_p
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    97
            self._IterDebugData.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p)]
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    98
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    99
            self._FreeDebugData = self.PLClibraryHandle.FreeDebugData
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   100
            self._FreeDebugData.restype = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   101
            
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   102
            self._WaitDebugData = self.PLClibraryHandle.WaitDebugData
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   103
            self._WaitDebugData.restype = ctypes.c_int  
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   104
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   105
            self._suspendDebug = self.PLClibraryHandle.suspendDebug
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   106
            self._suspendDebug.restype = None
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   107
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   108
            self._resumeDebug = self.PLClibraryHandle.resumeDebug
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   109
            self._resumeDebug.restype = None
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   110
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   111
            self._PythonIterator = self.PLClibraryHandle.PythonIterator
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   112
            self._PythonIterator.restype = ctypes.c_char_p
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   113
            self._PythonIterator.argtypes = [ctypes.c_char_p]
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   114
            
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   115
            return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   116
        except:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   117
            print traceback.format_exc()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   118
            return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   119
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   120
    def _FreePLC(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   121
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   122
        Unload PLC library.
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   123
        This is also called by __init__ to create dummy C func proxies
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   124
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   125
        # Forget all refs to library
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   126
        self._startPLC = lambda:None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   127
        self._stopPLC = lambda:None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   128
        self._ResetDebugVariables = lambda:None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   129
        self._RegisterDebugVariable = lambda x:None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   130
        self._IterDebugData = lambda x,y:None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   131
        self._FreeDebugData = lambda:None
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   132
        self._WaitDebugData = lambda:-1
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   133
        self._suspendDebug = lambda:None
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   134
        self._resumeDebug = lambda:None
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   135
        self._PythonIterator = lambda:""
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   136
        self.PLClibraryHandle = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   137
        # Unload library explicitely
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   138
        if getattr(self,"_PLClibraryHandle",None) is not None:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   139
            print "Unload PLC"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   140
            dlclose(self._PLClibraryHandle)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   141
            res = self._DetectDirtyLibs()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   142
        else:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   143
            res = False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   144
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   145
        self._PLClibraryHandle = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   146
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   147
        return res
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   148
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   149
    def _DetectDirtyLibs(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   150
        # Detect dirty libs
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   151
        # Get lib dependencies (for dirty lib detection)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   152
        if os.name == "posix":
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   153
            # parasiting libs listed with ldd
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   154
            badlibs = [ toks.split()[0] for toks in commands.getoutput(
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   155
                            "ldd "+self._GetLibFileName()).splitlines() ]
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   156
            for badlib in badlibs:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   157
                if badlib[:6] in ["libwx_",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   158
                                  "libwxs",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   159
                                  "libgtk",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   160
                                  "libgdk",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   161
                                  "libatk",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   162
                                  "libpan",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   163
                                  "libX11",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   164
                                  ]:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   165
                    #badhandle = dlopen(badlib, dl.RTLD_NOLOAD)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   166
                    print "Dirty lib detected :" + badlib
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   167
                    #dlclose(badhandle)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   168
                    return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   169
        return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   170
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   171
    def PythonThreadProc(self):
283
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   172
        pyfile = os.path.join(self.workingdir, "runtime.py")
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   173
        if os.path.exists(pyfile):
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   174
            # TODO handle exceptions in runtime.py
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   175
            execfile(pyfile)
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   176
        res = ""
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   177
        print "PythonThreadProc started"
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   178
        while self.PLCStatus == "Started":
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   179
            cmd = self._PythonIterator(res)
283
d0e6fc0701fb Added "runtime.py", a file that is executed in python thread in runtime, before handling python_eval FBs requests. Added small python editor taken from wxPython demo, and appropriate icon and button to launch it.
etisserant
parents: 280
diff changeset
   180
            #print "_PythonIterator(", res, ") -> ", cmd
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   181
            try :
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   182
                res = eval(cmd)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   183
            except Exception,e:
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   184
                res = "#EXCEPTION : "+str(e)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   185
                print res
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   186
        print "PythonThreadProc finished"
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   187
    
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   188
    def StartPLC(self, debug=False):
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   189
        print "StartPLC"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   190
        if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   191
            c_argv = ctypes.c_char_p * len(self.argv)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   192
            if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   193
                if debug:
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   194
                    self._resumeDebug()
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   195
                self.PLCStatus = "Started"
269
d29c5f71574f add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents: 239
diff changeset
   196
                if self.statuschange is not None:
d29c5f71574f add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents: 239
diff changeset
   197
                    self.statuschange(self.PLCStatus)
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   198
                self.PythonThread = Thread(target=self.PythonThreadProc)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   199
                self.PythonThread.start()
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   200
                return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   201
            else:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   202
                print "_StartPLC did not return 0 !"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   203
                self._DoStopPLC()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   204
        return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   205
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   206
    def _DoStopPLC(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   207
        self._stopPLC()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   208
        self.PLCStatus = "Stopped"
269
d29c5f71574f add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents: 239
diff changeset
   209
        if self.statuschange is not None:
d29c5f71574f add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents: 239
diff changeset
   210
            self.statuschange(self.PLCStatus)
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   211
        if self._FreePLC():
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   212
            self.PLCStatus = "Dirty"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   213
        return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   214
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   215
    def StopPLC(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   216
        if self.PLCStatus == "Started":
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   217
            self._DoStopPLC()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   218
            return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   219
        return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   220
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   221
    def _Reload(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   222
        self.daemon.shutdown(True)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   223
        self.daemon.sock.close()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   224
        os.execv(sys.executable,[sys.executable]+sys.argv[:])
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   225
        # never reached
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   226
        return 0
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   227
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   228
    def ForceReload(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   229
        # respawn python interpreter
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   230
        Timer(0.1,self._Reload).start()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   231
        return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   232
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   233
    def GetPLCstatus(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   234
        return self.PLCStatus
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   235
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   236
    def NewPLC(self, md5sum, data, extrafiles):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   237
        print "NewPLC (%s)"%md5sum
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   238
        if self.PLCStatus in ["Stopped", "Empty", "Dirty"]:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   239
            NewFileName = md5sum + lib_ext
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   240
            extra_files_log = os.path.join(self.workingdir,"extra_files.txt")
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   241
            try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   242
                os.remove(os.path.join(self.workingdir,
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   243
                                       self.CurrentPLCFilename))
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   244
                for filename in file(extra_files_log, "r").readlines() + extra_files_log:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   245
                    try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   246
                        os.remove(os.path.join(self.workingdir, filename))
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   247
                    except:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   248
                        pass
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   249
            except:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   250
                pass
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   251
                        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   252
            try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   253
                # Create new PLC file
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   254
                open(os.path.join(self.workingdir,NewFileName),
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   255
                     'wb').write(data)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   256
        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   257
                # Store new PLC filename based on md5 key
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   258
                open(self._GetMD5FileName(), "w").write(md5sum)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   259
        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   260
                # Then write the files
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   261
                log = file(extra_files_log, "w")
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   262
                for fname,fdata in extrafiles:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   263
                    fpath = os.path.join(self.workingdir,fname)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   264
                    open(fpath, "wb").write(fdata)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   265
                    log.write(fname+'\n')
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   266
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   267
                # Store new PLC filename
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   268
                self.CurrentPLCFilename = NewFileName
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   269
            except:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   270
                print traceback.format_exc()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   271
                return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   272
            if self.PLCStatus == "Empty":
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   273
                self.PLCStatus = "Stopped"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   274
            return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   275
        return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   276
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   277
    def MatchMD5(self, MD5):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   278
        try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   279
            last_md5 = open(self._GetMD5FileName(), "r").read()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   280
            return last_md5 == MD5
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   281
        except:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   282
            return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   283
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   284
    def SetTraceVariablesList(self, idxs):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   285
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   286
        Call ctype imported function to append 
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   287
        these indexes to registred variables in PLC debugger
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   288
        """
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   289
        self._suspendDebug()
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   290
        # keep a copy of requested idx
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   291
        self._Idxs = idxs[:]
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   292
        self._ResetDebugVariables()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   293
        for idx in idxs:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   294
            self._RegisterDebugVariable(idx)
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   295
        self._resumeDebug()
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   296
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   297
    class IEC_STRING(ctypes.Structure):
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   298
        """
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   299
        Must be changed according to changes in iec_types.h
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   300
        """
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   301
        _fields_ = [("len", ctypes.c_uint8),
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   302
                    ("body", ctypes.c_char * 40)] 
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   303
    
238
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   304
    TypeTranslator = {"BOOL" :       (ctypes.c_uint8, lambda x:x.value!=0),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   305
                      "STEP" :       (ctypes.c_uint8, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   306
                      "TRANSITION" : (ctypes.c_uint8, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   307
                      "ACTION" :     (ctypes.c_uint8, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   308
                      "SINT" :       (ctypes.c_int8, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   309
                      "USINT" :      (ctypes.c_uint8, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   310
                      "BYTE" :       (ctypes.c_uint8, lambda x:x.value),
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   311
                      "STRING" :     (IEC_STRING, lambda x:x.body[:x.len]),
238
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   312
                      "INT" :        (ctypes.c_int16, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   313
                      "UINT" :       (ctypes.c_uint16, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   314
                      "WORD" :       (ctypes.c_uint16, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   315
                      "WSTRING" :    (None, None),#TODO
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   316
                      "DINT" :       (ctypes.c_int32, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   317
                      "UDINT" :      (ctypes.c_uint32, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   318
                      "DWORD" :      (ctypes.c_uint32, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   319
                      "LINT" :       (ctypes.c_int64, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   320
                      "ULINT" :      (ctypes.c_uint64, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   321
                      "LWORD" :      (ctypes.c_uint64, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   322
                      "REAL" :       (ctypes.c_float, lambda x:x.value),
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   323
                      "LREAL" :      (ctypes.c_double, lambda x:x.value),
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   324
                      } 
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   325
                           
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   326
    def GetTraceVariables(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   327
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   328
        Return a list of variables, corresponding to the list of requiered idx
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   329
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   330
        tick = self._WaitDebugData()
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   331
        if tick == -1:
237
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   332
            res = None
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   333
        else:
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   334
            idx = ctypes.c_int()
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   335
            typename = ctypes.c_char_p()
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   336
            res = []
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   337
    
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   338
            for given_idx in self._Idxs:
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   339
                buffer=self._IterDebugData(ctypes.byref(idx), ctypes.byref(typename))
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 238
diff changeset
   340
                c_type,unpack_func = self.TypeTranslator.get(typename.value, (None,None))
237
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   341
                if c_type is not None and given_idx == idx.value:
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 238
diff changeset
   342
                    res.append(unpack_func(ctypes.cast(buffer,
238
02d0daed3e46 Debugger now reports BOOL as booleans
etisserant
parents: 237
diff changeset
   343
                                                       ctypes.POINTER(c_type)).contents))
237
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   344
                else:
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   345
                    print "Debug error idx : %d, expected_idx %d, type : %s"%(idx.value, given_idx,typename.value)
9a50936bc55f Some scenario may block debugger.
etisserant
parents: 235
diff changeset
   346
                    res.append(None)
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   347
        self._FreeDebugData()
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   348
        return tick, res
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   349
        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   350
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   351