runtime/PLCObject.py
author Edouard Tisserant
Mon, 07 May 2012 18:47:29 +0200
changeset 717 1c23952dbde1
parent 699 6ff64cadb1ff
child 734 5c42cafaee15
permissions -rwxr-xr-x
refactoring
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
690
ef60d7e188e6 Added a semaphore when starting runtime's python thread to make sure startPLC doesn't return before PLC is really initialized.
Edouard Tisserant
parents: 614
diff changeset
    26
from threading import Timer, Thread, Lock, Semaphore
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 299
diff changeset
    27
import ctypes, os, commands, types, sys
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
    28
from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 299
diff changeset
    29
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    30
if os.name in ("nt", "ce"):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    31
    from _ctypes import LoadLibrary as dlopen
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    32
    from _ctypes import FreeLibrary as dlclose
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    33
elif os.name == "posix":
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    34
    from _ctypes import dlopen, dlclose
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    35
344
25b7b7f854bc Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents: 339
diff changeset
    36
import traceback
699
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
    37
def get_last_traceback(tb):
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
    38
    while tb.tb_next:
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
    39
        tb = tb.tb_next
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
    40
    return tb
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    41
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    42
lib_ext ={
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    43
     "linux2":".so",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    44
     "win32":".dll",
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    45
     }.get(sys.platform, "")
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    46
291
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
    47
def PLCprint(message):
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
    48
    sys.stdout.write("PLCobject : "+message+"\n")
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
    49
    sys.stdout.flush()
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
    50
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    51
class PLCObject(pyro.ObjBase):
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
    52
    _Idxs = []
368
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
    53
    def __init__(self, workingdir, daemon, argv, statuschange, evaluator, website):
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    54
        pyro.ObjBase.__init__(self)
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 299
diff changeset
    55
        self.evaluator = evaluator
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    56
        self.argv = [workingdir] + argv # force argv[0] to be "path" to exec...
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    57
        self.workingdir = workingdir
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    58
        self.PLCStatus = "Stopped"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    59
        self.PLClibraryHandle = None
352
81777d4e379c fixed bug : Lock _FreePLC until _stopPLC finish
greg
parents: 350
diff changeset
    60
        self.PLClibraryLock = Lock()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
    61
        self.DummyIteratorLock = None
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    62
        # Creates fake C funcs proxies
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    63
        self._FreePLC()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    64
        self.daemon = daemon
269
d29c5f71574f add a TaskBarIcon to configure beremiz_service and display plc states (started, stopped)
greg
parents: 239
diff changeset
    65
        self.statuschange = statuschange
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 299
diff changeset
    66
        self.hmi_frame = None
