etherlab/etherlab.py
author laurent
Wed, 15 Feb 2012 00:38:26 +0100
changeset 2032 766078d83e22
parent 2031 c6f32810723e
child 2034 ae8fecf082a1
permissions -rw-r--r--
Adding support for DS402 node subplugin (only simple state transition implemented)
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
     1
import os, shutil
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
     2
import cPickle
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
     3
from xml.dom import minidom
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
     4
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
     5
import wx
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
     6
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
     7
from xmlclass import *
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
     8
from plugger import PlugTemplate
2023
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
     9
from PLCControler import UndoBuffer, LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    10
from ConfigEditor import ConfigEditor, ETHERCAT_VENDOR, ETHERCAT_GROUP, ETHERCAT_DEVICE
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    11
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    12
#--------------------------------------------------
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    13
#                 Ethercat DS402 Node
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    14
#--------------------------------------------------
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    15
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    16
NODE_VARIABLES = [
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    17
    ("ControlWord", 0x6040, 0x00, "UINT", "Q"),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    18
    ("StatusWord", 0x6041, 0x00, "UINT", "I"),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    19
    ("ModesOfOperationDisplay", 0x06061, 0x00, "SINT", "I"),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    20
    ("ErrorCode", 0x603f, 0x00, "UINT", "I"),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    21
]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    22
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    23
class _EthercatDS402SlavePlug:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    24
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    25
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    26
      <xsd:element name="EtherlabDS402Slave">
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    27
        <xsd:complexType>
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    28
          <xsd:attribute name="Node_Type" type="xsd:string" use="optional"/>
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    29
        </xsd:complexType>
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    30
      </xsd:element>
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    31
    </xsd:schema>
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    32
    """
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    33
    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    34
    def _GetChildBySomething(self, something, toks):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    35
        return self
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    36
    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    37
    def GetParamsAttributes(self, path = None):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    38
        infos = PlugTemplate.GetParamsAttributes(self, path = None)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    39
        for element in infos:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    40
            if element["name"] == "EtherlabDS402Slave":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    41
                for child in element["children"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    42
                    if child["name"] == "Node_Type":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    43
                        child["type"] = [module[0] for module in self.PlugParent.GetModulesByProfile(402)]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    44
        return infos
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    45
    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    46
    def GetAllChannels(self):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    47
        AllChannels = PlugTemplate.GetAllChannels(self)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    48
        for slave_pos in self.PlugParent.GetSlaves():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    49
            if slave_pos[0] not in AllChannels:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    50
                AllChannels.append(slave_pos[0])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    51
        AllChannels.sort()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    52
        return AllChannels
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    53
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    54
    def GetCurrentLocation(self):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    55
        """
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    56
        @return:  Tupple containing plugin IEC location of current plugin : %I0.0.4.5 => (0,0,4,5)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    57
        """
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    58
        return self.PlugParent.GetCurrentLocation() + (self.BaseParams.getIEC_Channel(), 0)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    59
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    60
    def GetSlaveInfos(self):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    61
        slave_type = self.EtherlabDS402Slave.getNode_Type()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    62
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    63
        for module_type, vendor_id, product_code, revision_number in self.PlugParent.GetModulesByProfile(402):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    64
            if module_type == slave_type:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    65
                return {"device_type": module_type,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    66
                        "vendor": GenerateHexDecValue(vendor_id),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    67
                        "product_code": GenerateHexDecValue(product_code, 16),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    68
                        "revision_number": GenerateHexDecValue(revision_number, 16)}
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    69
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    70
        return None
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    71
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    72
    def GetVariableLocationTree(self):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    73
        slave_infos = self.GetSlaveInfos()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    74
        vars = []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    75
        if slave_infos is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    76
            vars = self.PlugParent.GetDeviceLocationTree(self.GetCurrentLocation(), slave_infos) 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    77
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    78
        return  {"name": self.BaseParams.getName(),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    79
                 "type": LOCATION_PLUGIN,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    80
                 "location": self.GetFullIEC_Channel(),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    81
                 "children": vars}
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    82
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    83
    def PlugGenerate_C(self, buildpath, locations):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    84
        """
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    85
        Generate C code
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    86
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    87
        @param locations: List of complete variables locations \
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    88
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    89
            "NAME" : name of the variable (generally "__IW0_1_2" style)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    90
            "DIR" : direction "Q","I" or "M"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    91
            "SIZE" : size "X", "B", "W", "D", "L"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    92
            "LOC" : tuple of interger for IEC location (0,1,2,...)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    93
            }, ...]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    94
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    95
        """
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    96
        current_location = self.GetCurrentLocation()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    97
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    98
        location_str = "_".join(map(lambda x:str(x), current_location))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
    99
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   100
        slave_pos = current_location[-2:]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   101
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   102
        slave_infos = self.GetSlaveInfos()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   103
        device = None
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   104
        if slave_infos is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   105
            device = self.PlugParent.GetModuleInfos(slave_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   106
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   107
        if device is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   108
            raise (ValueError, 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   109
                   _("No information found for DS402 node \"%s\" at location %s!") % (
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   110
                      slave_infos["device_type"], ".".join(current_location)))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   111
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   112
        slave_idx = self.PlugParent.FileGenerator.DeclareSlave(slave_pos, slave_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   113
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   114
        plc_ds402node_filepath = os.path.join(os.path.split(__file__)[0], "plc_ds402node.c")
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   115
        plc_ds402node_file = open(plc_ds402node_filepath, 'r')
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   116
        plc_ds402node_code = plc_ds402node_file.read()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   117
        plc_ds402node_file.close()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   118
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   119
        str_completion = {
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   120
            "location": location_str,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   121
            "MCL_includes": "",
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   122
            "located_variables_declaration": [],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   123
            "entry_variables": [],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   124
            "extern_pdo_entry_configuration": [],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   125
            "retrieve_variables": [],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   126
            "publish_variables": [],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   127
        }
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   128
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   129
        variables = {}
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   130
        for variable in NODE_VARIABLES:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   131
            var_infos = dict(zip(["name", "index", "subindex", "var_type", "dir"], variable))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   132
            var_infos["location"] = location_str
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   133
            var_infos["slave"] = slave_idx
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   134
            var_infos["var_size"] = self.PlugParent.GetSizeOfType(var_infos["var_type"])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   135
            var_infos["var_name"] = "__%(dir)s%(var_size)s%(location)s_%(index)d_%(subindex)d" % var_infos
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   136
            var_infos["real_var"] = "__DS402Node_%(location)s.%(name)s" % var_infos
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   137
            var_infos.update(slave_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   138
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   139
            variables[(var_infos["index"], var_infos["subindex"])] = var_infos["name"]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   140
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   141
            str_completion["entry_variables"].append("    IEC_%(var_type)s %(name)s;" % var_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   142
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   143
            ConfigureVariable(var_infos, str_completion)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   144
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   145
            str_completion["extern_pdo_entry_configuration"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   146
                "extern unsigned int slave%(slave)d_%(index).4x_%(subindex).2x;" % var_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   147
                
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   148
            if var_infos["var_type"] == "BOOL":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   149
                str_completion["extern_pdo_entry_configuration"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   150
                    "extern unsigned int slave%(slave)d_%(index).4x_%(subindex).2x_bit;" % var_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   151
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   152
            self.PlugParent.FileGenerator.DeclareVariable(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   153
                    slave_pos, var_infos["index"], var_infos["subindex"], 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   154
                    var_infos["var_type"], var_infos["dir"], var_infos["var_name"], False)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   155
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   156
        for element in ["located_variables_declaration", 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   157
                        "entry_variables", 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   158
                        "extern_pdo_entry_configuration", 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   159
                        "retrieve_variables", 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   160
                        "publish_variables"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   161
            str_completion[element] = "\n".join(str_completion[element])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   162
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   163
        Gen_DS402Nodefile_path = os.path.join(buildpath, "ds402node_%s.c"%location_str)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   164
        ds402nodefile = open(Gen_DS402Nodefile_path, 'w')
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   165
        ds402nodefile.write(plc_ds402node_code % str_completion)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   166
        ds402nodefile.close()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   167
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   168
        for location in locations:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   169
            loc = location["LOC"][len(current_location):]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   170
            if variables.get(loc, None) is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   171
                self.PlugParent.FileGenerator.DeclareVariable(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   172
                    slave_pos, loc[0], loc[1], location["IEC_TYPE"], location["DIR"], location["NAME"])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   173
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   174
        return [(Gen_DS402Nodefile_path, '"-I%s"'%os.path.abspath(self.GetPlugRoot().GetIECLibPath()))],"",True
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   175
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   176
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   177
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   178
TYPECONVERSION = {"BOOL" : "X", "SINT" : "B", "INT" : "W", "DINT" : "D", "LINT" : "L",
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   179
    "USINT" : "B", "UINT" : "W", "UDINT" : "D", "ULINT" : "L", 
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   180
    "BYTE" : "B", "WORD" : "W", "DWORD" : "D", "LWORD" : "L"}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   181
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   182
DATATYPECONVERSION = {"BOOL" : "BIT", "SINT" : "S8", "INT" : "S16", "DINT" : "S32", "LINT" : "S64",
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   183
    "USINT" : "U8", "UINT" : "U16", "UDINT" : "U32", "ULINT" : "U64", 
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   184
    "BYTE" : "U8", "WORD" : "U16", "DWORD" : "U32", "LWORD" : "U64"}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   185
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   186
VARCLASSCONVERSION = {"T": LOCATION_VAR_INPUT, "R": LOCATION_VAR_OUTPUT, "RT": LOCATION_VAR_MEMORY}
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   187
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   188
#--------------------------------------------------
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   189
#                 Ethercat MASTER
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   190
#--------------------------------------------------
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   191
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   192
EtherCATConfigClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "EtherCATConfig.xsd")) 
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   193
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   194
def ExtractHexDecValue(value):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   195
    try:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   196
        return int(value)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   197
    except:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   198
        pass
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   199
    try:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   200
        return int(value.replace("#", "0"), 16)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   201
    except:
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   202
        raise ValueError, "Invalid value for HexDecValue \"%s\"" % value
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   203
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   204
def GenerateHexDecValue(value, base=10):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   205
    if base == 10:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   206
        return str(value)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   207
    elif base == 16:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   208
        return "#x%.8x" % value
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   209
    else:
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   210
        raise ValueError, "Not supported base"
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   211
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   212
cls = EtherCATConfigClasses.get("Config_Slave", None)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   213
if cls:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   214
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   215
    def getType(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   216
        slave_info = self.getInfo()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   217
        return {"device_type": slave_info.getName(),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   218
                "vendor": GenerateHexDecValue(slave_info.getVendorId()),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   219
                "product_code": GenerateHexDecValue(slave_info.getProductCode(), 16),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   220
                "revision_number": GenerateHexDecValue(slave_info.getRevisionNo(), 16)}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   221
    setattr(cls, "getType", getType)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   222
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   223
    def setType(self, type_infos):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   224
        slave_info = self.getInfo()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   225
        slave_info.setName(type_infos["device_type"])
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   226
        slave_info.setVendorId(ExtractHexDecValue(type_infos["vendor"]))
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   227
        slave_info.setProductCode(ExtractHexDecValue(type_infos["product_code"]))
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   228
        slave_info.setRevisionNo(ExtractHexDecValue(type_infos["revision_number"]))
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   229
    setattr(cls, "setType", setType)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   230
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   231
cls = EtherCATConfigClasses.get("Slave_Info", None)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   232
if cls:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   233
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   234
    def getSlavePosition(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   235
        return self.getPhysAddr(), self.getAutoIncAddr()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   236
    setattr(cls, "getSlavePosition", getSlavePosition)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   237
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   238
    def setSlavePosition(self, alias, pos):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   239
        self.setPhysAddr(alias)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   240
        self.setAutoIncAddr(pos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   241
    setattr(cls, "setSlavePosition", setSlavePosition)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   242
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   243
class _EthercatPlug:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   244
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   245
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   246
      <xsd:element name="EtherlabNode">
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   247
        <xsd:complexType>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   248
          <xsd:attribute name="MasterNumber" type="xsd:integer" use="optional" default="0"/>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   249
          <xsd:attribute name="ConfigurePDOs" type="xsd:boolean" use="optional" default="true"/>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   250
        </xsd:complexType>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   251
      </xsd:element>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   252
    </xsd:schema>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   253
    """
