connectors/LPC/LPCObject.py
author ed
Sun, 06 Dec 2009 15:58:33 +0100
changeset 457 e01434cfcb9d
parent 453 923d036dfa90
child 502 5343ae43f6d0
permissions -rwxr-xr-x
Fixes
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     1
#!/usr/bin/env python
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     3
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     6
#
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     8
#
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     9
#See COPYING file for copyrights details.
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    10
#
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    15
#
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    19
#General Public License for more details.
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    20
#
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    24
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    25
from threading import Timer, Thread, Lock
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    26
import ctypes, os, commands, types, sys
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    27
import traceback
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    28
from LPCProto import *
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    29
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    30
class LPCObject():
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    31
    def __init__(self,pluginsroot):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    32
        self.PLCStatus = "Stopped"
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    33
        self.pluginsroot = pluginsroot
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    34
        self.PLCprint = pluginsroot.logger.write
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    35
        self.SerialConnection = None
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    36
        self._Idxs = []
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    37
        
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    38
    def HandleSerialTransaction(self, transaction):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    39
        if self.SerialConnection is None:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    40
            try:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    41
                self.SerialConnection = LPCProto(6,115200,2)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    42
            except Exception,e:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    43
                self.pluginsroot.logger.write_error(str(e)+"\n")
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    44
                self.SerialConnection = None
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    45
                return "Disconnected", res
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    46
        try:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    47
            return self.SerialConnection.HandleTransaction(transaction)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    48
        except LPCError,e:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    49
            #pluginsroot.logger.write_error(traceback.format_exc())
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    50
            self.pluginsroot.logger.write_error(str(e)+"\n")
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    51
            self.SerialConnection = None
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    52
            return "Disconnected", res
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    53
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    54
    def StartPLC(self, debug=False):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    55
        PLCprint("StartPLC")
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    56
        self.HandleSerialTransaction(STARTTransaction())
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    57
            
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    58
    def StopPLC(self):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    59
        PLCprint("StopPLC")
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    60
        self.HandleSerialTransaction(STOPTransaction())
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    61
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    62
    def ForceReload(self):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    63
        pass
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    64
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    65
    def GetPLCstatus(self):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    66
        status,data = self.HandleSerialTransaction(IDLETransaction())
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    67
        return status
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    68
    
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    69
    def NewPLC(self, md5sum, data, extrafiles):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    70
        pass
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    71
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    72
    def MatchMD5(self, MD5):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    73
        status,data = self.HandleSerialTransaction(PLCIDTransaction())
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    74
        return data == MD5
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    75
    
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    76
    def SetTraceVariablesList(self, idxs):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    77
        self._Idxs = idxs[]
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    78
        status,data = self.HandleSerialTransaction(
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    79
               SET_TRACE_VARIABLETransaction(
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    80
                     ''.join(map(chr,idx))))
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    81
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    82
    class IEC_STRING(ctypes.Structure):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    83
        """
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    84
        Must be changed according to changes in iec_types.h
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    85
        """
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    86
        _fields_ = [("len", ctypes.c_uint8),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    87
                    ("body", ctypes.c_char * 127)] 
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    88
    
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    89
    TypeTranslator = {"BOOL" :       (ctypes.c_uint8, lambda x:x.value!=0),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    90
                      "STEP" :       (ctypes.c_uint8, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    91
                      "TRANSITION" : (ctypes.c_uint8, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    92
                      "ACTION" :     (ctypes.c_uint8, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    93
                      "SINT" :       (ctypes.c_int8, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    94
                      "USINT" :      (ctypes.c_uint8, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    95
                      "BYTE" :       (ctypes.c_uint8, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    96
                      "STRING" :     (IEC_STRING, lambda x:x.body[:x.len]),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    97
                      "INT" :        (ctypes.c_int16, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    98
                      "UINT" :       (ctypes.c_uint16, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    99
                      "WORD" :       (ctypes.c_uint16, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   100
                      "WSTRING" :    (None, None),#TODO
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   101
                      "DINT" :       (ctypes.c_int32, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   102
                      "UDINT" :      (ctypes.c_uint32, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   103
                      "DWORD" :      (ctypes.c_uint32, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   104
                      "LINT" :       (ctypes.c_int64, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   105
                      "ULINT" :      (ctypes.c_uint64, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   106
                      "LWORD" :      (ctypes.c_uint64, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   107
                      "REAL" :       (ctypes.c_float, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   108
                      "LREAL" :      (ctypes.c_double, lambda x:x.value),
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   109
                      } 
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   110
                           
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   111
    def GetTraceVariables(self):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   112
        """
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   113
        Return a list of variables, corresponding to the list of required idx
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   114
        """
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   115
        status,data = self.HandleSerialTransaction(GET_TRACE_VARIABLETransaction())
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   116
        if data is not None:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   117
            # transform serial string to real byte string in memory 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   118
            buffer = ctypes.c_char_p(data)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   119
            # tick is first value in buffer
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   120
            tick = ctypes.cast(buffer,ctypes.POINTER(ctypes.c_uint32)).contents
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   121
            # variable data starts just after tick 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   122
            cursorp = ctypes.addressof(buffer) = ctypes.sizeof(ctypes.c_uint32)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   123
            endp = offset + len(data)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   124
            for idx, iectype in self._Idxs:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   125
                cursor = ctypes.c_void_p(cursorp)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   126
                c_type,unpack_func = self.TypeTranslator.get(iectype, (None,None))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   127
                if c_type is not None and cursorp < endp:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   128
                    res.append(unpack_func(ctypes.cast(cursor,
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   129
                                                       ctypes.POINTER(c_type)).contents))
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   130
                    cursorp += ctypes.sizeof(c_type) 
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   131
                else:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   132
                    PLCprint("Debug error !")
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   133
                        break
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   134
            return self.PLCStatus, tick, res
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
   135
        return self.PLCStatus, None, None
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
   136