368
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
    67
        self.website = website
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    68
        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    69
        # Get the last transfered PLC if connector must be restart
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    70
        try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    71
            self.CurrentPLCFilename=open(
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    72
                             self._GetMD5FileName(),
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    73
                             "r").read().strip() + lib_ext
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    74
        except Exception, e:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    75
            self.PLCStatus = "Empty"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    76
            self.CurrentPLCFilename=None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    77
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
    78
    def StatusChange(self):
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
    79
        if self.statuschange is not None:
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
    80
            self.statuschange(self.PLCStatus)
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
    81
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    82
    def _GetMD5FileName(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    83
        return os.path.join(self.workingdir, "lasttransferedPLC.md5")
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    84
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    85
    def _GetLibFileName(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    86
        return os.path.join(self.workingdir,self.CurrentPLCFilename)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    87
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    88
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    89
    def _LoadNewPLC(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    90
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    91
        Load PLC library
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    92
        Declare all functions, arguments and return values
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    93
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    94
        try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    95
            self._PLClibraryHandle = dlopen(self._GetLibFileName())
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    96
            self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    97
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    98
            self._startPLC = self.PLClibraryHandle.startPLC
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
    99
            self._startPLC.restype = ctypes.c_int
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   100
            self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)]
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   101
            
455
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   102
            self._stopPLC_real = self.PLClibraryHandle.stopPLC
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   103
            self._stopPLC_real.restype = None
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   104
            
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   105
            self._PythonIterator = getattr(self.PLClibraryHandle, "PythonIterator", None)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   106
            if self._PythonIterator is not None:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   107
                self._PythonIterator.restype = ctypes.c_char_p
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   108
                self._PythonIterator.argtypes = [ctypes.c_char_p]
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   109
                
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   110
                self._stopPLC = self._stopPLC_real
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   111
            else:
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 699
diff changeset
   112
                # If python confnode is not enabled, we reuse _PythonIterator
455
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   113
                # as a call that block pythonthread until StopPLC 
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   114
                self.PythonIteratorLock = Lock()
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   115
                self.PythonIteratorLock.acquire()
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   116
                def PythonIterator(res):
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   117
                    self.PythonIteratorLock.acquire()
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   118
                    self.PythonIteratorLock.release()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   119
                    return None
455
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   120
                self._PythonIterator = PythonIterator
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   121
                
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   122
                def __StopPLC():
455
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   123
                    self._stopPLC_real()
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   124
                    self.PythonIteratorLock.release()
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   125
                self._stopPLC = __StopPLC
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   126
            
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   127
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   128
            self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   129
            self._ResetDebugVariables.restype = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   130
    
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   131
            self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   132
            self._RegisterDebugVariable.restype = None
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 467
diff changeset
   133
            self._RegisterDebugVariable.argtypes = [ctypes.c_int, ctypes.c_void_p]
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   134
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   135
            self._FreeDebugData = self.PLClibraryHandle.FreeDebugData
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   136
            self._FreeDebugData.restype = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   137
            
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   138
            self._GetDebugData = self.PLClibraryHandle.GetDebugData
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   139
            self._GetDebugData.restype = ctypes.c_int  
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   140
            self._GetDebugData.argtypes = [ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_void_p)]
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   141
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   142
            self._suspendDebug = self.PLClibraryHandle.suspendDebug
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   143
            self._suspendDebug.restype = ctypes.c_int
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   144
            self._suspendDebug.argtypes = [ctypes.c_int]
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   145
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   146
            self._resumeDebug = self.PLClibraryHandle.resumeDebug
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   147
            self._resumeDebug.restype = None
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   148
            
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   149
            return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   150
        except:
