config_utils.py
author etisserant
Mon, 13 Aug 2007 17:22:39 +0200
changeset 7 e20fa7257d41
parent 3 6d8728efcdec
permissions -rw-r--r--
Added stdout/stderr separation limitation and coloration
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
     1
#!/usr/bin/env python
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
     3
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 1
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 1
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
     6
#
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 1
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
     8
#
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    10
#
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 1
diff changeset
    12
#modify it under the terms of the GNU General Public
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    15
#
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 1
diff changeset
    19
#General Public License for more details.
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    20
#
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 1
diff changeset
    21
#You should have received a copy of the GNU General Public
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
3
6d8728efcdec Adding validity verifications on project folders opened
lbessard
parents: 1
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    24
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    25
from types import *
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    26
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    27
DicoTypes = {"BOOL":0x01, "SINT":0x02, "INT":0x03,"DINT":0x04,"LINT":0x10,
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    28
             "USINT":0x05,"UINT":0x06,"UDINT":0x07,"ULINT":0x1B,"REAL":0x08,
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    29
             "LREAL":0x11,"STRING":0x09,"BYTE":0x02,"WORD":0x03,"DWORD":0x04,
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    30
             "LWORD":0x1B,"WSTRING":0x0B}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    31
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    32
DictLocations = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    33
DictCobID = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    34
DictLocationsNotMapped = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    35
ListCobIDAvailable = []
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    36
SlavesPdoNumber = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    37
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    38
# Constants for PDO types 
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    39
RPDO = 1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    40
TPDO = 2
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    41
SlavePDOType = {"I" : TPDO, "Q" : RPDO}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    42
InvertPDOType = {RPDO : TPDO, TPDO : RPDO}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    43
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    44
DefaultTransmitTypeMaster = 0x01
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    45
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    46
GenerateMasterMapping = lambda x:[None] + [(loc_infos["type"], name) for name, loc_infos in x]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    47
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    48
TrashVariableSizes = {1 : 0x01, 8 : 0x05, 16 : 0x06, 32 : 0x07, 64 : 0x1B}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    49
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    50
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    51
def GetSlavePDOIndexes(slave, type, parameters = False):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    52
    indexes = []
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    53
    if type & RPDO:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    54
        indexes.extend([idx for idx in slave.GetIndexes() if 0x1400 <= idx <= 0x15FF])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    55
    if type & TPDO:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    56
        indexes.extend([idx for idx in slave.GetIndexes() if 0x1800 <= idx <= 0x19FF])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    57
    if not parameters:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    58
        return [idx + 0x200 for idx in indexes]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    59
    else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    60
        return indexes
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    61
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    62
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    63
def LE_to_BE(value, size): # Convert Little Endian to Big Endian
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    64
    data = ("%" + str(size * 2) + "." + str(size * 2) + "X") % value
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    65
    list_car = [data[i:i+2] for i in xrange(0, len(data), 2)]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    66
    list_car.reverse()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    67
    return "".join([chr(int(car, 16)) for car in list_car])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    68
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    69
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    70
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    71
def SearchSlavePDOMapping(loc_infos, slave): # Search the TPDO or RPDO mapping where location is defined on the slave
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    72
    typeinfos = slave.GetEntryInfos(loc_infos["type"])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    73
    model = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + typeinfos["size"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    74
    slavePDOidxlist = GetSlavePDOIndexes(slave, loc_infos["pdotype"])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    75
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    76
    for PDOidx in slavePDOidxlist:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    77
        values = slave.GetEntry(PDOidx)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    78
        if values != None:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    79
            for subindex, mapping in enumerate(values):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    80
                if subindex != 0 and mapping == model:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    81
                    return PDOidx, subindex
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    82
    return None
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    83
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    84
def GenerateMappingDCF(cobid, idx, pdomapping, mapped): # Build concise DCF
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    85
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    86
    # Create entry for RPDO or TPDO parameters and Disable PDO
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    87
    dcfdata = LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE((0x80000000 + cobid), 4)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    88
    # Set Transmit type synchrone
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    89
    dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x02, 1) + LE_to_BE(0x01, 4) + LE_to_BE(DefaultTransmitTypeSlave, 1)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    90
    # Re-Enable PDO
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    91
    #         ---- INDEX -----   --- SUBINDEX ----   ----- SIZE ------   ------ DATA ------
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    92
    dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(0x00000000 + cobid, 4)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    93
    nbparams = 3
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    94
    if mapped == False and pdomapping != None:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    95
    # Map Variables
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    96
        for subindex, (name, loc_infos) in enumerate(pdomapping):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    97
            value = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + loc_infos["size"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    98
            dcfdata += LE_to_BE(idx + 0x200, 2) + LE_to_BE(subindex + 1, 1) + LE_to_BE(0x04, 4) + LE_to_BE(value, 4)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
    99
            nbparams += 1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   100
    return dcfdata, nbparams
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   101
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   102
def GetNewCobID(nodeid, type): # Return a cobid not used
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   103
    global ListCobIDAvailable, SlavesPdoNumber
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   104
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   105
    if len(ListCobIDAvailable) == 0:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   106
        return None
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   107
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   108
    nbSlavePDO = SlavesPdoNumber[nodeid][type]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   109
    if type == RPDO:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   110
        if nbSlavePDO < 4:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   111
            # For the fourth PDO -> cobid = 0x200 + ( numPdo parameters * 0x100) + nodeid
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   112
            newcobid = (0x200 + nbSlavePDO * 0x100 + nodeid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   113
            if newcobid in ListCobIDAvailable:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   114
                ListCobIDAvailable.remove(newcobid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   115
                return newcobid, 0x1400 + nbSlavePDO
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   116
        return ListCobIDAvailable.pop(0), 0x1400 + nbSlavePDO
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   117
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   118
    elif type == TPDO:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   119
        if nbSlavePDO < 4:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   120
            # For the fourth PDO -> cobid = 0x180 + (numPdo parameters * 0x100) + nodeid
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   121
            newcobid = (0x180 + nbSlavePDO * 0x100 + nodeid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   122
            if newcobid in ListCobIDAvailable:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   123
                ListCobIDAvailable.remove(newcobid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   124
                return newcobid, 0x1800 + nbSlavePDO
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   125
        return ListCobIDAvailable.pop(0), 0x1800 + nbSlavePDO
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   126
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   127
    for number in xrange(4):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   128
        if type == RPDO:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   129
            # For the fourth PDO -> cobid = 0x200 + ( numPdo * 0x100) + nodeid
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   130
            newcobid = (0x200 + number * 0x100 + nodeid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   131
        elif type == TPDO:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   132
            # For the fourth PDO -> cobid = 0x180 + (numPdo * 0x100) + nodeid
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   133
            newcobid = (0x180 + number * 0x100 + nodeid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   134
        else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   135
            return None
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   136
        if newcobid in ListCobIDAvailable:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   137
            ListCobIDAvailable.remove(newcobid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   138
            return newcobid
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   139
    return ListCobIDAvailable.pop(0)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   140
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   141
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   142
def GenerateConciseDCF(locations, busname, nodelist):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   143
    global DictLocations, DictCobID, DictLocationsNotMapped, ListCobIDAvailable, SlavesPdoNumber, DefaultTransmitTypeSlave
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   144
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   145
    DictLocations = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   146
    DictCobID = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   147
    DictLocationsNotMapped = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   148
    DictSDOparams = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   149
    ListCobIDAvailable = range(0x180, 0x580)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   150
    SlavesPdoNumber = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   151
    DictNameVariable = { "" : 1, "X": 2, "B": 3, "W": 4, "D": 5, "L": 6, "increment": 0x100, 1:("__I", 0x2000), 2:("__Q", 0x4000)}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   152
    DefaultTransmitTypeSlave = 0xFF
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   153
    # Master Node initialisation
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   154
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   155
    manager = nodelist.Manager
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   156
    masternode = manager.GetCurrentNodeCopy()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   157
    if not masternode.IsEntry(0x1F22):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   158
        masternode.AddEntry(0x1F22, 1, "")
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   159
    manager.AddSubentriesToCurrent(0x1F22, 127, masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   160
    # Adding trash mappable variables for unused mapped datas
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   161
    idxTrashVariables = 0x2000 + masternode.GetNodeID()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   162
    TrashVariableValue = {}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   163
    manager.AddMapVariableToCurrent(idxTrashVariables, "trashvariables", 3, len(TrashVariableSizes), masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   164
    for subidx, (size, typeidx) in enumerate(TrashVariableSizes.items()):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   165
        manager.SetCurrentEntry(idxTrashVariables, subidx + 1, "TRASH%d" % size, "name", None, masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   166
        manager.SetCurrentEntry(idxTrashVariables, subidx + 1, typeidx, "type", None, masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   167
        TrashVariableValue[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   168
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   169
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   170
    # Extract Master Node current empty mapping index
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   171
    CurrentPDOParamsIdx = {RPDO : 0x1400 + len(GetSlavePDOIndexes(masternode, RPDO)),
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   172
                           TPDO : 0x1800 + len(GetSlavePDOIndexes(masternode, TPDO))}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   173
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   174
    # Get list of all Slave's CobID and Slave's default SDO server parameters
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   175
    for nodeid, nodeinfos in nodelist.SlaveNodes.items():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   176
        node = nodeinfos["Node"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   177
        node.SetNodeID(nodeid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   178
        DictSDOparams[nodeid] = {"RSDO" : node.GetEntry(0x1200,0x01), "TSDO" : node.GetEntry(0x1200,0x02)}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   179
        slaveRpdoIndexes = GetSlavePDOIndexes(node, RPDO, True)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   180
        slaveTpdoIndexes = GetSlavePDOIndexes(node, TPDO, True)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   181
        SlavesPdoNumber[nodeid] = {RPDO : len(slaveRpdoIndexes), TPDO : len(slaveTpdoIndexes)}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   182
        for PdoIdx in slaveRpdoIndexes + slaveTpdoIndexes:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   183
            pdo_cobid = node.GetEntry(PdoIdx, 0x01)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   184
            if pdo_cobid > 0x600 :
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   185
                pdo_cobid -= 0x80000000
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   186
            if pdo_cobid in ListCobIDAvailable:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   187
                ListCobIDAvailable.remove(pdo_cobid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   188
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   189
    # Get list of locations check if exists and mappables -> put them in DictLocations
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   190
    for locationtype, name in locations:    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   191
        if name in DictLocations.keys():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   192
            if DictLocations[name]["type"] != DicoTypes[locationtype]:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   193
                raise ValueError, "Conflict type for location \"%s\"" % name 
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   194
        else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   195
            loc = [i for i in name.split("_") if len(i) > 0]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   196
            if len(loc) not in (4, 5):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   197
                continue
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   198
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   199
            prefix = loc[0][0]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   200
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   201
            # Extract and check busname
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   202
            if loc[0][1].isdigit():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   203
                sizelocation = ""
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   204
                busnamelocation = int(loc[0][1:])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   205
            else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   206
                sizelocation = loc[0][1]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   207
                busnamelocation = int(loc[0][2:])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   208
            if busnamelocation != busname:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   209
                continue # A ne pas remplacer par un message d'erreur
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   210
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   211
            # Extract and check nodeid
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   212
            nodeid = int(loc[1])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   213
            if nodeid not in nodelist.SlaveNodes.keys():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   214
                continue
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   215
            node = nodelist.SlaveNodes[nodeid]["Node"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   216
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   217
            # Extract and check index and subindex
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   218
            index = int(loc[2])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   219
            subindex = int(loc[3])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   220
            if not node.IsEntry(index, subindex):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   221
                continue
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   222
            subentry_infos = node.GetSubentryInfos(index, subindex)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   223
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   224
            if subentry_infos and subentry_infos["pdo"]:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   225
                if sizelocation == "X" and len(loc) > 4:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   226
                    numbit = loc[4]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   227
                elif sizelocation != "X" and len(loc) > 4:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   228
                    continue
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   229
                else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   230
                    numbit = None
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   231
                
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   232
                locationtype = DicoTypes[locationtype]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   233
                entryinfos = node.GetSubentryInfos(index, subindex)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   234
                if entryinfos["type"] != locationtype:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   235
                    raise ValueError, "Invalid type for location \"%s\"" % name
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   236
                
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   237
                typeinfos = node.GetEntryInfos(locationtype)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   238
                DictLocations[name] = {"type":locationtype, "pdotype":SlavePDOType[prefix],
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   239
                                       "nodeid": nodeid, "index": index,"subindex": subindex, 
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   240
                                       "bit": numbit, "size": typeinfos["size"], "busname": busname, "sizelocation": sizelocation}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   241
                  
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   242
    # Create DictCobID with variables already mapped and add them in DictValidLocations
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   243
    for name, locationinfos in DictLocations.items():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   244
        node = nodelist.SlaveNodes[locationinfos["nodeid"]]["Node"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   245
        result = SearchSlavePDOMapping(locationinfos, node)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   246
        if result != None:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   247
            index, subindex = result
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   248
            cobid = nodelist.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 1)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   249
            if cobid not in DictCobID.keys():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   250
                mapping = [None]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   251
                values = node.GetEntry(index)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   252
                for value in values[1:]:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   253
                    mapping.append(value % 0x100)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   254
                DictCobID[cobid] = {"type" : InvertPDOType[locationinfos["pdotype"]], "mapping" : mapping}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   255
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   256
            DictCobID[cobid]["mapping"][subindex] = (locationinfos["type"], name)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   257
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   258
        else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   259
            if locationinfos["nodeid"] not in DictLocationsNotMapped.keys():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   260
                DictLocationsNotMapped[locationinfos["nodeid"]] = {TPDO : [], RPDO : []}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   261
            DictLocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos))
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   262
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   263
    # Check Master Pdo parameters for cobid already used and remove it in ListCobIDAvailable
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   264
    ListPdoParams = [idx for idx in masternode.GetIndexes() if 0x1400 <= idx <= 0x15FF or  0x1800 <= idx <= 0x19FF]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   265
    for idx in ListPdoParams:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   266
        cobid = masternode.GetEntry(idx, 0x01)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   267
        if cobid not in DictCobID.keys():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   268
            ListCobIDAvailable.pop(cobid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   269
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   270
    #-------------------------------------------------------------------------------
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   271
    #                         Build concise DCF for the others locations
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   272
    #-------------------------------------------------------------------------------
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   273
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   274
    for nodeid, locations in DictLocationsNotMapped.items():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   275
        # Get current concise DCF
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   276
        node = nodelist.SlaveNodes[nodeid]["Node"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   277
        nodeDCF = masternode.GetEntry(0x1F22, nodeid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   278
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   279
        if nodeDCF != None and nodeDCF != '':
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   280
            tmpnbparams = [i for i in nodeDCF[:4]]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   281
            tmpnbparams.reverse()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   282
            nbparams = int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   283
            dataparams = nodeDCF[4:]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   284
        else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   285
            nbparams = 0
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   286
            dataparams = ""
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   287
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   288
        for pdotype in (TPDO, RPDO):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   289
            pdosize = 0
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   290
            pdomapping = []
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   291
            for name, loc_infos in locations[pdotype]:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   292
                pdosize += loc_infos["size"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   293
                # If pdo's size > 64 bits
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   294
                if pdosize > 64:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   295
                    result = GetNewCobID(nodeid, pdotype)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   296
                    if result:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   297
                        SlavesPdoNumber[nodeid][pdotype] += 1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   298
                        new_cobid, new_idx = result
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   299
                        data, nbaddedparams = GenerateMappingDCF(new_cobid, new_idx, pdomapping, False)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   300
                        dataparams += data
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   301
                        nbparams += nbaddedparams
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   302
                        DictCobID[new_cobid] = {"type" : InvertPDOType[pdotype], "mapping" : GenerateMasterMapping(pdomapping)}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   303
                    pdosize = loc_infos["size"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   304
                    pdomapping = [(name, loc_infos)]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   305
                else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   306
                    pdomapping.append((name, loc_infos))
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   307
            if len(pdomapping) > 0:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   308
                result = GetNewCobID(nodeid, pdotype)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   309
                if result:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   310
                    SlavesPdoNumber[nodeid][pdotype] += 1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   311
                    new_cobid, new_idx = result
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   312
                    data, nbaddedparams = GenerateMappingDCF(new_cobid, new_idx, pdomapping, False)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   313
                    dataparams += data
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   314
                    nbparams += nbaddedparams
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   315
                    DictCobID[new_cobid] = {"type" : InvertPDOType[pdotype], "mapping" : GenerateMasterMapping(pdomapping)}
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   316
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   317
        dcf = LE_to_BE(nbparams, 0x04) + dataparams
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   318
        masternode.SetEntry(0x1F22, nodeid, dcf)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   319
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   320
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   321
    #-------------------------------------------------------------------------------
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   322
    #                         Master Node Configuration
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   323
    #-------------------------------------------------------------------------------
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   324
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   325
    # Configure Master's SDO parameters entries
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   326
    for nodeid, SDOparams in DictSDOparams.items():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   327
        SdoClient_index = [0x1280 + nodeid]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   328
        manager.ManageEntriesOfCurrent(SdoClient_index,[], masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   329
        if SDOparams["RSDO"] != None:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   330
            RSDO_cobid = SDOparams["RSDO"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   331
        else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   332
            RSDO_cobid = 0x600 + nodeid 
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   333
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   334
        if SDOparams["TSDO"] != None:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   335
            TSDO_cobid = SDOparams["TSDO"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   336
        else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   337
            TSDO_cobid = 0x580 + nodeid
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   338
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   339
        masternode.SetEntry(SdoClient_index[0], 0x01, RSDO_cobid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   340
        masternode.SetEntry(SdoClient_index[0], 0x02, TSDO_cobid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   341
        masternode.SetEntry(SdoClient_index[0], 0x03, nodeid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   342
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   343
    # Configure Master's PDO parameters entries and set cobid, transmit type
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   344
    for cobid, pdo_infos in DictCobID.items():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   345
        current_idx = CurrentPDOParamsIdx[pdo_infos["type"]]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   346
        addinglist = [current_idx, current_idx + 0x200]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   347
        manager.ManageEntriesOfCurrent(addinglist, [], masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   348
        masternode.SetEntry(current_idx, 0x01, cobid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   349
        masternode.SetEntry(current_idx, 0x02, DefaultTransmitTypeMaster)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   350
        if len(pdo_infos["mapping"]) > 2:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   351
            manager.AddSubentriesToCurrent(current_idx + 0x200, len(pdo_infos["mapping"]) - 2, masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   352
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   353
        # Create Master's PDO mapping
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   354
        for subindex, variable in enumerate(pdo_infos["mapping"]):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   355
            if subindex == 0:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   356
                continue
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   357
            new_index = False
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   358
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   359
            if type(variable) != IntType:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   360
                
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   361
                typeidx, varname = variable
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   362
                indexname = DictNameVariable[DictLocations[variable[1]]["pdotype"]][0] + DictLocations[variable[1]]["sizelocation"] + str(DictLocations[variable[1]]["busname"]) + "_" + str(DictLocations[variable[1]]["nodeid"])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   363
                mapvariableidx = DictNameVariable[DictLocations[variable[1]]["pdotype"]][1] +  DictNameVariable[DictLocations[variable[1]]["sizelocation"]] * DictNameVariable["increment"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   364
                
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   365
                if not masternode.IsEntry(mapvariableidx):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   366
                    manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1, masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   367
                    new_index = True
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   368
                    nbsubentries = masternode.GetEntry(mapvariableidx, 0x00)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   369
                else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   370
                    nbsubentries = masternode.GetEntry(mapvariableidx, 0x00)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   371
                    mapvariableidxbase = mapvariableidx 
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   372
                    while mapvariableidx < (mapvariableidxbase + 0x1FF) and nbsubentries == 0xFF:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   373
                        mapvariableidx += 0x800
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   374
                        if not manager.IsCurrentEntry(mapvariableidx):
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   375
                            manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1, masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   376
                            new_index = True
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   377
                        nbsubentries = masternode.GetEntry(mapvariableidx, 0x00)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   378
                
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   379
                if mapvariableidx < 0x6000:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   380
                    if DictLocations[variable[1]]["bit"] != None:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   381
                        subindexname = "_" + str(DictLocations[variable[1]]["index"]) + "_" + str(DictLocations[variable[1]]["subindex"]) + "_" + str(DictLocations[variable[1]]["bit"])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   382
                    else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   383
                        subindexname = "_" + str(DictLocations[variable[1]]["index"]) + "_" + str(DictLocations[variable[1]]["subindex"])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   384
                    if not new_index:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   385
                        manager.AddSubentriesToCurrent(mapvariableidx, 1, masternode)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   386
                        nbsubentries += 1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   387
                    masternode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name" : subindexname})
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   388
                    masternode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type" : typeidx})
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   389
                    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   390
                    # Map Variable
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   391
                    typeinfos = manager.GetEntryInfos(typeidx)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   392
                    if typeinfos != None:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   393
                        value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"]
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   394
                        masternode.SetEntry(current_idx + 0x200, subindex, value)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   395
            else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   396
                masternode.SetEntry(current_idx + 0x200, subindex, TrashVariableValue[variable])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   397
        
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   398
        CurrentPDOParamsIdx[pdo_infos["type"]] += 1
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   399
    #masternode.Print()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   400
    return masternode
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   401
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   402
if __name__ == "__main__":
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   403
    from nodemanager import *
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   404
    from nodelist import *
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   405
    import sys
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   406
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   407
    manager = NodeManager(sys.path[0])
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   408
    nodelist = NodeList(manager)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   409
    result = nodelist.LoadProject("/home/deobox/Desktop/TestMapping")
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   410
   
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   411
##    if result != None:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   412
##        print result
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   413
##    else:
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   414
##        print "MasterNode :"
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   415
##        manager.CurrentNode.Print()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   416
##        for nodeid, node in nodelist.SlaveNodes.items():
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   417
##            print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   418
##            node["Node"].Print()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   419
            
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   420
    #filepath = "/home/deobox/beremiz/test_nodelist/listlocations.txt"
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   421
    filepath = "/home/deobox/Desktop/TestMapping/listlocations.txt"
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   422
    
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   423
    file = open(filepath,'r')
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   424
    locations = [location.split(' ') for location in [line.strip() for line in file.readlines() if len(line) > 0]] 
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   425
    file.close()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   426
    GenerateConciseDCF(locations, 32, nodelist)
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   427
    print "MasterNode :"
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   428
    manager.CurrentNode.Print()
6e9f24fd1b98 Adding Build Method for PLC program generating
lbessard
parents:
diff changeset
   429
    #masternode.Print()