LPCconnector/LPCObject.py
author Laurent Bessard
Thu, 03 Oct 2013 22:59:38 +0200
changeset 57 4b838bdedc16
parent 45 786b12887e91
permissions -rwxr-xr-x
Fixed bug when starting simulation. In some case PLC status was asked before local runtime was connected.
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
from LPCProto import *
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    26
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    27
class LPCObject():
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    28
    def __init__(self, confnodesroot, comportstr):
45
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    29
        self.TransactionLock = Lock()
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    30
        self.PLCStatus = "Disconnected"
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    31
        self.confnodesroot = confnodesroot
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    32
        self.PLCprint = confnodesroot.logger.writeyield
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    33
        self._Idxs = []
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    34
        try:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    35
            self.connect(comportstr)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    36
        except Exception,e:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    37
            self.confnodesroot.logger.write_error(str(e)+"\n")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    38
            self.SerialConnection = None
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    39
            self.PLCStatus = "Disconnected"
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    40
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    41
    def HandleSerialTransaction(self, transaction):
45
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    42
        res = None
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    43
        disconnected=False
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    44
        failure=None
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    45
        self.TransactionLock.acquire()
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    46
        if self.SerialConnection is not None:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    47
            try:
45
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    48
                self.PLCStatus, res = \
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    49
                    self.SerialConnection.HandleTransaction(transaction)
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    50
            except LPCProtoError,e:
45
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    51
                disconnected=True
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    52
                if self.SerialConnection is not None:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    53
                    self.SerialConnection.close()
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    54
                    self.SerialConnection = None
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    55
                self.PLCStatus = "Disconnected"
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    56
            except Exception,e:
45
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    57
                failure = str(e)
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    58
        self.TransactionLock.release()
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    59
        if disconnected:
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    60
            self.confnodesroot.logger.write(_("PLC disconnected\n"))
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    61
        if failure is not None:
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    62
            self.confnodesroot.logger.write_warning(failure+"\n")
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    63
        return res
0
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
    def StartPLC(self, debug=False):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    66
        raise LPCProtoError("Not implemented")
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
    def StopPLC(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    69
        raise LPCProtoError("Not implemented")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    70
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    71
    def GetPLCstatus(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    72
        raise LPCProtoError("Not implemented")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    73
    
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    74
    def NewPLC(self, md5sum, data, extrafiles):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    75
        raise LPCProtoError("Not implemented")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    76
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    77
    def MatchMD5(self, MD5):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    78
        raise LPCProtoError("Not implemented")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    79
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    80
    def SetTraceVariablesList(self, idxs):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    81
        raise LPCProtoError("Not implemented")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    82
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    83
    def GetTraceVariables(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    84
        raise LPCProtoError("Not implemented")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    85