LPCconnector/LPCAppObject.py
author Laurent Bessard
Wed, 03 Jul 2013 11:28:12 +0200
changeset 52 7fc0928030b6
parent 49 f340728940f6
permissions -rw-r--r--
Fixed bug when opening project with path containing non-ascii characters
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     3
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     6
#
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     8
#
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    10
#
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    15
#
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    19
#General Public License for more details.
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    20
#
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    24
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    25
import ctypes
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    26
from LPCAppProto import *
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    27
from LPCObject import *
41
b02a30880829 Now Debug Buffer Unpacking happens in beremiz/typemapping.py, and not in copy-pasted-unsychronized code
Edouard Tisserant
parents: 35
diff changeset
    28
from targets.typemapping import  LogLevelsCount, TypeTranslator, UnpackDebugBuffer
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    29
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    30
class LPCAppObject(LPCObject):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    31
    def connect(self,comport):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    32
        self.SerialConnection = LPCAppProto(comport,#number
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    33
                                         115200, #speed
44
a6577420a27b Longer Timeout since restart after download seems to take longer now.
Edouard Tisserant
parents: 42
diff changeset
    34
                                         5)      #timeout
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    35
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    36
    def StartPLC(self, debug=False):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    37
        self.HandleSerialTransaction(STARTTransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    38
            
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    39
    def StopPLC(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    40
        self.HandleSerialTransaction(STOPTransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    41
        return True
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    42
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    43
    def ResetPLC(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    44
        self.HandleSerialTransaction(RESETTransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    45
        return self.PLCStatus
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    46
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    47
    def GetPLCstatus(self):
29
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
    48
        strcounts = self.HandleSerialTransaction(GET_LOGCOUNTSTransaction())
34
b2fb188c9bff Fixed bug when GET_LOGCOUNTSTransaction return None
Laurent Bessard
parents: 31
diff changeset
    49
        if strcounts is not None and len(strcounts) == LogLevelsCount * 4:
31
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
    50
            cstrcounts = ctypes.create_string_buffer(strcounts)
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
    51
            ccounts = ctypes.cast(cstrcounts, ctypes.POINTER(ctypes.c_uint32))
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
    52
            counts = [int(ccounts[idx]) for idx in xrange(LogLevelsCount)]
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
    53
        else :
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
    54
            counts = [0]*LogLevelsCount
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
    55
        return self.PLCStatus, counts 
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    56
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    57
    def MatchMD5(self, MD5):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    58
        data = self.HandleSerialTransaction(GET_PLCIDTransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    59
        if data is not None:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    60
            return data[:32] == MD5[:32]
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    61
        return False
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    62
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    63
    def SetTraceVariablesList(self, idxs):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    64
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    65
        Call ctype imported function to append 
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    66
        these indexes to registred variables in PLC debugger
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    67
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    68
        if idxs:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    69
            buff = ""
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    70
            # keep a copy of requested idx
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    71
            self._Idxs = idxs[:]
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    72
            for idx,iectype,force in idxs:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    73
                idxstr = ctypes.string_at(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    74
                          ctypes.pointer(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    75
                           ctypes.c_uint32(idx)),4)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    76
                if force !=None:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    77
                    c_type,unpack_func, pack_func = TypeTranslator.get(iectype, (None,None,None))
46
c6b7d4c176c2 Now forced STRING also optimized in transmitted buffer
Edouard Tisserant
parents: 44
diff changeset
    78
                    forced_type_size = ctypes.sizeof(c_type) \
49
f340728940f6 Fixed bug when forcing STRING variables
Laurent Bessard
parents: 47
diff changeset
    79
                        if iectype != "STRING" else len(force)+1
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    80
                    forced_type_size_str = chr(forced_type_size)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    81
                    forcestr = ctypes.string_at(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    82
                                ctypes.pointer(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    83
                                 pack_func(c_type,force)),
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    84
                                 forced_type_size)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    85
                    buff += idxstr + forced_type_size_str + forcestr
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    86
                else:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    87
                    buff += idxstr + chr(0)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    88
        else:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    89
            buff = ""
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    90
            self._Idxs =  []
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    91
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    92
        self.HandleSerialTransaction(SET_TRACE_VARIABLETransaction(buff))
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    93
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    94
    def GetTraceVariables(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    95
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    96
        Return a list of variables, corresponding to the list of required idx
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    97
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    98
        strbuf = self.HandleSerialTransaction(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    99
                                     GET_TRACE_VARIABLETransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   100
        if strbuf is not None and len(strbuf) > 4 and self.PLCStatus == "Started":
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   101
            size = len(strbuf) - 4
31
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
   102
            ctick = ctypes.create_string_buffer(strbuf[:4])
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
   103
            tick = ctypes.cast(ctick, ctypes.POINTER(ctypes.c_int)).contents
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
   104
            cbuff = ctypes.create_string_buffer(strbuf[4:])
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
   105
            buff = ctypes.cast(cbuff, ctypes.c_void_p)
41
b02a30880829 Now Debug Buffer Unpacking happens in beremiz/typemapping.py, and not in copy-pasted-unsychronized code
Edouard Tisserant
parents: 35
diff changeset
   106
            TraceVariables = UnpackDebugBuffer(buff, size,  self._Idxs)
b02a30880829 Now Debug Buffer Unpacking happens in beremiz/typemapping.py, and not in copy-pasted-unsychronized code
Edouard Tisserant
parents: 35
diff changeset
   107
            if TraceVariables is not None:
42
30eae94f35f7 Fixed typo
Edouard Tisserant
parents: 41
diff changeset
   108
                return self.PLCStatus, tick.value, TraceVariables
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   109
        return self.PLCStatus, None, [] 
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   110
47
f860aa7e90e5 Added support for resetting log messages
Laurent Bessard
parents: 46
diff changeset
   111
    def ResetLogCount(self):
f860aa7e90e5 Added support for resetting log messages
Laurent Bessard
parents: 46
diff changeset
   112
        self.HandleSerialTransaction(RESET_LOGCOUNTSTransaction())
f860aa7e90e5 Added support for resetting log messages
Laurent Bessard
parents: 46
diff changeset
   113
29
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   114
    def GetLogMessage(self, level, msgid):
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   115
        strbuf = self.HandleSerialTransaction(GET_LOGMSGTransaction(level, msgid))
35
cb9da964a178 Fixed bugs with pulling plc status and log count
Laurent Bessard
parents: 34
diff changeset
   116
        if strbuf is not None and len(strbuf) > 12:
29
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   117
            cbuf = ctypes.cast(
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   118
                          ctypes.c_char_p(strbuf[:12]),
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   119
                          ctypes.POINTER(ctypes.c_uint32))
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   120
            return (strbuf[12:],)+tuple(int(cbuf[idx]) for idx in range(3))
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   121
        return None