2023
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   254
    EditorType = ConfigEditor
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   255
    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   256
    PlugChildsTypes = [("EthercatDS402Slave", _EthercatDS402SlavePlug, "Ethercat DS402 Slave")]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   257
    
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   258
    def __init__(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   259
        filepath = self.ConfigFileName()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   260
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   261
        self.Config = EtherCATConfigClasses["EtherCATConfig"]()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   262
        if os.path.isfile(filepath):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   263
            xmlfile = open(filepath, 'r')
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   264
            tree = minidom.parse(xmlfile)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   265
            xmlfile.close()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   266
            
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   267
            for child in tree.childNodes:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   268
                if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "EtherCATConfig":
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   269
                    self.Config.loadXMLTree(child, ["xmlns:xsi", "xsi:noNamespaceSchemaLocation"])
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   270
                    self.CreateConfigBuffer(True)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   271
        else:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   272
            self.CreateConfigBuffer(False)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   273
            self.OnPlugSave()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   274
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   275
    def ExtractHexDecValue(self, value):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   276
        return ExtractHexDecValue(value)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   277
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   278
    def GetSizeOfType(self, type):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   279
        return TYPECONVERSION.get(self.GetPlugRoot().GetBaseType(type), None)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   280
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   281
    def ConfigFileName(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   282
        return os.path.join(self.PlugPath(), "config.xml")
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   283
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   284
    def GetSlaves(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   285
        slaves = []
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   286
        for slave in self.Config.getConfig().getSlave():
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   287
            slaves.append(slave.getInfo().getSlavePosition())
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   288
        slaves.sort()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   289
        return slaves
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   290
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   291
    def GetSlave(self, slave_pos):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   292
        for slave in self.Config.getConfig().getSlave():
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   293
            slave_info = slave.getInfo()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   294
            if slave_info.getSlavePosition() == slave_pos:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   295
                return slave
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   296
        return None
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   297
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   298
    def AddSlave(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   299
        slaves = self.GetSlaves()
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   300
        for PlugInstance in self.IterChilds():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   301
            slaves.append((PlugInstance.BaseParams.getIEC_Channel(), 0))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   302
        slaves.sort()
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   303
        if len(slaves) > 0:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   304
            new_pos = (slaves[-1][0] + 1, 0)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   305
        else:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   306
            new_pos = (0, 0)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   307
        slave = EtherCATConfigClasses["Config_Slave"]()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   308
        slave_infos = slave.getInfo()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   309
        slave_infos.setName("undefined")
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   310
        slave_infos.setSlavePosition(new_pos[0], new_pos[1])
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   311
        self.Config.getConfig().appendSlave(slave)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   312
        self.BufferConfig()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   313
        return new_pos
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   314
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   315
    def RemoveSlave(self, slave_pos):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   316
        config = self.Config.getConfig()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   317
        for idx, slave in enumerate(config.getSlave()):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   318
            slave_infos = slave.getInfo()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   319
            if slave_infos.getSlavePosition() == slave_pos:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   320
                config.removeSlave(idx)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   321
                self.BufferConfig()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   322
                return True
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   323
        return False
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   324
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   325
    def SetSlavePos(self, slave_pos, alias=None, position=None):
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   326
        for PlugInstance in self.IterChilds():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   327
            if PlugInstance.BaseParams.getIEC_Channel() == alias:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   328
                return _("Slave with alias \"%d\" already exists!" % alias)
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   329
        slave = self.GetSlave(slave_pos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   330
        if slave is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   331
            slave_info = slave.getInfo()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   332
            new_pos = slave_pos
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   333
            if alias is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   334
                new_pos = (alias, new_pos[1])
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   335
            if position is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   336
                new_pos = (new_pos[0], position)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   337
            if self.GetSlave(new_pos) is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   338
                return _("Slave with position \"%d:%d\" already exists!" % new_pos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   339
            slave_info.setSlavePosition(*new_pos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   340
            self.BufferConfig()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   341
        return None
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   342
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   343
    def GetSlaveType(self, slave_pos):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   344
        slave = self.GetSlave(slave_pos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   345
        if slave is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   346
            return slave.getType()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   347
        return None
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   348
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   349
    def SetSlaveType(self, slave_pos, type_infos):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   350
        slave = self.GetSlave(slave_pos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   351
        if slave is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   352
            slave.setType(type_infos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   353
            self.BufferConfig()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   354
        return None
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   355
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   356
    def GetSlaveInfos(self, slave_pos):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   357
        slave = self.GetSlave(slave_pos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   358
        if slave is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   359
            type_infos = slave.getType()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   360
            device = self.GetModuleInfos(type_infos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   361
            if device is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   362
                infos = type_infos.copy()
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   363
                entries = device.GetEntriesList()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   364
                entries_list = entries.items()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   365
                entries_list.sort()
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   366
                infos.update({"physics": device.getPhysics(),
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   367
                              "sync_managers": device.GetSyncManagers(),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   368
                              "entries": [entry[1] for entry in entries_list]})
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   369
                return infos
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   370
        return None
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   371
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   372
    def GetModuleInfos(self, type_infos):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   373
        return self.PlugParent.GetModuleInfos(type_infos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   374
    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   375
    def GetModulesByProfile(self, profile_type):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   376
        return self.PlugParent.GetModulesByProfile(profile_type)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   377
    
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   378
    def GetSlaveTypesLibrary(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   379
        return self.PlugParent.GetModulesLibrary()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   380
    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   381
    def GetDeviceLocationTree(self, current_location, type_infos):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   382
        vars = []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   383
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   384
        device = self.GetModuleInfos(type_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   385
        if device is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   386
            sync_managers = []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   387
            for sync_manager in device.getSm():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   388
                sync_manager_control_byte = ExtractHexDecValue(sync_manager.getControlByte())
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   389
                sync_manager_direction = sync_manager_control_byte & 0x0c
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   390
                if sync_manager_direction:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   391
                    sync_managers.append(LOCATION_VAR_OUTPUT)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   392
                else:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   393
                    sync_managers.append(LOCATION_VAR_INPUT)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   394
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   395
            entries = device.GetEntriesList().items()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   396
            entries.sort()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   397
            for (index, subindex), entry in entries:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   398
                var_size = self.GetSizeOfType(entry["Type"])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   399
                if var_size is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   400
                    var_class = VARCLASSCONVERSION.get(entry["PDOMapping"], None)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   401
                    if var_class is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   402
                        if var_class == LOCATION_VAR_INPUT:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   403
                            var_dir = "%I"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   404
                        else:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   405
                            var_dir = "%Q"    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   406
                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   407
                        vars.append({"name": "0x%4.4x-0x%2.2x: %s" % (index, subindex, entry["Name"]),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   408
                                     "type": var_class,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   409
                                     "size": var_size,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   410
                                     "IEC_type": entry["Type"],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   411
                                     "var_name": "%s_%4.4x_%2.2x" % (type_infos["device_type"], index, subindex),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   412
                                     "location": "%s%s%s"%(var_dir, var_size, ".".join(map(str, current_location + 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   413
                                                                                                (index, subindex)))),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   414
                                     "description": "",
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   415
                                     "children": []})
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   416
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   417
        return vars
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   418
    
2023
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   419
    def GetVariableLocationTree(self):
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   420
        '''See PlugTemplate.GetVariableLocationTree() for a description.'''
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   421
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   422
        current_location = self.GetCurrentLocation()
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   423
        
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   424
        groups = []
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   425
        for slave_pos in self.GetSlaves():
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   426
            
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   427
            slave = self.GetSlave(slave_pos)
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   428
            if slave is not None:
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   429
                type_infos = slave.getType()
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   430
                
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   431
                vars = self.GetDeviceLocationTree(current_location + slave_pos, type_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   432
                if len(vars) > 0:
2023
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   433
                    groups.append({"name": "%s (%d,%d)" % ((type_infos["device_type"],) + slave_pos),
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   434
                                   "type": LOCATION_GROUP,
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   435
                                   "location": ".".join(map(str, current_location + slave_pos)) + ".x",
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   436
                                   "children": vars})
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   437
                
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   438
        return  {"name": self.BaseParams.getName(),
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   439
                 "type": LOCATION_PLUGIN,
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   440
                 "location": self.GetFullIEC_Channel(),
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   441
                 "children": groups}
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   442
    
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   443
    PluginMethods = [
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   444
        {"bitmap" : os.path.join("images", "EditCfile"),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   445
         "name" : _("Edit Config"), 
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   446
         "tooltip" : _("Edit Config"),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   447
         "method" : "_OpenView"},
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   448
    ]
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   449
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   450
    def PlugTestModified(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   451
        return self.ChangesToSave or not self.ConfigIsSaved()    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   452
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   453
    def OnPlugSave(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   454
        filepath = self.ConfigFileName()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   455
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   456
        text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   457
        extras = {"xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   458
                  "xsi:noNamespaceSchemaLocation" : "EtherCATInfo.xsd"}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   459
        text += self.Config.generateXMLText("EtherCATConfig", 0, extras)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   460
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   461
        xmlfile = open(filepath,"w")
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   462
        xmlfile.write(text.encode("utf-8"))
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   463
        xmlfile.close()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   464
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   465
        self.ConfigBuffer.CurrentSaved()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   466
        return True
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   467
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   468
    def _Generate_C(self, buildpath, locations):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   469
        current_location = self.GetCurrentLocation()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   470
        # define a unique name for the generated C file
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   471
        location_str = "_".join(map(lambda x:str(x), current_location))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   472
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   473
        Gen_Ethercatfile_path = os.path.join(buildpath, "ethercat_%s.c"%location_str)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   474
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   475
        self.FileGenerator = _EthercatCFileGenerator(self)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   476
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   477
        LocationCFilesAndCFLAGS, LDFLAGS, extra_files = PlugTemplate._Generate_C(self, buildpath, locations)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   478
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   479
        self.FileGenerator.GenerateCFile(Gen_Ethercatfile_path, location_str, self.EtherlabNode)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   480
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   481
        LocationCFilesAndCFLAGS.append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   482
            (current_location, 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   483
             [(Gen_Ethercatfile_path, '"-I%s"'%os.path.abspath(self.GetPlugRoot().GetIECLibPath()))], 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   484
             True))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   485
        LDFLAGS.append("-lethercat -lrtdm")
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   486
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   487
        return LocationCFilesAndCFLAGS, LDFLAGS, extra_files
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   488
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   489
    def PlugGenerate_C(self, buildpath, locations):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   490
        """
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   491
        Generate C code
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   492
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   493
        @param locations: List of complete variables locations \
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   494
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   495
            "NAME" : name of the variable (generally "__IW0_1_2" style)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   496
            "DIR" : direction "Q","I" or "M"
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   497
            "SIZE" : size "X", "B", "W", "D", "L"
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   498
            "LOC" : tuple of interger for IEC location (0,1,2,...)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   499
            }, ...]
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   500
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   501
        """
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   502
        current_location = self.GetCurrentLocation()
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   503
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   504
        slaves = self.GetSlaves()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   505
        for slave_pos in slaves:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   506
            slave = self.GetSlave(slave_pos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   507
            if slave is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   508
                self.FileGenerator.DeclareSlave(slave_pos, slave.getType())
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   509
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   510
        for location in locations:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   511
            loc = location["LOC"][len(current_location):]
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   512
            slave_pos = loc[:2]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   513
            if slave_pos in slaves:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   514
                self.FileGenerator.DeclareVariable(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   515
                    slave_pos, loc[2], loc[3], location["IEC_TYPE"], location["DIR"], location["NAME"])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   516
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   517
        return [],"",False
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   518
        
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   519
#-------------------------------------------------------------------------------
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   520
#                      Current Buffering Management Functions
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   521
#-------------------------------------------------------------------------------
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   522
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   523
    """
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   524
    Return a copy of the config
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   525
    """
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   526
    def Copy(self, model):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   527
        return cPickle.loads(cPickle.dumps(model))
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   528
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   529
    def CreateConfigBuffer(self, saved):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   530
        self.ConfigBuffer = UndoBuffer(cPickle.dumps(self.Config), saved)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   531
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   532
    def BufferConfig(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   533
        self.ConfigBuffer.Buffering(cPickle.dumps(self.Config))
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   534
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   535
    def ConfigIsSaved(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   536
        if self.ConfigBuffer is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   537
            return self.ConfigBuffer.IsCurrentSaved()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   538
        else:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   539
            return True
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   540
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   541
    def LoadPrevious(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   542
        self.Config = cPickle.loads(self.ConfigBuffer.Previous())
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   543
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   544
    def LoadNext(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   545
        self.Config = cPickle.loads(self.ConfigBuffer.Next())
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   546
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   547
    def GetBufferState(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   548
        first = self.ConfigBuffer.IsFirst()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   549
        last = self.ConfigBuffer.IsLast()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   550
        return not first, not last
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   551
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   552
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   553
SLAVE_PDOS_CONFIGURATION_DECLARATION = """
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   554
/* Slave %(slave)d, "%(device_type)s"
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   555
 * Vendor ID:       0x%(vendor).8x
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   556
 * Product code:    0x%(product_code).8x
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   557
 * Revision number: 0x%(revision_number).8x
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   558
 */
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   559
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   560
ec_pdo_entry_info_t slave_%(slave)d_pdo_entries[] = {
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   561
%(pdos_entries_infos)s
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   562
};
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   563
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   564
ec_pdo_info_t slave_%(slave)d_pdos[] = {
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   565
%(pdos_infos)s
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   566
};
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   567
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   568
ec_sync_info_t slave_%(slave)d_syncs[] = {
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   569
%(pdos_sync_infos)s
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   570
    {0xff}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   571
};
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   572
"""
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   573
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   574
SLAVE_CONFIGURATION_TEMPLATE = """
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   575
    if (!(slave%(slave)d = ecrt_master_slave_config(master, %(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x))) {
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   576
        fprintf(stderr, "Failed to get slave %(device_type)s configuration at alias %(alias)d and position %(position)d.\\n");
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   577
        return -1;
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   578
    }
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   579
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   580
    if (ecrt_slave_config_pdos(slave%(slave)d, EC_END, slave_%(slave)d_syncs)) {
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   581
        fprintf(stderr, "Failed to configure PDOs for slave %(device_type)s at alias %(alias)d and position %(position)d.\\n");
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   582
        return -1;
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   583
    }
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   584
"""
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   585
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   586
SLAVE_INITIALIZATION_TEMPLATE = """
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   587
    {
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   588
        uint8_t value[] = {%(data)s};
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   589
        if (ecrt_master_sdo_download(master, %(slave)d, 0x%(index).4x, 0x%(subindex).2x, (uint8_t *)value, %(data_size)d, &abort_code)) {
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   590
            fprintf(stderr, "Failed to initialize slave %(device_type)s at alias %(alias)d and position %(position)d.\\nError: %%d\\n", abort_code);
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   591
            return -1;
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   592
        }
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   593
    }
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   594
"""
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   595
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   596
def ConfigureVariable(entry_infos, str_completion):
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   597
    entry_infos["data_type"] = DATATYPECONVERSION.get(entry_infos["var_type"], None)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   598
    if entry_infos["data_type"] is None:
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   599
        raise ValueError, _("Type of location \"%s\" not yet supported!") % entry_infos["var_name"]
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   600
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   601
    if not entry_infos.has_key("real_var"):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   602
        entry_infos["real_var"] = "beremiz" + entry_infos["var_name"]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   603
        str_completion["located_variables_declaration"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   604
            "IEC_%(var_type)s %(real_var)s;" % entry_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   605
    str_completion["located_variables_declaration"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   606
         "IEC_%(var_type)s *%(var_name)s = &%(real_var)s;" % entry_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   607
    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   608
    if entry_infos["data_type"] == "BIT":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   609
        if entry_infos["dir"] == "I":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   610
            str_completion["retrieve_variables"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   611
              ("    %(real_var)s = EC_READ_BIT(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   612
               "slave%(slave)d_%(index).4x_%(subindex).2x_bit);") % entry_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   613
        elif entry_infos["dir"] == "Q":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   614
            str_completion["publish_variables"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   615
              ("    EC_WRITE_BIT(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   616
               "slave%(slave)d_%(index).4x_%(subindex).2x_bit, %(real_var)s);") % entry_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   617
    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   618
    else:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   619
        if entry_infos["dir"] == "I":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   620
            str_completion["retrieve_variables"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   621
                ("    %(real_var)s = EC_READ_%(data_type)s(domain1_pd + " + 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   622
                 "slave%(slave)d_%(index).4x_%(subindex).2x);") % entry_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   623
        elif entry_infos["dir"] == "Q":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   624
            str_completion["publish_variables"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   625
                ("    EC_WRITE_%(data_type)s(domain1_pd + slave%(slave)d_%(index).4x_%(subindex).2x, " + 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   626
                 "%(real_var)s);") % entry_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   627
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   628
def ConfigurePDO(entry_infos, str_completion):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   629
    str_completion["used_pdo_entry_offset_variables_declaration"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   630
        "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x;" % entry_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   631
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   632
    if entry_infos["var_type"] == "BOOL":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   633
        str_completion["used_pdo_entry_offset_variables_declaration"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   634
            "unsigned int slave%(slave)d_%(index).4x_%(subindex).2x_bit;" % entry_infos)
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   635
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   636
        str_completion["used_pdo_entry_configuration"].append(
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   637
             ("    {%(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x, " + 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   638
              "0x%(index).4x, %(subindex)d, &slave%(slave)d_%(index).4x_%(subindex).2x, " + 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   639
              "&slave%(slave)d_%(index).4x_%(subindex).2x_bit},") % entry_infos)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   640
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   641
    else:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   642
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   643
        str_completion["used_pdo_entry_configuration"].append(
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   644
            ("    {%(alias)d, %(position)d, 0x%(vendor).8x, 0x%(product_code).8x, 0x%(index).4x, " + 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   645
             "%(subindex)d, &slave%(slave)d_%(index).4x_%(subindex).2x},") % entry_infos)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   646
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   647
class _EthercatCFileGenerator:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   648
    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   649
    def __init__(self, controler):
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   650
        self.Controler = controler
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   651
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   652
        self.Slaves = []
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   653
        self.UsedVariables = {}
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   654
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   655
    def __del__(self):
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   656
        self.Controler = None            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   657
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   658
    def DeclareSlave(self, slave_identifier, slave):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   659
        self.Slaves.append((slave_identifier, slave))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   660
        self.Slaves.sort()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   661
        return self.Slaves.index((slave_identifier, slave))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   662
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   663
    def DeclareVariable(self, slave_identifier, index, subindex, iec_type, dir, name, configure=True):
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   664
        slave_variables = self.UsedVariables.setdefault(slave_identifier, {})
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   665
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   666
        entry_infos = slave_variables.get((index, subindex), None)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   667
        if entry_infos is None:
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   668
            slave_variables[(index, subindex)] = {
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   669
                "infos": (iec_type, dir, name),
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   670
                "configure": configure,
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   671
                "mapped": False}
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   672
        elif entry_infos["infos"] != (iec_type, dir, name):
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   673
            raise ValueError, _("Definition conflict for location \"%s\"") % name 
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   674
        
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   675
    def GenerateCFile(self, filepath, location_str, etherlab_node_infos):
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   676
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   677
        plc_etherlab_filepath = os.path.join(os.path.split(__file__)[0], "plc_etherlab.c")
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   678
        plc_etherlab_file = open(plc_etherlab_filepath, 'r')
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   679
        plc_etherlab_code = plc_etherlab_file.read()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   680
        plc_etherlab_file.close()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   681
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   682
        str_completion = {
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   683
            "location": location_str,
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   684
            "configure_pdos": int(etherlab_node_infos.getConfigurePDOs()),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   685
            "master_number": etherlab_node_infos.getMasterNumber(),
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   686
            "located_variables_declaration": [],
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   687
            "used_pdo_entry_offset_variables_declaration": [],
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   688
            "used_pdo_entry_configuration": [],
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   689
            "pdos_configuration_declaration": "",
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   690
            "slaves_declaration": "",
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   691
            "slaves_configuration": "",
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   692
            "slaves_initialization": "",
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   693
            "retrieve_variables": [],
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   694
            "publish_variables": [],
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   695
        }
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   696
        
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   697
        for slave_entries in self.UsedVariables.itervalues():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   698
            for entry_infos in slave_entries.itervalues():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   699
                entry_infos["mapped"] = False
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   700
        
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   701
        for slave_idx, (slave_pos, type_infos) in enumerate(self.Slaves):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   702
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   703
            device = self.Controler.GetModuleInfos(type_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   704
            if device is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   705
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   706
                slave_variables = self.UsedVariables.get(slave_pos, {})
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   707
                device_entries = device.GetEntriesList()
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   708
                
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   709
                if len(device.getTxPdo() + device.getRxPdo()) > 0 or len(slave_variables) > 0:
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   710
                    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   711
                    for element in ["vendor", "product_code", "revision_number"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   712
                        type_infos[element] = ExtractHexDecValue(type_infos[element])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   713
                    type_infos.update(dict(zip(["slave", "alias", "position"], (slave_idx,) + slave_pos)))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   714
                
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   715
                    str_completion["slaves_declaration"] += "static ec_slave_config_t *slave%(slave)d = NULL;\n" % type_infos
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   716
                    str_completion["slaves_configuration"] += SLAVE_CONFIGURATION_TEMPLATE % type_infos
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   717
    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   718
                    for initCmd in device.getInitCmd():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   719
                        index = ExtractHexDecValue(initCmd.getIndex())
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   720
                        subindex = ExtractHexDecValue(initCmd.getSubIndex())
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   721
                        entry = device_entries.get((index, subindex), None)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   722
                        if entry is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   723
                            data_size = entry["BitSize"] / 8
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   724
                            data = ("%%.%dx" % (data_size * 2)) % initCmd.getData().getcontent()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   725
                            data_str = ",".join(["0x%s" % data[i:i+2] for i in xrange(0, data_size * 2, 2)])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   726
                            init_cmd_infos = {
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   727
                                "index": index,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   728
                                "subindex": subindex,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   729
                                "data": data_str,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   730
                                "data_size": data_size
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   731
                            }
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   732
                            init_cmd_infos.update(type_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   733
                            str_completion["slaves_initialization"] += SLAVE_INITIALIZATION_TEMPLATE % init_cmd_infos
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   734
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   735
                    pdos_infos = {
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   736
                        "pdos_entries_infos": [],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   737
                        "pdos_infos": [],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   738
                        "pdos_sync_infos": [], 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   739
                    }
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   740
                    pdos_infos.update(type_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   741
                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   742
                    sync_managers = []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   743
                    for sync_manager_idx, sync_manager in enumerate(device.getSm()):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   744
                        sync_manager_infos = {
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   745
                            "index": sync_manager_idx, 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   746
                            "name": sync_manager.getcontent(),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   747
                            "slave": slave_idx,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   748
                            "pdos": [], 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   749
                            "pdos_number": 0,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   750
                        }
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   751
                        
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   752
                        sync_manager_control_byte = ExtractHexDecValue(sync_manager.getControlByte())
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   753
                        sync_manager_direction = sync_manager_control_byte & 0x0c
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   754
                        sync_manager_watchdog = sync_manager_control_byte & 0x40
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   755
                        if sync_manager_direction:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   756
                            sync_manager_infos["sync_manager_type"] = "EC_DIR_OUTPUT"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   757
                        else:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   758
                            sync_manager_infos["sync_manager_type"] = "EC_DIR_INPUT"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   759
                        if sync_manager_watchdog:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   760
                            sync_manager_infos["watchdog"] = "EC_WD_ENABLE"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   761
                        else:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   762
                            sync_manager_infos["watchdog"] = "EC_WD_DISABLE"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   763
                        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   764
                        sync_managers.append(sync_manager_infos)
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   765
                    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   766
                    pdos_index = []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   767
                    for only_mandatory in [True, False]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   768
                        for pdo, pdo_type in ([(pdo, "Inputs") for pdo in device.getTxPdo()] +
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   769
                                              [(pdo, "Outputs") for pdo in device.getRxPdo()]):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   770
                            entries = pdo.getEntry()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   771
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   772
                            pdo_needed = pdo.getMandatory()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   773
                            if pdo_needed is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   774
                                pdo_needed = False
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   775
                            if only_mandatory != pdo_needed:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   776
                                continue
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   777
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   778
                            pdo_index = ExtractHexDecValue(pdo.getIndex().getcontent())
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   779
                            pdos_index.append(pdo_index)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   780
                            entries_infos = []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   781
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   782
                            for entry in entries:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   783
                                index = ExtractHexDecValue(entry.getIndex().getcontent())
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   784
                                subindex = ExtractHexDecValue(entry.getSubIndex())
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   785
                                entry_infos = {
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   786
                                    "index": index,
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   787
                                    "subindex": subindex,
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   788
                                    "name": ExtractName(entry.getName()),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   789
                                    "bitlen": entry.getBitLen(),
2023
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   790
                                }
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   791
                                entry_infos.update(type_infos)
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   792
                                entries_infos.append("    {0x%(index).4x, 0x%(subindex).2x, %(bitlen)d}, /* %(name)s */" % entry_infos)
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   793
                                
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   794
                                entry_declaration = slave_variables.get((index, subindex), None)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   795
                                if entry_declaration is not None and not entry_declaration["mapped"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   796
                                    pdo_needed = True
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   797
                                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   798
                                    entry_infos.update(dict(zip(["var_type", "dir", "var_name"], entry_declaration["infos"])))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   799
                                    entry_declaration["mapped"] = True
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   800
                                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   801
                                    if entry_infos["var_type"] != entry.getDataType().getcontent():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   802
                                        raise ValueError, _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   803
                                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   804
                                    if (entry_infos["dir"] == "I" and pdo_type != "Inputs" or 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   805
                                        entry_infos["dir"] == "Q" and pdo_type != "Outputs"):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   806
                                        raise ValueError, _("Wrong direction for location \"%s\"!") % entry_infos["var_name"]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   807
                                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   808
                                    if entry_declaration["configure"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   809
                                        ConfigureVariable(entry_infos, str_completion)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   810
                                    ConfigurePDO(entry_infos, str_completion)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   811
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   812
                            if pdo_needed:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   813
                                sm = pdo.getSm()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   814
                                if sm is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   815
                                    for sm_idx, sync_manager in enumerate(sync_managers):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   816
                                        if sync_manager["name"] == pdo_type:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   817
                                            sm = sm_idx
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   818
                                if sm is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   819
                                    raise ValueError, _("No sync manager available for %s pdo!") % pdo_type
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   820
                                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   821
                                sync_managers[sm]["pdos_number"] += 1
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   822
                                sync_managers[sm]["pdos"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   823
                                    {"slave": slave_idx,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   824
                                     "index": pdo_index,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   825
                                     "name": ExtractName(pdo.getName()),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   826
                                     "type": pdo_type, 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   827
                                     "entries": entries_infos,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   828
                                     "entries_number": len(entries_infos),
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   829
                                     "fixed": pdo.getFixed() == True})
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   830
                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   831
                    dynamic_pdos = {}
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   832
                    dynamic_pdos_number = 0
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   833
                    for category, min_index, max_index in [("Inputs", 0x1600, 0x1800), 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   834
                                                           ("Outputs", 0x1a00, 0x1C00)]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   835
                        for sync_manager in sync_managers:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   836
                            if sync_manager["name"] == category:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   837
                                category_infos = dynamic_pdos.setdefault(category, {})
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   838
                                category_infos["sync_manager"] = sync_manager
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   839
                                category_infos["pdos"] = [pdo for pdo in category_infos["sync_manager"]["pdos"] 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   840
                                                          if not pdo["fixed"] and pdo["type"] == category]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   841
                                category_infos["current_index"] = min_index
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   842
                                category_infos["max_index"] = max_index
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   843
                                break
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   844
                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   845
                    for (index, subindex), entry_declaration in slave_variables.iteritems():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   846
                        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   847
                        if not entry_declaration["mapped"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   848
                            entry = device_entries.get((index, subindex), None)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   849
                            if entry is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   850
                                raise ValueError, _("Unknown entry index 0x%4.4x, subindex 0x%2.2x for device %s") % \
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   851
                                                 (index, subindex, type_infos["device_type"])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   852
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   853
                            entry_infos = {
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   854
                                "index": index,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   855
                                "subindex": subindex,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   856
                                "name": entry["Name"],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   857
                                "bitlen": entry["BitSize"],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   858
                            }
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   859
                            entry_infos.update(type_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   860
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   861
                            entry_infos.update(dict(zip(["var_type", "dir", "var_name", "real_var"], entry_declaration["infos"])))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   862
                            entry_declaration["mapped"] = True
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   863
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   864
                            if entry_infos["var_type"] != entry["Type"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   865
                                raise ValueError, _("Wrong type for location \"%s\"!") % entry_infos["var_name"]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   866
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   867
                            if entry_infos["dir"] == "I" and entry["PDOMapping"] in ["T", "RT"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   868
                                pdo_type = "Inputs"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   869
                            elif entry_infos["dir"] == "Q" and entry["PDOMapping"] in ["R", "RT"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   870
                                pdo_type = "Outputs"
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   871
                            else:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   872
                                raise ValueError, _("Wrong direction for location \"%s\"!") % entry_infos["var_name"]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   873
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   874
                            if not dynamic_pdos.has_key(pdo_type):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   875
                                raise ValueError, _("No Sync manager defined for %s!") % pdo_type
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   876
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   877
                            if entry_declaration["configure"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   878
                                ConfigureVariable(entry_infos, str_completion)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   879
                            ConfigurePDO(entry_infos, str_completion)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   880
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   881
                            if len(dynamic_pdos[pdo_type]["pdos"]) > 0:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   882
                                pdo = dynamic_pdos[pdo_type]["pdos"][0]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   883
                            else:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   884
                                while dynamic_pdos[pdo_type]["current_index"] in pdos_index:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   885
                                    dynamic_pdos[pdo_type]["current_index"] += 1
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   886
                                if dynamic_pdos[pdo_type]["current_index"] >= dynamic_pdos[pdo_type]["max_index"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   887
                                    raise ValueError, _("No more free PDO index available for %s!") % pdo_type
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   888
                                pdos_index.append(dynamic_pdos[pdo_type]["current_index"])
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   889
                                
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   890
                                dynamic_pdos_number += 1
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   891
                                pdo = {"slave": slave_idx,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   892
                                       "index": dynamic_pdos[pdo_type]["current_index"],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   893
                                       "name": "Dynamic PDO %d" % dynamic_pdos_number,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   894
                                       "type": pdo_type, 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   895
                                       "entries": [],
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   896
                                       "entries_number": 0,
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   897
                                       "fixed": False}
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   898
                                dynamic_pdos[pdo_type]["sync_manager"]["pdos_number"] += 1
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   899
                                dynamic_pdos[pdo_type]["sync_manager"]["pdos"].append(pdo)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   900
                                dynamic_pdos[pdo_type]["pdos"].append(pdo)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   901
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   902
                            pdo["entries"].append("    {0x%(index).4x, 0x%(subindex).2x, %(bitlen)d}, /* %(name)s */" % entry_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   903
                            pdo["entries_number"] += 1
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   904
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   905
                            if pdo["entries_number"] == 255:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   906
                                dynamic_pdos[pdo_type]["pdos"].pop(0)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   907
                            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   908
                    pdo_offset = 0
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   909
                    entry_offset = 0
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   910
                    for sync_manager_infos in sync_managers:
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   911
                        
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   912
                        for pdo_infos in sync_manager_infos["pdos"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   913
                            pdo_infos["offset"] = entry_offset
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   914
                            pdo_entries = pdo_infos["entries"]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   915
                            pdos_infos["pdos_infos"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   916
                                ("    {0x%(index).4x, %(entries_number)d, " + 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   917
                                 "slave_%(slave)d_pdo_entries + %(offset)d}, /* %(name)s */") % pdo_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   918
                            entry_offset += len(pdo_entries)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   919
                            pdos_infos["pdos_entries_infos"].extend(pdo_entries)
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   920
                        
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   921
                        sync_manager_infos["offset"] = pdo_offset
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   922
                        pdos_infos["pdos_sync_infos"].append(
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   923
                            ("    {%(index)d, %(sync_manager_type)s, %(pdos_number)d, " + 
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   924
                             "slave_%(slave)d_pdos + %(offset)d, %(watchdog)s},") % sync_manager_infos)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   925
                        pdo_offset += sync_manager_infos["pdos_number"]
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   926
                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   927
                    for element in ["pdos_entries_infos", "pdos_infos", "pdos_sync_infos"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   928
                        pdos_infos[element] = "\n".join(pdos_infos[element])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   929
                    
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   930
                    str_completion["pdos_configuration_declaration"] += SLAVE_PDOS_CONFIGURATION_DECLARATION % pdos_infos
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   931
        
2023
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   932
        for element in ["used_pdo_entry_offset_variables_declaration", 
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   933
                        "used_pdo_entry_configuration", 
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   934
                        "located_variables_declaration", 
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   935
                        "retrieve_variables", 
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
   936
                        "publish_variables"]:
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   937
            str_completion[element] = "\n".join(str_completion[element])
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   938
        
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   939
        etherlabfile = open(filepath, 'w')
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   940
        etherlabfile.write(plc_etherlab_code % str_completion)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   941
        etherlabfile.close()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   942
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   943
#--------------------------------------------------
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   944
#                 Ethercat Plugin
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   945
#--------------------------------------------------
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   946
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   947
EtherCATInfoClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "EtherCATInfo.xsd")) 
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   948
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   949
cls = EtherCATInfoClasses["EtherCATInfo.xsd"].get("DeviceType", None)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   950
if cls:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   951
    cls.DataTypes = None
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   952
    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   953
    def GetProfileNumbers(self):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   954
        profiles = []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   955
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   956
        for profile in self.getProfile():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   957
            profile_content = profile.getcontent()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   958
            if profile_content is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   959
                continue
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   960
            
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   961
            for content_element in profile_content["value"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   962
                if content_element["name"] == "ProfileNo":
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   963
                    profiles.append(content_element["value"])
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   964
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   965
        return profiles
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   966
    setattr(cls, "GetProfileNumbers", GetProfileNumbers)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
   967
    
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   968
    def GetProfileDictionaries(self):
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   969
        dictionaries = []
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   970
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   971
        for profile in self.getProfile():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   972
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   973
            profile_content = profile.getcontent()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   974
            if profile_content is None:
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
   975
                continue
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   976
            
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   977
            for content_element in profile_content["value"]:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   978
                if content_element["name"] == "Dictionary":
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   979
                    dictionaries.append(content_element["value"])
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   980
                elif content_element["name"] == "DictionaryFile":
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   981
                    raise ValueError, "DictionaryFile for defining Device Profile is not yet supported!"
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   982
                
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   983
        return dictionaries
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   984
    setattr(cls, "GetProfileDictionaries", GetProfileDictionaries)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   985
    
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   986
    def ExtractDataTypes(self):
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   987
        self.DataTypes = {}
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   988
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   989
        for dictionary in self.GetProfileDictionaries():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   990
            
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   991
            datatypes = dictionary.getDataTypes()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   992
            if datatypes is not None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   993
                
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   994
                for datatype in datatypes.getDataType():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   995
                    content = datatype.getcontent()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   996
                    if content is not None and content["name"] == "SubItem":
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   997
                        self.DataTypes[datatype.getName()] = datatype
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   998
    
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
   999
    setattr(cls, "ExtractDataTypes", ExtractDataTypes)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1000
    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1001
    def getInitCmd(self):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1002
        mailbox = self.getMailbox()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1003
        if mailbox is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1004
            return []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1005
        
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1006
        coe = mailbox.getCoE()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1007
        if coe is None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1008
            return []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1009
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1010
        return coe.getInitCmd()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1011
    setattr(cls, "getInitCmd", getInitCmd)
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1012
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1013
    def GetEntriesList(self):
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1014
        if self.DataTypes is None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1015
            self.ExtractDataTypes()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1016
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1017
        entries = {}
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1018
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1019
        for dictionary in self.GetProfileDictionaries():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1020
            
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1021
            for object in dictionary.getObjects().getObject():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1022
                entry_index = object.getIndex().getcontent()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1023
                index = ExtractHexDecValue(entry_index)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1024
                entry_type = object.getType()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1025
                entry_name = ExtractName(object.getName())
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1026
                
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1027
                entry_type_infos = self.DataTypes.get(entry_type, None)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1028
                if entry_type_infos is not None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1029
                    content = entry_type_infos.getcontent()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1030
                    for subitem in content["value"]:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1031
                        entry_subidx = subitem.getSubIdx()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1032
                        if entry_subidx is None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1033
                            entry_subidx = "0"
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1034
                        subidx = ExtractHexDecValue(entry_subidx)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1035
                        subitem_access = ""
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1036
                        subitem_pdomapping = ""
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1037
                        subitem_flags = subitem.getFlags()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1038
                        if subitem_flags is not None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1039
                            access = subitem_flags.getAccess()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1040
                            if access is not None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1041
                                subitem_access = access.getcontent()
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1042
                            pdomapping = subitem_flags.getPdoMapping()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1043
                            if pdomapping is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1044
                                subitem_pdomapping = pdomapping.upper()
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1045
                        entries[(index, subidx)] = {
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1046
                            "Index": entry_index,
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1047
                            "SubIndex": entry_subidx,
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1048
                            "Name": "%s - %s" % 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1049
                                    (entry_name.decode("utf-8"),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1050
                                     ExtractName(subitem.getDisplayName(), 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1051
                                                 subitem.getName()).decode("utf-8")),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1052
                            "Type": subitem.getType(),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1053
                            "BitSize": subitem.getBitSize(),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1054
                            "Access": subitem_access, 
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1055
                            "PDOMapping": subitem_pdomapping, 
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1056
                            "PDO index": "", 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1057
                            "PDO name": "", 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1058
                            "PDO type": ""}
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1059
                else:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1060
                    entry_access = ""
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1061
                    entry_pdomapping = ""
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1062
                    entry_flags = object.getFlags()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1063
                    if entry_flags is not None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1064
                        access = entry_flags.getAccess()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1065
                        if access is not None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1066
                            entry_access = access.getcontent()
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1067
                        pdomapping = entry_flags.getPdoMapping()
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1068
                        if pdomapping is not None:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1069
                            entry_pdomapping = pdomapping.upper()
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1070
                    entries[(index, 0)] = {
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1071
                         "Index": entry_index,
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1072
                         "SubIndex": "0",
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1073
                         "Name": entry_name,
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1074
                         "Type": entry_type,
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1075
                         "BitSize": object.getBitSize(),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1076
                         "Access": entry_access,
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1077
                         "PDOMapping": entry_pdomapping, 
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1078
                         "PDO index": "", 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1079
                         "PDO name": "", 
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1080
                         "PDO type": ""}
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1081
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1082
        for TxPdo in self.getTxPdo():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1083
            ExtractPdoInfos(TxPdo, "Transmit", entries)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1084
        for RxPdo in self.getRxPdo():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1085
            ExtractPdoInfos(RxPdo, "Receive", entries)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1086
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1087
        return entries
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1088
    setattr(cls, "GetEntriesList", GetEntriesList)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1089
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1090
    def GetSyncManagers(self):
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1091
        sync_managers = []
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1092
        for sync_manager in self.getSm():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1093
            sync_manager_infos = {}
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1094
            for name, value in [("Name", sync_manager.getcontent()),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1095
                                ("Start Address", sync_manager.getStartAddress()),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1096
                                ("Default Size", sync_manager.getDefaultSize()),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1097
                                ("Control Byte", sync_manager.getControlByte()),
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1098
                                ("Enable", sync_manager.getEnable())]:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1099
                if value is None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1100
                    value =""
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1101
                sync_manager_infos[name] = value
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1102
            sync_managers.append(sync_manager_infos)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1103
        return sync_managers
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1104
    setattr(cls, "GetSyncManagers", GetSyncManagers)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1105
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1106
def GroupItemCompare(x, y):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1107
    if x["type"] == y["type"]:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1108
        if x["type"] == ETHERCAT_GROUP:
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1109
            return cmp(x["order"], y["order"])
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1110
        else:
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1111
            return cmp(x["name"], y["name"])
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1112
    elif x["type"] == ETHERCAT_GROUP:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1113
        return -1
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1114
    return 1
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1115
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1116
def SortGroupItems(group):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1117
    for item in group["children"]:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1118
        if item["type"] == ETHERCAT_GROUP:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1119
            SortGroupItems(item)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1120
    group["children"].sort(GroupItemCompare)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1121
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1122
def ExtractName(names, default=None):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1123
    if len(names) == 1:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1124
        return names[0].getcontent()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1125
    else:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1126
        for name in names:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1127
            if name.getLcId() == 1033:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1128
                return name.getcontent()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1129
    return default
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1130
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1131
def ExtractPdoInfos(pdo, pdo_type, entries):
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1132
    pdo_index = pdo.getIndex().getcontent()
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1133
    pdo_name = ExtractName(pdo.getName())
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1134
    for pdo_entry in pdo.getEntry():
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1135
        entry_index = pdo_entry.getIndex().getcontent()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1136
        entry_subindex = pdo_entry.getSubIndex()
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1137
        index = ExtractHexDecValue(entry_index)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1138
        subindex = ExtractHexDecValue(entry_subindex)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1139
        
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1140
        entry = entries.get((index, subindex), None)
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1141
        if entry is not None:
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1142
            entry["PDO index"] = pdo_index
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1143
            entry["PDO name"] = pdo_name
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1144
            entry["PDO type"] = pdo_type
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1145
        else:
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1146
            entry_type = pdo_entry.getDataType()
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1147
            if entry_type is not None:
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1148
                if pdo_type == "Transmit":
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1149
                    access = "ro"
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1150
                    pdomapping = "T"
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1151
                else:
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1152
                    access = "wo"
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1153
                    pdomapping = "R"
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1154
                entries[(index, subindex)] = {
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1155
                    "Index": entry_index,
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1156
                    "SubIndex": entry_subindex,
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1157
                    "Name": ExtractName(pdo_entry.getName()),
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1158
                    "Type": entry_type.getcontent(),
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1159
                    "Access": access,
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1160
                    "PDOMapping": pdomapping,
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1161
                    "PDO index": pdo_index, 
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1162
                    "PDO name": pdo_name, 
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1163
                    "PDO type": pdo_type}
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1164
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1165
class RootClass:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1166
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1167
    PlugChildsTypes = [("EthercatNode",_EthercatPlug,"Ethercat Master")]
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1168
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1169
    def __init__(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1170
        self.LoadModulesLibrary()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1171
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1172
    def GetModulesLibraryPath(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1173
        library_path = os.path.join(self.PlugPath(), "modules")
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1174
        if not os.path.exists(library_path):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1175
            os.mkdir(library_path)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1176
        return library_path
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1177
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1178
    def _ImportModuleLibrary(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1179
        dialog = wx.FileDialog(self.GetPlugRoot().AppFrame, _("Choose an XML file"), os.getcwd(), "",  _("XML files (*.xml)|*.xml|All files|*.*"), wx.OPEN)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1180
        if dialog.ShowModal() == wx.ID_OK:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1181
            filepath = dialog.GetPath()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1182
            if os.path.isfile(filepath):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1183
                shutil.copy(filepath, self.GetModulesLibraryPath())
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1184
                self.LoadModulesLibrary()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1185
            else:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1186
                self.GetPlugRoot().logger.write_error(_("No such XML file: %s\n") % filepath)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1187
        dialog.Destroy()  
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1188
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1189
    PluginMethods = [
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1190
        {"bitmap" : os.path.join("images", "ImportDEF"),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1191
         "name" : _("Import module library"), 
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1192
         "tooltip" : _("Import module library"),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1193
         "method" : "_ImportModuleLibrary"},
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1194
    ]
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1195
    
2023
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
  1196
    def PlugGenerate_C(self, buildpath, locations):
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
  1197
        return [],"",False
f9f884cf3033 Adding support for not configuring pdos when not mandatory and not needed by locations defined in PLC program. Adding support for displaying locations tree in Topology panel and BrowseLocationsDialog. Merging pdos grid and variables grid into one single grid in slave infos panel.
laurent
parents: 2022
diff changeset
  1198
    
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1199
    def LoadModulesLibrary(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1200
        self.ModulesLibrary = {}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1201
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1202
        library_path = self.GetModulesLibraryPath()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1203
        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1204
        files = os.listdir(library_path)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1205
        for file in files:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1206
            filepath = os.path.join(library_path, file)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1207
            if os.path.isfile(filepath) and os.path.splitext(filepath)[-1] == ".xml":
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1208
                xmlfile = open(filepath, 'r')
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1209
                xml_tree = minidom.parse(xmlfile)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1210
                xmlfile.close()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1211
                
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1212
                modules_infos = None
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1213
                for child in xml_tree.childNodes:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1214
                    if child.nodeType == xml_tree.ELEMENT_NODE and child.nodeName == "EtherCATInfo":
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1215
                        modules_infos = EtherCATInfoClasses["EtherCATInfo.xsd"]["EtherCATInfo"]()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1216
                        modules_infos.loadXMLTree(child, ["xmlns:xsi", "xsi:noNamespaceSchemaLocation"])
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1217
                
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1218
                if modules_infos is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1219
                    vendor = modules_infos.getVendor()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1220
                    
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1221
                    vendor_category = self.ModulesLibrary.setdefault(ExtractHexDecValue(vendor.getId()), 
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1222
                                                                     {"name": ExtractName(vendor.getName(), _("Miscellaneous")), 
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1223
                                                                      "groups": {}})
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1224
                    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1225
                    for group in modules_infos.getDescriptions().getGroups().getGroup():
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1226
                        group_type = group.getType()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1227
                        
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1228
                        vendor_category["groups"].setdefault(group_type, {"name": ExtractName(group.getName(), group_type), 
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1229
                                                                          "parent": group.getParentGroup(),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1230
                                                                          "order": group.getSortOrder(), 
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1231
                                                                          "devices": []})
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1232
                    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1233
                    for device in modules_infos.getDescriptions().getDevices().getDevice():
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1234
                        device_group = device.getGroupType()
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1235
                        if not vendor_category["groups"].has_key(device_group):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1236
                            raise ValueError, "Not such group \"%\"" % device_group
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1237
                        vendor_category["groups"][device_group]["devices"].append((device.getType().getcontent(), device))
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1238
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1239
    def GetModulesLibrary(self):
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1240
        library = []
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1241
        children_dict = {}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1242
        for vendor_id, vendor in self.ModulesLibrary.iteritems():
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1243
            groups = []
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1244
            library.append({"name": vendor["name"],
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1245
                            "type": ETHERCAT_VENDOR,
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1246
                            "children": groups})
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1247
            for group_type, group in vendor["groups"].iteritems():
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1248
                group_infos = {"name": group["name"],
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1249
                               "order": group["order"],
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1250
                               "type": ETHERCAT_GROUP,
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1251
                               "children": children_dict.setdefault(group_type, [])}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1252
                if group["parent"] is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1253
                    parent_children = children_dict.setdefault(group["parent"], [])
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1254
                    parent_children.append(group_infos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1255
                else:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1256
                    groups.append(group_infos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1257
                device_dict = {}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1258
                for device_type, device in group["devices"]:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1259
                    device_infos = {"name": ExtractName(device.getName()),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1260
                                    "type": ETHERCAT_DEVICE,
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1261
                                    "infos": {"device_type": device_type,
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1262
                                              "vendor": vendor_id,
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1263
                                              "product_code": device.getType().getProductCode(),
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1264
                                              "revision_number": device.getType().getRevisionNo()}}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1265
                    group_infos["children"].append(device_infos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1266
                    device_type_occurrences = device_dict.setdefault(device_type, [])
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1267
                    device_type_occurrences.append(device_infos)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1268
                for device_type_occurrences in device_dict.itervalues():
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1269
                    if len(device_type_occurrences) > 1:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1270
                        for occurrence in device_type_occurrences:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1271
                            occurrence["name"] += _(" (rev. %s)") % occurrence["infos"]["revision_number"]
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1272
        library.sort(lambda x, y: cmp(x["name"], y["name"]))
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1273
        return library
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1274
    
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1275
    def GetModuleInfos(self, type_infos):
2031
c6f32810723e Fix some issues regarding arbitrary variable mapping
laurent
parents: 2030
diff changeset
  1276
        vendor = self.ModulesLibrary.get(ExtractHexDecValue(type_infos["vendor"]), None)
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1277
        if vendor is not None:
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1278
            for group_name, group in vendor["groups"].iteritems():
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1279
                for device_type, device in group["devices"]:
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1280
                    product_code = ExtractHexDecValue(device.getType().getProductCode())
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1281
                    revision_number = ExtractHexDecValue(device.getType().getRevisionNo())
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1282
                    if (device_type == type_infos["device_type"] and
2029
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1283
                        product_code == ExtractHexDecValue(type_infos["product_code"]) and
7c848efa21c6 Adding support for displaying slave sync managers and profile object dictionary and for arbitrarily mapping variable through variable location
laurent
parents: 2026
diff changeset
  1284
                        revision_number == ExtractHexDecValue(type_infos["revision_number"])):
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1285
                        return device
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1286
        return None
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
  1287
    
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1288
    def GetModulesByProfile(self, profile_type):
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1289
        modules = []
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1290
        for vendor_id, vendor in self.ModulesLibrary.iteritems():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1291
            for group_type, group in vendor["groups"].iteritems():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1292
                for device_type, device in group["devices"]:
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1293
                    if profile_type in device.GetProfileNumbers():
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1294
                        product_code = ExtractHexDecValue(device.getType().getProductCode())
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1295
                        revision_number = ExtractHexDecValue(device.getType().getRevisionNo())
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1296
                        modules.append((device_type, vendor_id, product_code, revision_number))
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1297
        return modules
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1298
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2031
diff changeset
  1299