etherlab/EthercatSlave.py
author Laurent Bessard
Mon, 29 Apr 2013 11:03:06 +0200
changeset 2139 1565239349a8
parent 2131 504f18b3306a
child 2152 e6946c298a42
permissions -rw-r--r--
Added support for module extra params in EthercatCFileGenerator
2111
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     1
import wx
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     2
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     3
from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     4
from ConfigTreeNode import ConfigTreeNode
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     5
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     6
from ConfigEditor import NodeEditor
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     7
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     8
TYPECONVERSION = {"BOOL" : "X", "SINT" : "B", "INT" : "W", "DINT" : "D", "LINT" : "L",
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
     9
    "USINT" : "B", "UINT" : "W", "UDINT" : "D", "ULINT" : "L", 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    10
    "BYTE" : "B", "WORD" : "W", "DWORD" : "D", "LWORD" : "L"}
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    11
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    12
DATATYPECONVERSION = {"BOOL" : "BIT", "SINT" : "S8", "INT" : "S16", "DINT" : "S32", "LINT" : "S64",
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    13
    "USINT" : "U8", "UINT" : "U16", "UDINT" : "U32", "ULINT" : "U64", 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    14
    "BYTE" : "U8", "WORD" : "U16", "DWORD" : "U32", "LWORD" : "U64"}
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    15
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    16
VARCLASSCONVERSION = {"T": LOCATION_VAR_INPUT, "R": LOCATION_VAR_OUTPUT, "RT": LOCATION_VAR_MEMORY}
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    17
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    18
def ExtractHexDecValue(value):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    19
    try:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    20
        return int(value)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    21
    except:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    22
        pass
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    23
    try:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    24
        return int(value.replace("#", "0"), 16)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    25
    except:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    26
        raise ValueError, "Invalid value for HexDecValue \"%s\"" % value
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    27
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    28
def GenerateHexDecValue(value, base=10):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    29
    if base == 10:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    30
        return str(value)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    31
    elif base == 16:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    32
        return "#x%.8x" % value
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    33
    else:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    34
        raise ValueError, "Not supported base"
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    35
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    36
def ExtractName(names, default=None):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    37
    if len(names) == 1:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    38
        return names[0].getcontent()
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    39
    else:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    40
        for name in names:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    41
            if name.getLcId() == 1033:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    42
                return name.getcontent()
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    43
    return default
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    44
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    45
#--------------------------------------------------
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    46
#                    Ethercat Node
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    47
#--------------------------------------------------
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    48
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    49
class _EthercatSlaveCTN:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    50
    
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    51
    NODE_PROFILE = None
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    52
    EditorType = NodeEditor
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    53
    
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    54
    def GetIconName(self):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    55
        return "Slave"
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    56
    
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    57
    def ExtractHexDecValue(self, value):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    58
        return ExtractHexDecValue(value)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    59
    
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    60
    def GetSizeOfType(self, type):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    61
        return TYPECONVERSION.get(self.GetCTRoot().GetBaseType(type), None)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    62
    
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    63
    def GetSlavePos(self):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    64
        return self.BaseParams.getIEC_Channel()
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    65
    
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    66
    def GetParamsAttributes(self, path = None):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    67
        if path:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    68
            parts = path.split(".", 1)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    69
            if self.MandatoryParams and parts[0] == self.MandatoryParams[0]:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    70
                return self.MandatoryParams[1].getElementInfos(parts[0], parts[1])
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    71
            elif self.CTNParams and parts[0] == self.CTNParams[0]:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    72
                return self.CTNParams[1].getElementInfos(parts[0], parts[1])
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    73
        else:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    74
            params = []
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    75
            if self.CTNParams:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    76
                params.append(self.CTNParams[1].getElementInfos(self.CTNParams[0]))
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    77
            else:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    78
                params.append({
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    79
                    'use': 'required', 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    80
                    'type': 'element', 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    81
                    'name': 'SlaveParams', 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    82
                    'value': None, 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    83
                    'children': []
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    84
                })
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    85
            
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    86
            slave_type = self.CTNParent.GetSlaveType(self.GetSlavePos())
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    87
            params[0]['children'].insert(0,
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    88
                   {'use': 'optional', 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    89
                    'type': self.CTNParent.GetSlaveTypesLibrary(self.NODE_PROFILE), 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    90
                    'name': 'Type', 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    91
                    'value': (slave_type["device_type"], slave_type)}) 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    92
            params[0]['children'].insert(1,
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    93
                   {'use': 'optional', 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    94
                    'type': 'unsignedLong', 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    95
                    'name': 'Alias', 
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    96
                    'value': self.CTNParent.GetSlaveAlias(self.GetSlavePos())})
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    97
            return params
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    98
        
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
    99
    def SetParamsAttribute(self, path, value):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   100
        position = self.BaseParams.getIEC_Channel()
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   101
        
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   102
        if path == "SlaveParams.Type":
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   103
            self.CTNParent.SetSlaveType(position, value)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   104
            slave_type = self.CTNParent.GetSlaveType(self.GetSlavePos())
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   105
            value = (slave_type["device_type"], slave_type)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   106
            return value, True
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   107
        elif path == "SlaveParams.Alias":
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   108
            self.CTNParent.SetSlaveAlias(position, value)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   109
            return value, True
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   110
        
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   111
        value, refresh = ConfigTreeNode.SetParamsAttribute(self, path, value)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   112
        
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   113
        # Filter IEC_Channel, Slave_Type and Alias that have specific behavior
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   114
        if path == "BaseParams.IEC_Channel" and value != position:
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   115
            self.CTNParent.SetSlavePosition(position, value)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   116
        
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   117
        return value, refresh
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   118
        
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   119
    def GetSlaveInfos(self):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   120
        return self.CTNParent.GetSlaveInfos(self.GetSlavePos())
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   121
    
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   122
    def GetSlaveVariables(self, limits):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   123
        return self.CTNParent.GetSlaveVariables(self.GetSlavePos(), limits)
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   124
    
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   125
    def GetVariableLocationTree(self):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   126
        return  {"name": self.BaseParams.getName(),
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   127
                 "type": LOCATION_CONFNODE,
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   128
                 "location": self.GetFullIEC_Channel(),
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   129
                 "children": self.CTNParent.GetDeviceLocationTree(self.GetSlavePos(), self.GetCurrentLocation(), self.BaseParams.getName())
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   130
        }
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   131
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   132
    def CTNGenerate_C(self, buildpath, locations):
f2cffda17d00 Split etherlab.py into multiple files
Laurent Bessard
parents:
diff changeset
   133
        return [],"",False