LPCconnector/LPCAppObject.py
author Edouard Tisserant
Sun, 17 Jun 2012 12:56:54 +0200
changeset 13 2e3c3da3593e
parent 0 51f5a3138405
child 29 86fb7dc2b54e
permissions -rw-r--r--
remerge
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 *
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    28
from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator
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
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    34
                                         2)      #timeout
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):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    48
        self.HandleSerialTransaction(GET_PLCIDTransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    49
        return self.PLCStatus
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    50
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    51
    def MatchMD5(self, MD5):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    52
        data = self.HandleSerialTransaction(GET_PLCIDTransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    53
        if data is not None:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    54
            return data[:32] == MD5[:32]
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    55
        return False
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 SetTraceVariablesList(self, idxs):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    58
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    59
        Call ctype imported function to append 
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    60
        these indexes to registred variables in PLC debugger
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    61
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    62
        if idxs:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    63
            buff = ""
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    64
            # keep a copy of requested idx
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    65
            self._Idxs = idxs[:]
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    66
            for idx,iectype,force in idxs:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    67
                idxstr = ctypes.string_at(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    68
                          ctypes.pointer(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    69
                           ctypes.c_uint32(idx)),4)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    70
                if force !=None:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    71
                    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
    72
                    forced_type_size = ctypes.sizeof(c_type)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    73
                    forced_type_size_str = chr(forced_type_size)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    74
                    forcestr = ctypes.string_at(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    75
                                ctypes.pointer(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    76
                                 pack_func(c_type,force)),
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    77
                                 forced_type_size)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    78
                    buff += idxstr + forced_type_size_str + forcestr
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    79
                else:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    80
                    buff += idxstr + chr(0)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    81
        else:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    82
            buff = ""
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    83
            self._Idxs =  []
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    84
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    85
        self.HandleSerialTransaction(SET_TRACE_VARIABLETransaction(buff))
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    86
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    87
    def GetTraceVariables(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    88
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    89
        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
    90
        """
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    91
        offset = 0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    92
        strbuf = self.HandleSerialTransaction(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    93
                                     GET_TRACE_VARIABLETransaction())
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    94
        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
    95
            res=[]
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    96
            size = len(strbuf) - 4
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    97
            tick = ctypes.cast(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    98
                    ctypes.c_char_p(strbuf[:4]),
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    99
                    ctypes.POINTER(ctypes.c_int)).contents
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   100
            buff = ctypes.cast(
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   101
                      ctypes.c_char_p(strbuf[4:]),
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   102
                      ctypes.c_void_p)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   103
            for idx, iectype, forced in self._Idxs:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   104
                cursor = ctypes.c_void_p(buff.value + offset)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   105
                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
   106
                if c_type is not None and offset < size:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   107
                    res.append(unpack_func(ctypes.cast(cursor,
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   108
                                                       ctypes.POINTER(c_type)).contents))
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   109
                    offset += ctypes.sizeof(c_type)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   110
                else:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   111
                    #if c_type is None:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   112
                        #PLCprint("Debug error - " + iectype + " not supported !")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   113
                    #if offset >= size:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   114
                        #PLCprint("Debug error - buffer too small !")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   115
                    break
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   116
            if offset and offset == size:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   117
                return self.PLCStatus, tick.value, res
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   118
            #PLCprint("Debug error - wrong buffer unpack !")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   119
        return self.PLCStatus, None, [] 
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
   120