LPCconnector/LPCAppObject.py
author Edouard Tisserant
Mon, 29 Apr 2013 17:31:24 +0900
changeset 45 786b12887e91
parent 44 a6577420a27b
child 46 c6b7d4c176c2
permissions -rw-r--r--
Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
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))
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    78
                    forced_type_size = ctypes.sizeof(c_type)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    79
                    forced_type_size_str = chr(forced_type_size)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    80
                    forcestr = ctypes.string_at(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    81
                                ctypes.pointer(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    82
                                 pack_func(c_type,force)),
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    83
                                 forced_type_size)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    84
                    buff += idxstr + forced_type_size_str + forcestr
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    85
                else:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    86
                    buff += idxstr + chr(0)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    87
        else:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    88
            buff = ""
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    89
            self._Idxs =  []
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    90
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    91
        self.HandleSerialTransaction(SET_TRACE_VARIABLETransaction(buff))
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    92
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    93
    def GetTraceVariables(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    94
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    95
        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
    96
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    97
        strbuf = self.HandleSerialTransaction(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    98
                                     GET_TRACE_VARIABLETransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    99
        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
   100
            size = len(strbuf) - 4
31
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
   101
            ctick = ctypes.create_string_buffer(strbuf[:4])
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
   102
            tick = ctypes.cast(ctick, ctypes.POINTER(ctypes.c_int)).contents
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
   103
            cbuff = ctypes.create_string_buffer(strbuf[4:])
a9c9d1fc97d3 Made better ctypes unpacking of PLC data
Edouard Tisserant
parents: 30
diff changeset
   104
            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
   105
            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
   106
            if TraceVariables is not None:
42
30eae94f35f7 Fixed typo
Edouard Tisserant
parents: 41
diff changeset
   107
                return self.PLCStatus, tick.value, TraceVariables
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   108
        return self.PLCStatus, None, [] 
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   109
29
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   110
    def GetLogMessage(self, level, msgid):
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   111
        strbuf = self.HandleSerialTransaction(GET_LOGMSGTransaction(level, msgid))
35
cb9da964a178 Fixed bugs with pulling plc status and log count
Laurent Bessard
parents: 34
diff changeset
   112
        if strbuf is not None and len(strbuf) > 12:
29
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   113
            cbuf = ctypes.cast(
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   114
                          ctypes.c_char_p(strbuf[:12]),
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   115
                          ctypes.POINTER(ctypes.c_uint32))
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   116
            return (strbuf[12:],)+tuple(int(cbuf[idx]) for idx in range(3))
86fb7dc2b54e Untested support for logging
Drauoude Tnaressit
parents: 0
diff changeset
   117
        return None