291
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
   151
            PLCprint(traceback.format_exc())
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   152
            return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   153
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   154
    def _FreePLC(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   155
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   156
        Unload PLC library.
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   157
        This is also called by __init__ to create dummy C func proxies
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   158
        """
352
81777d4e379c fixed bug : Lock _FreePLC until _stopPLC finish
greg
parents: 350
diff changeset
   159
        self.PLClibraryLock.acquire()
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   160
        # Forget all refs to library
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   161
        self._startPLC = lambda:None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   162
        self._stopPLC = lambda:None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   163
        self._ResetDebugVariables = lambda:None
479
c28f40b27798 Bug on RegisterDebugVariable when no PLC running fixed
laurent
parents: 477
diff changeset
   164
        self._RegisterDebugVariable = lambda x, y:None
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   165
        self._IterDebugData = lambda x,y:None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   166
        self._FreeDebugData = lambda:None
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   167
        self._GetDebugData = lambda:-1
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   168
        self._suspendDebug = lambda x:-1
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 229
diff changeset
   169
        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
   170
        self._PythonIterator = lambda:""
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   171
        self.PLClibraryHandle = None
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   172
        # Unload library explicitely
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   173
        if getattr(self,"_PLClibraryHandle",None) is not None:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   174
            dlclose(self._PLClibraryHandle)
393
af20e07e53c5 Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents: 368
diff changeset
   175
            self._PLClibraryHandle = None
af20e07e53c5 Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents: 368
diff changeset
   176
        
352
81777d4e379c fixed bug : Lock _FreePLC until _stopPLC finish
greg
parents: 350
diff changeset
   177
        self.PLClibraryLock.release()
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   178
        return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   179
299
d7f8ffe18017 Enhanced the way "runtime.py" is executed,
etisserant
parents: 291
diff changeset
   180
    def PrepareRuntimePy(self):
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 286
diff changeset
   181
        self.python_threads_vars = globals().copy()
344
25b7b7f854bc Wait the debug thread has terminated before freeing PLC to avoid random segmentation fault.
greg
parents: 339
diff changeset
   182
        self.python_threads_vars["WorkingDir"] = self.workingdir
368
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
   183
        self.python_threads_vars["website"] = self.website
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   184
        self.python_threads_vars["_runtime_begin"] = []
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   185
        self.python_threads_vars["_runtime_cleanup"] = []
368
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
   186
        
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   187
        for filename in os.listdir(self.workingdir):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   188
            name, ext = os.path.splitext(filename)
391
9b1801ef99b5 fix runtime.py filename case to avoid problem on multi-platform
greg
parents: 368
diff changeset
   189
            if name.upper().startswith("RUNTIME") and ext.upper() == ".PY":
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   190
                try:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   191
                    # TODO handle exceptions in runtime.py
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   192
                    # pyfile may redefine _runtime_cleanup
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   193
                    # or even call _PythonThreadProc itself.
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   194
                    execfile(os.path.join(self.workingdir, filename), self.python_threads_vars)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   195
                except:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   196
                    PLCprint(traceback.format_exc())
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   197
                runtime_begin = self.python_threads_vars.get("_%s_begin" % name, None)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   198
                if runtime_begin is not None:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   199
                    self.python_threads_vars["_runtime_begin"].append(runtime_begin)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   200
                runtime_cleanup = self.python_threads_vars.get("_%s_cleanup" % name, None)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   201
                if runtime_cleanup is not None:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   202
                    self.python_threads_vars["_runtime_cleanup"].append(runtime_cleanup)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   203
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   204
        for runtime_begin in self.python_threads_vars.get("_runtime_begin", []):
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 327
diff changeset
   205
            runtime_begin()
368
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
   206
            
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
   207
        if self.website is not None:
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
   208
            self.website.PLCStarted()
291
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
   209
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
   210
    def FinishRuntimePy(self):
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   211
        for runtime_cleanup in self.python_threads_vars.get("_runtime_cleanup", []):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents: 365
diff changeset
   212
            runtime_cleanup()    
368
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
   213
        if self.website is not None:
86ecd8374dae Adding support for twisted website HMI
laurent
parents: 366
diff changeset
   214
            self.website.PLCStopped()
291
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
   215
        self.python_threads_vars = None
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
   216
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   217
    def PythonThreadProc(self):
350
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   218
        c_argv = ctypes.c_char_p * len(self.argv)
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   219
        error = None
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   220
        if self._LoadNewPLC():
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   221
            if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   222
                self.PLCStatus = "Started"
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
   223
                self.StatusChange()
690
ef60d7e188e6 Added a semaphore when starting runtime's python thread to make sure startPLC doesn't return before PLC is really initialized.
Edouard Tisserant
parents: 614
diff changeset
   224
                self.StartSem.release()
350
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   225
                self.evaluator(self.PrepareRuntimePy)
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   226
                res,cmd = "None","None"
352
81777d4e379c fixed bug : Lock _FreePLC until _stopPLC finish
greg
parents: 350
diff changeset
   227
                while True:
350
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   228
                    #print "_PythonIterator(", res, ")",
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   229
                    cmd = self._PythonIterator(res)
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   230
                    #print " -> ", cmd
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   231
                    if cmd is None:
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   232
                        break
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   233
                    try :
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   234
                        res = str(self.evaluator(eval,cmd,self.python_threads_vars))
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   235
                    except Exception,e:
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   236
                        res = "#EXCEPTION : "+str(e)
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   237
                        PLCprint(res)
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   238
                self.PLCStatus = "Stopped"
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   239
                self.StatusChange()
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   240
                self.evaluator(self.FinishRuntimePy)
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   241
            else:
350
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   242
                error = "starting"
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   243
        else:
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   244
            error = "loading"
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   245
        if error is not None:
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   246
            PLCprint("Problem %s PLC"%error)
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   247
            self.PLCStatus = "Broken"
690
ef60d7e188e6 Added a semaphore when starting runtime's python thread to make sure startPLC doesn't return before PLC is really initialized.
Edouard Tisserant
parents: 614
diff changeset
   248
            self.StatusChange()
ef60d7e188e6 Added a semaphore when starting runtime's python thread to make sure startPLC doesn't return before PLC is really initialized.
Edouard Tisserant
parents: 614
diff changeset
   249
            self.StartSem.release()
350
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   250
        self._FreePLC()
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   251
    
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   252
    def StartPLC(self):
350
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   253
        PLCprint("StartPLC")
465
67d32a91d70b Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 462
diff changeset
   254
        if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
690
ef60d7e188e6 Added a semaphore when starting runtime's python thread to make sure startPLC doesn't return before PLC is really initialized.
Edouard Tisserant
parents: 614
diff changeset
   255
            self.StartSem=Semaphore(0)
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   256
            self.PythonThread = Thread(target=self.PythonThreadProc)
350
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   257
            self.PythonThread.start()
690
ef60d7e188e6 Added a semaphore when starting runtime's python thread to make sure startPLC doesn't return before PLC is really initialized.
Edouard Tisserant
parents: 614
diff changeset
   258
            self.StartSem.acquire()
352
81777d4e379c fixed bug : Lock _FreePLC until _stopPLC finish
greg
parents: 350
diff changeset
   259
            
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   260
    def StopPLC(self):
350
a3a5561bde1d - now call load, start, free PLC from the python Thread
greg
parents: 344
diff changeset
   261
        PLCprint("StopPLC")
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   262
        if self.PLCStatus == "Started":
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   263
            self.PLCStatus = "Stopped"
352
81777d4e379c fixed bug : Lock _FreePLC until _stopPLC finish
greg
parents: 350
diff changeset
   264
            self._stopPLC()
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   265
            return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   266
        return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   267
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   268
    def _Reload(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   269
        self.daemon.shutdown(True)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   270
        self.daemon.sock.close()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   271
        os.execv(sys.executable,[sys.executable]+sys.argv[:])
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   272
        # never reached
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   273
        return 0
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   274
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   275
    def ForceReload(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   276
        # respawn python interpreter
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   277
        Timer(0.1,self._Reload).start()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   278
        return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   279
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   280
    def GetPLCstatus(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   281
        return self.PLCStatus
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   282
    
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   283
    def NewPLC(self, md5sum, data, extrafiles):
291
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
   284
        PLCprint("NewPLC (%s)"%md5sum)
393
af20e07e53c5 Remove dirtylibs test while freeing plc libs in PLCObject.py
laurent
parents: 368
diff changeset
   285
        if self.PLCStatus in ["Stopped", "Empty", "Broken"]:
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   286
            NewFileName = md5sum + lib_ext
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   287
            extra_files_log = os.path.join(self.workingdir,"extra_files.txt")
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   288
            try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   289
                os.remove(os.path.join(self.workingdir,
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   290
                                       self.CurrentPLCFilename))
364
27ea6a6747fc Bug extra_files deletion in working directory fixed
laurent
parents: 361
diff changeset
   291
                for filename in file(extra_files_log, "r").readlines() + [extra_files_log]:
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   292
                    try:
364
27ea6a6747fc Bug extra_files deletion in working directory fixed
laurent
parents: 361
diff changeset
   293
                        os.remove(os.path.join(self.workingdir, filename.strip()))
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   294
                    except:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   295
                        pass
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   296
            except:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   297
                pass
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   298
                        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   299
            try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   300
                # Create new PLC file
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   301
                open(os.path.join(self.workingdir,NewFileName),
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   302
                     'wb').write(data)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   303
        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   304
                # Store new PLC filename based on md5 key
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   305
                open(self._GetMD5FileName(), "w").write(md5sum)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   306
        
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   307
                # Then write the files
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   308
                log = file(extra_files_log, "w")
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   309
                for fname,fdata in extrafiles:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   310
                    fpath = os.path.join(self.workingdir,fname)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   311
                    open(fpath, "wb").write(fdata)
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   312
                    log.write(fname+'\n')
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   313
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   314
                # Store new PLC filename
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   315
                self.CurrentPLCFilename = NewFileName
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   316
            except:
291
701c0601db02 Added systematic stdout.flush runtime side, so that results appear in log window
etisserant
parents: 290
diff changeset
   317
                PLCprint(traceback.format_exc())
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   318
                return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   319
            if self.PLCStatus == "Empty":
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   320
                self.PLCStatus = "Stopped"
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   321
            return True
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   322
        return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   323
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   324
    def MatchMD5(self, MD5):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   325
        try:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   326
            last_md5 = open(self._GetMD5FileName(), "r").read()
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   327
            return last_md5 == MD5
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   328
        except:
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   329
            return False
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   330
    
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 467
diff changeset
   331
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
   332
    
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   333
    def SetTraceVariablesList(self, idxs):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   334
        """
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   335
        Call ctype imported function to append 
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   336
        these indexes to registred variables in PLC debugger
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   337
        """
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   338
        if idxs:
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   339
            # suspend but dont disable
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   340
            if self._suspendDebug(False) == 0:
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   341
                # keep a copy of requested idx
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   342
                self._Idxs = idxs[:]
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   343
                self._ResetDebugVariables()
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   344
                for idx,iectype,force in idxs:
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   345
                    if force !=None:
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   346
                        c_type,unpack_func, pack_func = \
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   347
                            TypeTranslator.get(iectype,
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   348
                                                    (None,None,None))
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   349
                        force = ctypes.byref(pack_func(c_type,force)) 
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   350
                    self._RegisterDebugVariable(idx, force)
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 593
diff changeset
   351
                self._resumeDebug()
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   352
        else:
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   353
            self._suspendDebug(True)
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 461
diff changeset
   354
            self._Idxs =  []
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 269
diff changeset
   355
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   356
    def GetTraceVariables(self):
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   357
        """
339
6dbde4a0c31d Adding support for updating PLC status when stopping
greg
parents: 336
diff changeset
   358
        Return a list of variables, corresponding to the list of required idx
229
8bc65076e290 add tests for win32
greg
parents: 227
diff changeset
   359
        """
286
a2a8a52b0d4f Minor changes to get better cleanup of debug and python_eval threads, accross multiple debug sessions and PLC runs.
etisserant
parents: 283
diff changeset
   360
        if self.PLCStatus == "Started":
455
e050ef5bd285 Refactoring in PLCobject, for PLC that do not use python plugin
ed
parents: 450
diff changeset
   361
            res=[]
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   362
            tick = ctypes.c_uint32()
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   363
            size = ctypes.c_uint32()
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   364
            buffer = ctypes.c_void_p()
465
67d32a91d70b Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 462
diff changeset
   365
            offset = 0
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   366
            if self.PLClibraryLock.acquire(False) and \
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   367
               self._GetDebugData(ctypes.byref(tick),
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   368
                                  ctypes.byref(size),
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   369
                                  ctypes.byref(buffer)) == 0 :
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   370
                if size.value:
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   371
                    for idx, iectype, forced in self._Idxs:
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   372
                        cursor = ctypes.c_void_p(buffer.value + offset)
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   373
                        c_type,unpack_func, pack_func = \
593
726f58cf97e3 fixed typo in latest debugger changes
edouard
parents: 592
diff changeset
   374
                            TypeTranslator.get(iectype,
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   375
                                                    (None,None,None))
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
   376
                        if c_type is not None and offset < size.value:
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   377
                            res.append(unpack_func(
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   378
                                        ctypes.cast(cursor,
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   379
                                         ctypes.POINTER(c_type)).contents))
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   380
                            offset += ctypes.sizeof(c_type)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   381
                        else:
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   382
                            if c_type is None:
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   383
                                PLCprint("Debug error - " + iectype +
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 483
diff changeset
   384
                                         " not supported !")
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
   385
                            #if offset >= size.value:
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
   386
                                #PLCprint("Debug error - buffer too small ! %d != %d"%(offset, size.value))
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   387
                            break
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   388
                self._FreeDebugData()
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 479
diff changeset
   389
                self.PLClibraryLock.release()
465
67d32a91d70b Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 462
diff changeset
   390
            if offset and offset == size.value:
460
73a53278833b Safer debug unpack result checking, more verbose error message, slower retry when waiting PLC startup
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 459
diff changeset
   391
                return self.PLCStatus, tick.value, res
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
   392
            #elif size.value:
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
   393
                #PLCprint("Debug error - wrong buffer unpack ! %d != %d"%(offset, size.value))
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
   394
        return self.PLCStatus, None, []
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 504
diff changeset
   395
699
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   396
    def RemoteExec(self, script, **kwargs):
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   397
        try:
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   398
            exec script in kwargs
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   399
        except:
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   400
            e_type, e_value, e_traceback = sys.exc_info()
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   401
            line_no = traceback.tb_lineno(get_last_traceback(e_traceback))
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   402
            return (-1, "RemoteExec script failed!\n\nLine %d: %s\n\t%s" % 
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   403
                        (line_no, e_value, script.splitlines()[line_no - 1]))
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   404
        return (0, kwargs.get("returnVal", None))
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   405
    
6ff64cadb1ff Adding support for executing python scripts on remote runtime
laurent
parents: 690
diff changeset
   406