modbus/modbus.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 21 Oct 2022 17:52:45 +0200
branchwxPython4
changeset 3641 d8dc29dfc344
parent 2736 a81b72ef156c
child 3733 d1acf20e8e7c
permissions -rw-r--r--
IDE: fix exception when re-opening previously closed tab
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     1
#!/usr/bin/env python
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     3
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     6
#
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     7
# Copyright (c) 2016 Mario de Sousa (msousa@fe.up.pt)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     8
#
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     9
# This program is free software: you can redistribute it and/or modify
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    10
# it under the terms of the GNU General Public License as published by
2019
92f02bb17c7e Make Modbus extension license compatible with the rest of Beremiz IDE python code : GPLv2.
Edouard Tisserant
parents: 1919
diff changeset
    11
# the Free Software Foundation, either version 2 of the License, or
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    12
# (at your option) any later version.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    13
#
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    14
# This program is distributed in the hope that it will be useful,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    17
# GNU General Public License for more details.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    18
#
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    19
# You should have received a copy of the GNU General Public License
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    21
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    22
# This code is made available on the understanding that it will not be
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    23
# used in safety-critical situations without a full and competent review.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    24
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    25
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    26
from __future__ import absolute_import
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    27
import os
2432
dbc065a2f7a5 python3 support: pylint, W1613 # (xrange-builtin) xrange built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2291
diff changeset
    28
from six.moves import xrange
dbc065a2f7a5 python3 support: pylint, W1613 # (xrange-builtin) xrange built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2291
diff changeset
    29
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    30
from modbus.mb_utils import *
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    31
from ConfigTreeNode import ConfigTreeNode
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    32
from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_MEMORY
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
    33
import util.paths as paths
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    34
2736
a81b72ef156c Add ThirPartyPath call in util.path module, so that individual extensions don't have to each implement same logic to find dependencies
Edouard Tisserant
parents: 2724
diff changeset
    35
ModbusPath = paths.ThirdPartyPath("Modbus")
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    36
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    37
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    38
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    39
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    40
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    41
# C L I E N T    R E Q U E S T            #
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    42
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    43
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    44
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    45
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    46
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    47
class _RequestPlug(object):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    49
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
      <xsd:element name="ModbusRequest">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    51
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    52
          <xsd:attribute name="Function" type="xsd:string" use="optional" default="01 - Read Coils"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
          <xsd:attribute name="SlaveID" use="optional" default="1">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    55
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
                    <xsd:minInclusive value="0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
                    <xsd:maxInclusive value="255"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    58
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    59
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    60
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    61
          <xsd:attribute name="Nr_of_Channels" use="optional" default="1">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    62
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    63
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    64
                    <xsd:minInclusive value="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    65
                    <xsd:maxInclusive value="2000"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    66
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    67
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    68
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    69
          <xsd:attribute name="Start_Address" use="optional" default="0">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    70
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    71
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    72
                    <xsd:minInclusive value="0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    73
                    <xsd:maxInclusive value="65535"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    74
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    75
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    76
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
          <xsd:attribute name="Timeout_in_ms" use="optional" default="10">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    78
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    79
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    80
                    <xsd:minInclusive value="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    81
                    <xsd:maxInclusive value="100000"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    82
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    83
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    84
          </xsd:attribute>
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
    85
          <xsd:attribute name="Write_on_change" type="xsd:boolean" use="optional" default="false"/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    87
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    88
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    90
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    91
    def GetParamsAttributes(self, path=None):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    92
        infos = ConfigTreeNode.GetParamsAttributes(self, path=path)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    93
        for element in infos:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    94
            if element["name"] == "ModbusRequest":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    95
                for child in element["children"]:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    96
                    if child["name"] == "Function":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    97
                        list = modbus_function_dict.keys()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    98
                        list.sort()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    99
                        child["type"] = list
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   100
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   101
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   102
    def GetVariableLocationTree(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   103
        current_location = self.GetCurrentLocation()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   104
        name = self.BaseParams.getName()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   105
        address = self.GetParamsAttributes()[0]["children"][3]["value"]
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   106
        count = self.GetParamsAttributes()[0]["children"][2]["value"]
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   107
        function = self.GetParamsAttributes()[0]["children"][0]["value"]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   108
        # 'BOOL' or 'WORD'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   109
        datatype = modbus_function_dict[function][3]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   110
        # 1 or 16
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   111
        datasize = modbus_function_dict[function][4]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   112
        # 'Q' for coils and holding registers, 'I' for input discretes and input registers
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   113
        # datazone = modbus_function_dict[function][5]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   114
        # 'X' for bits, 'W' for words
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   115
        datatacc = modbus_function_dict[function][6]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   116
        # 'Coil', 'Holding Register', 'Input Discrete' or 'Input Register'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   117
        dataname = modbus_function_dict[function][7]
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   118
        # start off with a boolean entry
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   119
        # This is a flag used to allow the user program to control when to 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   120
        # execute the Modbus request.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   121
        # NOTE: If the Modbus request has a 'current_location' of
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   122
        #          %QX1.2.3
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   123
        #       then the execution control flag will be
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   124
        #          %QX1.2.3.0.0
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   125
        #       and all the Modbus registers/coils will be
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   126
        #          %QX1.2.3.0
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   127
        #          %QX1.2.3.1
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   128
        #          %QX1.2.3.2
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   129
        #            ..
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   130
        #          %QX1.2.3.n
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   131
        entries = []
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   132
        entries.append({
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
   133
            "name": "Execute request flag",
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   134
            "type": LOCATION_VAR_MEMORY,
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   135
            "size": 1,           # BOOL flag
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   136
            "IEC_type": "BOOL",  # BOOL flag
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   137
            "var_name": "var_name",
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   138
            "location": "X" + ".".join([str(i) for i in current_location]) + ".0.0",
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
   139
            "description": "Modbus request execution control flag",
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
   140
            "children": []})        
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
   141
        entries.append({
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   142
            "name": "Modbus Request Status flag",
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
   143
            "type": LOCATION_VAR_MEMORY,
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   144
            "size": 8,           # BYTE flag
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   145
            "IEC_type": "BYTE",  # BYTE flag
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
   146
            "var_name": "var_name",
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   147
            "location": "B" + ".".join([str(i) for i in current_location]) + ".0.1",
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
   148
            "description": "Modbus request status flag (0 -> OK, 1 -> Network error, 2 -> Received invalid frame, 3 -> Timeout, 4 -> Received error frame)",
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   149
            "children": []})        
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   150
        entries.append({
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   151
            "name": "Modbus Error Code",
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   152
            "type": LOCATION_VAR_MEMORY,
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   153
            "size": 8,           # BYTE flag
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   154
            "IEC_type": "BYTE",  # BYTE flag
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   155
            "var_name": "var_name",
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   156
            "location": "B" + ".".join([str(i) for i in current_location]) + ".0.2",
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   157
            "description": "Modbus Error Code received in Modbus error frame",
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
   158
            "children": []})        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   159
        for offset in range(address, address + count):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   160
            entries.append({
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   161
                "name": dataname + " " + str(offset),
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   162
                "type": LOCATION_VAR_MEMORY,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   163
                "size": datasize,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   164
                "IEC_type": datatype,
2589
d94d4f396bbf Nicer name than 'var_name' for variable auto-renaming in Modbus
Edouard Tisserant
parents: 2459
diff changeset
   165
                "var_name": "MB_" + "".join([w[0] for w in dataname.split()]) + "_" + str(offset),
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   166
                "location": datatacc + ".".join([str(i) for i in current_location]) + "." + str(offset),
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   167
                "description": "description",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   168
                "children": []})
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   169
        return {"name": name,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   170
                "type": LOCATION_CONFNODE,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   171
                "location": ".".join([str(i) for i in current_location]) + ".x",
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   172
                "children": entries}
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   173
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   174
    def CTNGenerate_C(self, buildpath, locations):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   175
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   176
        Generate C code
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   177
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   178
        @param locations: List of complete variables locations \
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   179
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   180
            "NAME" : name of the variable (generally "__IW0_1_2" style)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   181
            "DIR" : direction "Q","I" or "M"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   182
            "SIZE" : size "X", "B", "W", "D", "L"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   183
            "LOC" : tuple of interger for IEC location (0,1,2,...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   184
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   185
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   186
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   187
        return [], "", False
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   188
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   189
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
   190
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   191
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   192
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   193
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   194
# S E R V E R    M E M O R Y    A R E A       #
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   195
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   196
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   197
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   198
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   199
# dictionary implementing:
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   200
# key - string with the description we want in the request plugin GUI
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   201
# list - (modbus function number, request type, max count value)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   202
modbus_memtype_dict = {
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   203
    "01 - Coils":            ('1', 'rw_bits',  65536, "BOOL", 1, "Q", "X", "Coil"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   204
    "02 - Input Discretes":  ('2', 'ro_bits',  65536, "BOOL", 1, "I", "X", "Input Discrete"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   205
    "03 - Holding Registers": ('3', 'rw_words', 65536, "WORD", 16, "Q", "W", "Holding Register"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   206
    "04 - Input Registers":  ('4', 'ro_words', 65536, "WORD", 16, "I", "W", "Input Register"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   207
}
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   208
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   209
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   210
class _MemoryAreaPlug(object):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   211
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   212
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   213
      <xsd:element name="MemoryArea">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   214
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   215
          <xsd:attribute name="MemoryAreaType" type="xsd:string" use="optional" default="01 - Coils"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   216
          <xsd:attribute name="Nr_of_Channels" use="optional" default="1">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   217
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   218
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   219
                    <xsd:minInclusive value="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   220
                    <xsd:maxInclusive value="65536"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   221
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   222
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   223
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   224
          <xsd:attribute name="Start_Address" use="optional" default="0">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   225
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   226
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   227
                    <xsd:minInclusive value="0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   228
                    <xsd:maxInclusive value="65535"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   229
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   230
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   231
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   232
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   233
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   234
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   235
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   236
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   237
    def GetParamsAttributes(self, path=None):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   238
        infos = ConfigTreeNode.GetParamsAttributes(self, path=path)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   239
        for element in infos:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   240
            if element["name"] == "MemoryArea":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   241
                for child in element["children"]:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   242
                    if child["name"] == "MemoryAreaType":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   243
                        list = modbus_memtype_dict.keys()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   244
                        list.sort()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   245
                        child["type"] = list
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   246
        return infos
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   247
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   248
    def GetVariableLocationTree(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   249
        current_location = self.GetCurrentLocation()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   250
        name = self.BaseParams.getName()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   251
        address = self.GetParamsAttributes()[0]["children"][2]["value"]
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   252
        count = self.GetParamsAttributes()[0]["children"][1]["value"]
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   253
        function = self.GetParamsAttributes()[0]["children"][0]["value"]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   254
        # 'BOOL' or 'WORD'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   255
        datatype = modbus_memtype_dict[function][3]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   256
        # 1 or 16
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   257
        datasize = modbus_memtype_dict[function][4]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   258
        # 'Q' for coils and holding registers, 'I' for input discretes and input registers
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   259
        # datazone = modbus_memtype_dict[function][5]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   260
        # 'X' for bits, 'W' for words
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   261
        datatacc = modbus_memtype_dict[function][6]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   262
        # 'Coil', 'Holding Register', 'Input Discrete' or 'Input Register'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   263
        dataname = modbus_memtype_dict[function][7]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   264
        entries = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   265
        for offset in range(address, address + count):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   266
            entries.append({
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   267
                "name": dataname + " " + str(offset),
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   268
                "type": LOCATION_VAR_MEMORY,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   269
                "size": datasize,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   270
                "IEC_type": datatype,
2589
d94d4f396bbf Nicer name than 'var_name' for variable auto-renaming in Modbus
Edouard Tisserant
parents: 2459
diff changeset
   271
                "var_name": "MB_" + "".join([w[0] for w in dataname.split()]) + "_" + str(offset),
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   272
                "location": datatacc + ".".join([str(i) for i in current_location]) + "." + str(offset),
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   273
                "description": "description",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   274
                "children": []})
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   275
        return {"name": name,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   276
                "type": LOCATION_CONFNODE,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   277
                "location": ".".join([str(i) for i in current_location]) + ".x",
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   278
                "children": entries}
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   279
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   280
    def CTNGenerate_C(self, buildpath, locations):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   281
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   282
        Generate C code
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   283
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   284
        @param locations: List of complete variables locations \
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   285
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   286
            "NAME" : name of the variable (generally "__IW0_1_2" style)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   287
            "DIR" : direction "Q","I" or "M"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   288
            "SIZE" : size "X", "B", "W", "D", "L"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   289
            "LOC" : tuple of interger for IEC location (0,1,2,...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   290
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   291
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   292
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   293
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   294
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   295
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   296
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   297
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   298
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   299
# T C P    C L I E N T                 #
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   300
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   301
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   302
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   303
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   304
# XXX TODO "Configuration_Name" should disapear in favor of CTN Name, which is already unique
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   305
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   306
class _ModbusTCPclientPlug(object):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   307
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   308
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   309
      <xsd:element name="ModbusTCPclient">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   310
        <xsd:complexType>
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   311
          <xsd:attribute name="Configuration_Name" type="xsd:string" use="optional" default=""/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   312
          <xsd:attribute name="Remote_IP_Address" type="xsd:string" use="optional" default="localhost"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   313
          <xsd:attribute name="Remote_Port_Number" type="xsd:string" use="optional" default="502"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   314
          <xsd:attribute name="Invocation_Rate_in_ms" use="optional" default="100">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   315
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   316
                <xsd:restriction base="xsd:unsignedLong">
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   317
                    <xsd:minInclusive value="0"/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   318
                    <xsd:maxInclusive value="2147483647"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   319
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   320
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   321
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   322
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   323
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   324
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   325
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   326
    # NOTE: Max value of 2147483647 (i32_max) for Invocation_Rate_in_ms
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   327
    # corresponds to aprox 25 days.
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   328
    CTNChildrenTypes = [("ModbusRequest", _RequestPlug, "Request")]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   329
    # TODO: Replace with CTNType !!!
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   330
    PlugType = "ModbusTCPclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   331
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   332
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   333
    def __init__(self):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   334
        # NOTE:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   335
        # The ModbusTCPclient attribute is added dynamically by ConfigTreeNode._AddParamsMembers()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   336
        # It will be an XML parser object created by
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   337
        # GenerateParserFromXSDstring(self.XSD).CreateRoot()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   338
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   339
        # Set the default value for the "Configuration_Name" parameter
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   340
        # The default value will need to be different for each instance of the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   341
        # _ModbusTCPclientPlug class, so we cannot hardcode the default value in the XSD above
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   342
        # This value will be used by the web interface 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   343
        #   (i.e. the extension to the web server used to configure the Modbus parameters).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   344
        #   (The web server is run/activated/started by Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   345
        #   (The web server code is found in runtime/NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   346
        #   (The Modbus extension to the web server is found in runtime/Modbus_config.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   347
        loc_str = ".".join(map(str, self.GetCurrentLocation()))
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   348
        self.ModbusTCPclient.setConfiguration_Name("Modbus TCP Client " + loc_str)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   349
        
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   350
    # Return the number of (modbus library) nodes this specific TCP client will need
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   351
    #   return type: (tcp nodes, rtu nodes, ascii nodes)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   352
    def GetNodeCount(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   353
        return (1, 0, 0)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   354
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   355
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   356
        """ Return the node's Configuration_Name """
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   357
        return self.ModbusTCPclient.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   358
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   359
    def CTNGenerate_C(self, buildpath, locations):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   360
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   361
        Generate C code
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   362
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   363
        @param locations: List of complete variables locations \
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   364
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   365
            "NAME" : name of the variable (generally "__IW0_1_2" style)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   366
            "DIR" : direction "Q","I" or "M"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   367
            "SIZE" : size "X", "B", "W", "D", "L"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   368
            "LOC" : tuple of interger for IEC location (0,1,2,...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   369
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   370
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   371
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   372
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   373
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   374
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   375
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   376
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   377
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   378
# T C P    S E R V E R                 #
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   379
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   380
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   381
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   382
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   383
# XXX TODO "Configuration_Name" should disapear in favor of CTN Name, which is already unique
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   384
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   385
class _ModbusTCPserverPlug(object):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   386
    # NOTE: the Port number is a 'string' and not an 'integer'!
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   387
    # This is because the underlying modbus library accepts strings
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   388
    # (e.g.: well known port names!)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   389
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   390
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   391
      <xsd:element name="ModbusServerNode">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   392
        <xsd:complexType>
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   393
          <xsd:attribute name="Configuration_Name" type="xsd:string" use="optional" default=""/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   394
          <xsd:attribute name="Local_IP_Address" type="xsd:string" use="optional"  default="#ANY#"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   395
          <xsd:attribute name="Local_Port_Number" type="xsd:string" use="optional" default="502"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   396
          <xsd:attribute name="SlaveID" use="optional" default="0">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   397
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   398
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   399
                    <xsd:minInclusive value="0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   400
                    <xsd:maxInclusive value="255"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   401
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   402
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   403
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   404
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   405
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   406
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   407
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   408
    CTNChildrenTypes = [("MemoryArea", _MemoryAreaPlug, "Memory Area")]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   409
    # TODO: Replace with CTNType !!!
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   410
    PlugType = "ModbusTCPserver"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   411
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   412
    def __init__(self):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   413
        # NOTE:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   414
        # The ModbusServerNode attribute is added dynamically by ConfigTreeNode._AddParamsMembers()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   415
        # It will be an XML parser object created by
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   416
        # GenerateParserFromXSDstring(self.XSD).CreateRoot()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   417
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   418
        # Set the default value for the "Configuration_Name" parameter
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   419
        # The default value will need to be different for each instance of the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   420
        # _ModbusTCPclientPlug class, so we cannot hardcode the default value in the XSD above
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   421
        # This value will be used by the web interface 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   422
        #   (i.e. the extension to the web server used to configure the Modbus parameters).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   423
        #   (The web server is run/activated/started by Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   424
        #   (The web server code is found in runtime/NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   425
        #   (The Modbus extension to the web server is found in runtime/Modbus_config.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   426
        loc_str = ".".join(map(str, self.GetCurrentLocation()))
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   427
        self.ModbusServerNode.setConfiguration_Name("Modbus TCP Server " + loc_str)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   428
        
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   429
    # Return the number of (modbus library) nodes this specific TCP server will need
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   430
    #   return type: (tcp nodes, rtu nodes, ascii nodes)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   431
    def GetNodeCount(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   432
        return (1, 0, 0)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   433
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   434
    # Return a list with a single tuple conatining the (location, IP address, port number)
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   435
    #     location   : location of this node in the configuration tree
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   436
    #     port number: IP port used by this Modbus/IP server
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   437
    #     IP address : IP address of the network interface on which the server will be listening
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   438
    #                  ("", "*", or "#ANY#" => listening on all interfaces!)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   439
    def GetIPServerPortNumbers(self):
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   440
        port = self.ModbusServerNode.getLocal_Port_Number()
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   441
        addr = self.ModbusServerNode.getLocal_IP_Address()
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   442
        return [(self.GetCurrentLocation(), addr, port)]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   443
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   444
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   445
        """ Return the node's Configuration_Name """
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   446
        return self.ModbusServerNode.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   447
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   448
    def GetVariableLocationTree(self):
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   449
        current_location = self.GetCurrentLocation()
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   450
        name             = self.BaseParams.getName()
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   451
        # start off with flags that count the number of Modbus requests/transactions
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   452
        # handled by this Modbus server/slave.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   453
        # These flags are mapped onto located variables and therefore available to the user programs
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   454
        # May be used to detect communication errors.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   455
        # execute the Modbus request.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   456
        # NOTE: If the Modbus slave has a 'current_location' of
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   457
        #          %QX1.2
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   458
        #       then the "Modbus Read Request Counter"  will be %MD1.2.0
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   459
        #       then the "Modbus Write Request Counter" will be %MD1.2.1
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   460
        #       then the "Modbus Read Request Flag"     will be %MD1.2.2
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   461
        #       then the "Modbus Write Request Flag"    will be %MD1.2.3
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   462
        #
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   463
        # Note that any MemoryArea contained under this server/slave
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   464
        # will ocupy the locations of type
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   465
        #          %MX or %MW
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   466
        # which will never clash with the %MD used here.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   467
        # Additionaly, any MemoryArea contained under this server/slave
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   468
        # will ocupy locations with
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   469
        #           %M1.2.a.b (with a and b being numbers in range 0, 1, ...)
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   470
        # and therefore never ocupy the locations
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   471
        #           %M1.2.0
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   472
        #           %M1.2.1
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   473
        #           %M1.2.2
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   474
        #           %M1.2.3
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   475
        # used by the following flags/counters.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   476
        entries = []
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   477
        entries.append({
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   478
            "name": "Modbus Read Request Counter",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   479
            "type": LOCATION_VAR_MEMORY,
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   480
            "size": 32,           # UDINT flag
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   481
            "IEC_type": "UDINT",  # UDINT flag
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   482
            "var_name": "var_name",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   483
            "location": "D" + ".".join([str(i) for i in current_location]) + ".0",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   484
            "description": "Modbus read request counter",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   485
            "children": []})        
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   486
        entries.append({
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   487
            "name": "Modbus Write Request Counter",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   488
            "type": LOCATION_VAR_MEMORY,
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   489
            "size": 32,           # UDINT flag
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   490
            "IEC_type": "UDINT",  # UDINT flag
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   491
            "var_name": "var_name",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   492
            "location": "D" + ".".join([str(i) for i in current_location]) + ".1",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   493
            "description": "Modbus write request counter",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   494
            "children": []})        
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   495
        entries.append({
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   496
            "name": "Modbus Read Request Flag",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   497
            "type": LOCATION_VAR_MEMORY,
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   498
            "size": 1,            # BOOL flag
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   499
            "IEC_type": "BOOL",   # BOOL flag
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   500
            "var_name": "var_name",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   501
            "location": "X" + ".".join([str(i) for i in current_location]) + ".2",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   502
            "description": "Modbus read request flag",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   503
            "children": []})        
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   504
        entries.append({
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   505
            "name": "Modbus write Request Flag",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   506
            "type": LOCATION_VAR_MEMORY,
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   507
            "size": 1,            # BOOL flag
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   508
            "IEC_type": "BOOL",   # BOOL flag
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   509
            "var_name": "var_name",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   510
            "location": "X" + ".".join([str(i) for i in current_location]) + ".3",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   511
            "description": "Modbus write request flag",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   512
            "children": []})        
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   513
        # recursively call all the Memory Areas under this Modbus server/save
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   514
        # i.e., all the children objects which will be of class _MemoryAreaPlug
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   515
        for child in self.IECSortedChildren():
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   516
            entries.append(child.GetVariableLocationTree())
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   517
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   518
        return {"name": name,
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   519
                "type": LOCATION_CONFNODE,
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   520
                "location": ".".join([str(i) for i in current_location]) + ".x",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   521
                "children": entries}
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   522
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   523
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   524
    def CTNGenerate_C(self, buildpath, locations):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   525
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   526
        Generate C code
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   527
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   528
        @param locations: List of complete variables locations \
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   529
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   530
            "NAME" : name of the variable (generally "__IW0_1_2" style)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   531
            "DIR" : direction "Q","I" or "M"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   532
            "SIZE" : size "X", "B", "W", "D", "L"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   533
            "LOC" : tuple of interger for IEC location (0,1,2,...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   534
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   535
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   536
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   537
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   538
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   539
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   540
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   541
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   542
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   543
# R T U    C L I E N T                 #
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   544
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   545
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   546
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   547
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   548
# XXX TODO "Configuration_Name" should disapear in favor of CTN Name, which is already unique
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   549
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   550
class _ModbusRTUclientPlug(object):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   551
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   552
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   553
      <xsd:element name="ModbusRTUclient">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   554
        <xsd:complexType>
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   555
          <xsd:attribute name="Configuration_Name" type="xsd:string" use="optional" default=""/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   556
          <xsd:attribute name="Serial_Port" type="xsd:string"  use="optional" default="/dev/ttyS0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   557
          <xsd:attribute name="Baud_Rate"   type="xsd:string"  use="optional" default="9600"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   558
          <xsd:attribute name="Parity"      type="xsd:string"  use="optional" default="even"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   559
          <xsd:attribute name="Stop_Bits"   type="xsd:string"  use="optional" default="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   560
          <xsd:attribute name="Invocation_Rate_in_ms" use="optional" default="100">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   561
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   562
                <xsd:restriction base="xsd:integer">
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   563
                    <xsd:minInclusive value="0"/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   564
                    <xsd:maxInclusive value="2147483647"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   565
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   566
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   567
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   568
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   569
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   570
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   571
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   572
    # NOTE: Max value of 2147483647 (i32_max) for Invocation_Rate_in_ms
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   573
    # corresponds to aprox 25 days.
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   574
    CTNChildrenTypes = [("ModbusRequest", _RequestPlug, "Request")]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   575
    # TODO: Replace with CTNType !!!
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   576
    PlugType = "ModbusRTUclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   577
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   578
    def __init__(self):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   579
        # NOTE:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   580
        # The ModbusRTUclient attribute is added dynamically by ConfigTreeNode._AddParamsMembers()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   581
        # It will be an XML parser object created by
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   582
        # GenerateParserFromXSDstring(self.XSD).CreateRoot()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   583
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   584
        # Set the default value for the "Configuration_Name" parameter
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   585
        # The default value will need to be different for each instance of the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   586
        # _ModbusTCPclientPlug class, so we cannot hardcode the default value in the XSD above
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   587
        # This value will be used by the web interface 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   588
        #   (i.e. the extension to the web server used to configure the Modbus parameters).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   589
        #   (The web server is run/activated/started by Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   590
        #   (The web server code is found in runtime/NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   591
        #   (The Modbus extension to the web server is found in runtime/Modbus_config.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   592
        loc_str = ".".join(map(str, self.GetCurrentLocation()))
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   593
        self.ModbusRTUclient.setConfiguration_Name("Modbus RTU Client " + loc_str)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   594
        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   595
    def GetParamsAttributes(self, path=None):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   596
        infos = ConfigTreeNode.GetParamsAttributes(self, path=path)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   597
        for element in infos:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   598
            if element["name"] == "ModbusRTUclient":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   599
                for child in element["children"]:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   600
                    if child["name"] == "Baud_Rate":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   601
                        child["type"] = modbus_serial_baudrate_list
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   602
                    if child["name"] == "Stop_Bits":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   603
                        child["type"] = modbus_serial_stopbits_list
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   604
                    if child["name"] == "Parity":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   605
                        child["type"] = modbus_serial_parity_dict.keys()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   606
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   607
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   608
    # Return the number of (modbus library) nodes this specific RTU client will need
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   609
    #   return type: (tcp nodes, rtu nodes, ascii nodes)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   610
    def GetNodeCount(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   611
        return (0, 1, 0)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   612
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   613
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   614
        """ Return the node's Configuration_Name """
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   615
        return self.ModbusRTUclient.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   616
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   617
    def CTNGenerate_C(self, buildpath, locations):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   618
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   619
        Generate C code
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   620
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   621
        @param locations: List of complete variables locations \
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   622
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   623
            "NAME" : name of the variable (generally "__IW0_1_2" style)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   624
            "DIR" : direction "Q","I" or "M"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   625
            "SIZE" : size "X", "B", "W", "D", "L"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   626
            "LOC" : tuple of interger for IEC location (0,1,2,...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   627
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   628
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   629
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   630
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   631
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   632
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   633
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   634
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   635
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   636
# R T U    S L A V E                   #
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   637
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   638
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   639
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   640
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   641
# XXX TODO "Configuration_Name" should disapear in favor of CTN Name, which is already unique
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   642
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   643
class _ModbusRTUslavePlug(object):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   644
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   645
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   646
      <xsd:element name="ModbusRTUslave">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   647
        <xsd:complexType>
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   648
          <xsd:attribute name="Configuration_Name" type="xsd:string" use="optional" default=""/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   649
          <xsd:attribute name="Serial_Port" type="xsd:string"  use="optional" default="/dev/ttyS0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   650
          <xsd:attribute name="Baud_Rate"   type="xsd:string"  use="optional" default="9600"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   651
          <xsd:attribute name="Parity"      type="xsd:string"  use="optional" default="even"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   652
          <xsd:attribute name="Stop_Bits"   type="xsd:string"  use="optional" default="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   653
          <xsd:attribute name="SlaveID" use="optional" default="1">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   654
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   655
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   656
                    <xsd:minInclusive value="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   657
                    <xsd:maxInclusive value="255"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   658
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   659
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   660
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   661
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   662
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   663
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   664
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   665
    CTNChildrenTypes = [("MemoryArea", _MemoryAreaPlug, "Memory Area")]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   666
    # TODO: Replace with CTNType !!!
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   667
    PlugType = "ModbusRTUslave"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   668
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   669
    def __init__(self):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   670
        # NOTE:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   671
        # The ModbusRTUslave attribute is added dynamically by ConfigTreeNode._AddParamsMembers()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   672
        # It will be an XML parser object created by
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   673
        # GenerateParserFromXSDstring(self.XSD).CreateRoot()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   674
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   675
        # Set the default value for the "Configuration_Name" parameter
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   676
        # The default value will need to be different for each instance of the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   677
        # _ModbusTCPclientPlug class, so we cannot hardcode the default value in the XSD above
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   678
        # This value will be used by the web interface 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   679
        #   (i.e. the extension to the web server used to configure the Modbus parameters).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   680
        #   (The web server is run/activated/started by Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   681
        #   (The web server code is found in runtime/NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   682
        #   (The Modbus extension to the web server is found in runtime/Modbus_config.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   683
        loc_str = ".".join(map(str, self.GetCurrentLocation()))
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   684
        self.ModbusRTUslave.setConfiguration_Name("Modbus RTU Slave " + loc_str)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   685
        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   686
    def GetParamsAttributes(self, path=None):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   687
        infos = ConfigTreeNode.GetParamsAttributes(self, path=path)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   688
        for element in infos:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   689
            if element["name"] == "ModbusRTUslave":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   690
                for child in element["children"]:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   691
                    if child["name"] == "Baud_Rate":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   692
                        child["type"] = modbus_serial_baudrate_list
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   693
                    if child["name"] == "Stop_Bits":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   694
                        child["type"] = modbus_serial_stopbits_list
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   695
                    if child["name"] == "Parity":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   696
                        child["type"] = modbus_serial_parity_dict.keys()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   697
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   698
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   699
    # Return the number of (modbus library) nodes this specific RTU slave will need
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   700
    #   return type: (tcp nodes, rtu nodes, ascii nodes)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   701
    def GetNodeCount(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   702
        return (0, 1, 0)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   703
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   704
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   705
        """ Return the node's Configuration_Name """
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   706
        return self.ModbusRTUslave.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   707
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   708
    def GetVariableLocationTree(self):
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   709
        current_location = self.GetCurrentLocation()
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   710
        name             = self.BaseParams.getName()
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   711
        # start off with flags that count the number of Modbus requests/transactions
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   712
        # handled by this Modbus server/slave.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   713
        # These flags are mapped onto located variables and therefore available to the user programs
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   714
        # May be used to detect communication errors.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   715
        # execute the Modbus request.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   716
        # NOTE: If the Modbus slave has a 'current_location' of
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   717
        #          %QX1.2
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   718
        #       then the "Modbus Read Request Counter"  will be %MD1.2.0
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   719
        #       then the "Modbus Write Request Counter" will be %MD1.2.1
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   720
        #       then the "Modbus Read Request Flag"     will be %MD1.2.2
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   721
        #       then the "Modbus Write Request Flag"    will be %MD1.2.3
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   722
        #
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   723
        # Note that any MemoryArea contained under this server/slave
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   724
        # will ocupy the locations of type
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   725
        #          %MX or %MW
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   726
        # which will never clash with the %MD used here.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   727
        # Additionaly, any MemoryArea contained under this server/slave
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   728
        # will ocupy locations with
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   729
        #           %M1.2.a.b (with a and b being numbers in range 0, 1, ...)
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   730
        # and therefore never ocupy the locations
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   731
        #           %M1.2.0
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   732
        #           %M1.2.1
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   733
        #           %M1.2.2
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   734
        #           %M1.2.3
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   735
        # used by the following flags/counters.
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   736
        entries = []
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   737
        entries.append({
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   738
            "name": "Modbus Read Request Counter",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   739
            "type": LOCATION_VAR_MEMORY,
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   740
            "size": 32,           # UDINT flag
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   741
            "IEC_type": "UDINT",  # UDINT flag
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   742
            "var_name": "var_name",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   743
            "location": "D" + ".".join([str(i) for i in current_location]) + ".0",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   744
            "description": "Modbus read request counter",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   745
            "children": []})        
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   746
        entries.append({
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   747
            "name": "Modbus Write Request Counter",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   748
            "type": LOCATION_VAR_MEMORY,
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   749
            "size": 32,           # UDINT flag
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   750
            "IEC_type": "UDINT",  # UDINT flag
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   751
            "var_name": "var_name",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   752
            "location": "D" + ".".join([str(i) for i in current_location]) + ".1",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   753
            "description": "Modbus write request counter",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   754
            "children": []})        
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   755
        entries.append({
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   756
            "name": "Modbus Read Request Flag",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   757
            "type": LOCATION_VAR_MEMORY,
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   758
            "size": 1,            # BOOL flag
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   759
            "IEC_type": "BOOL",   # BOOL flag
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   760
            "var_name": "var_name",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   761
            "location": "X" + ".".join([str(i) for i in current_location]) + ".2",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   762
            "description": "Modbus read request flag",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   763
            "children": []})        
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   764
        entries.append({
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   765
            "name": "Modbus write Request Flag",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   766
            "type": LOCATION_VAR_MEMORY,
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   767
            "size": 1,            # BOOL flag
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   768
            "IEC_type": "BOOL",   # BOOL flag
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   769
            "var_name": "var_name",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   770
            "location": "X" + ".".join([str(i) for i in current_location]) + ".3",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   771
            "description": "Modbus write request flag",
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   772
            "children": []})        
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   773
        # recursively call all the Memory Areas under this Modbus server/save
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   774
        # i.e., all the children objects which will be of class _MemoryAreaPlug
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   775
        for child in self.IECSortedChildren():
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   776
            entries.append(child.GetVariableLocationTree())
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   777
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   778
        return {"name": name,
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   779
                "type": LOCATION_CONFNODE,
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   780
                "location": ".".join([str(i) for i in current_location]) + ".x",
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   781
                "children": entries}
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   782
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   783
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   784
    def CTNGenerate_C(self, buildpath, locations):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   785
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   786
        Generate C code
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   787
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   788
        @param locations: List of complete variables locations \
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   789
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   790
            "NAME" : name of the variable (generally "__IW0_1_2" style)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   791
            "DIR" : direction "Q","I" or "M"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   792
            "SIZE" : size "X", "B", "W", "D", "L"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   793
            "LOC" : tuple of interger for IEC location (0,1,2,...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   794
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   795
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   796
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   797
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   798
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   799
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   800
def _lt_to_str(loctuple):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   801
    return '.'.join(map(str, loctuple))
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   802
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   803
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   804
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   805
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   806
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   807
# R O O T    C L A S S                #
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   808
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   809
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   810
#
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   811
class RootClass(object):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   812
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   813
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   814
      <xsd:element name="ModbusRoot">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   815
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   816
          <xsd:attribute name="MaxRemoteTCPclients" use="optional" default="10">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   817
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   818
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   819
                    <xsd:minInclusive value="0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   820
                    <xsd:maxInclusive value="65535"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   821
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   822
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   823
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   824
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   825
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   826
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   827
    """
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   828
    CTNChildrenTypes = [("ModbusTCPclient", _ModbusTCPclientPlug, "Modbus TCP Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   829
                        ("ModbusTCPserver", _ModbusTCPserverPlug, "Modbus TCP Server"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   830
                        ("ModbusRTUclient", _ModbusRTUclientPlug, "Modbus RTU Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   831
                        ("ModbusRTUslave", _ModbusRTUslavePlug,  "Modbus RTU Slave")]
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   832
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   833
    # Return the number of (modbus library) nodes this specific instance of the modbus plugin will need
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   834
    #   return type: (tcp nodes, rtu nodes, ascii nodes)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   835
    def GetNodeCount(self):
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   836
        max_remote_tcpclient = self.GetParamsAttributes()[
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   837
            0]["children"][0]["value"]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   838
        total_node_count = (max_remote_tcpclient, 0, 0)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   839
        for child in self.IECSortedChildren():
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   840
            # ask each child how many nodes it needs, and add them all up.
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   841
            total_node_count = tuple(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   842
                x1 + x2 for x1, x2 in zip(total_node_count, child.GetNodeCount()))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   843
        return total_node_count
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   844
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   845
    # Return a list with tuples of the (location, port numbers) used by all the Modbus/IP servers
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   846
    def GetIPServerPortNumbers(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   847
        IPServer_port_numbers = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   848
        for child in self.IECSortedChildren():
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   849
            if child.CTNType == "ModbusTCPserver":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   850
                IPServer_port_numbers.extend(child.GetIPServerPortNumbers())
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   851
        return IPServer_port_numbers
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   852
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   853
    # Return a list with tuples of the (location, configuration_name) used by all the Modbus nodes (tcp/rtu, clients/servers)
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   854
    def GetConfigNames(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   855
        Node_Configuration_Names = []
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   856
        for child in self.IECSortedChildren():
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   857
            Node_Configuration_Names.extend([(child.GetCurrentLocation(), child.GetConfigName())])
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   858
        return Node_Configuration_Names
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   859
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   860
    def CTNGenerate_C(self, buildpath, locations):
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   861
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   862
        # print self.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   863
        # print type(self)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   864
        # print "self.CTNType >>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   865
        # print self.CTNType
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   866
        # print "type(self.CTNType) >>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   867
        # print type(self.CTNType)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   868
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   869
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   870
        loc_dict = {"locstr": "_".join(map(str, self.GetCurrentLocation()))}
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   871
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   872
        # Determine the number of (modbus library) nodes ALL instances of the modbus plugin will need
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   873
        #   total_node_count: (tcp nodes, rtu nodes, ascii nodes)
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   874
        #
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   875
        # Also get a list with tuples of (location, IP address, port number) used by all the Modbus/IP server nodes
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   876
        #   This list is later used to search for duplicates in port numbers!
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   877
        #   IPServer_port_numbers = [(location, IP address, port number), ...]
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   878
        #       location            : tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   879
        #       IPserver_port_number: a number (i.e. port number used by the Modbus/IP server)
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   880
        #       IP address          : IP address of the network interface on which the server will be listening
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   881
        #                             ("", "*", or "#ANY#" => listening on all interfaces!)
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   882
        #
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   883
        # Also get a list with tuples of (location, Configuration_Name) used by all the Modbus nodes
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   884
        #   This list is later used to search for duplicates in Configuration Names!
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   885
        #   Node_Configuration_Names = [(location, Configuration_Name), ...]
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   886
        #       location          : tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   887
        #       Configuration_Name: the "Configuration_Name" string
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   888
        total_node_count = (0, 0, 0)
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   889
        IPServer_port_numbers    = []
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   890
        Node_Configuration_Names = []
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   891
        for CTNInstance in self.GetCTRoot().IterChildren():
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   892
            if CTNInstance.CTNType == "modbus":
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   893
                # ask each modbus plugin instance how many nodes it needs, and add them all up.
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   894
                total_node_count = tuple(x1 + x2 for x1, x2 in zip(total_node_count, CTNInstance.GetNodeCount()))
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   895
                IPServer_port_numbers.   extend(CTNInstance.GetIPServerPortNumbers())
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   896
                Node_Configuration_Names.extend(CTNInstance.GetConfigNames        ())
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   897
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   898
        # Search for use of duplicate Configuration_Names by Modbus nodes
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   899
        # Configuration Names are used by the web server running on the PLC
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   900
        # (more precisely, run by Beremiz_service.py) to identify and allow 
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   901
        # changing the Modbus parameters after the program has been downloaded 
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   902
        # to the PLC (but before it is started)
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   903
        # With clashes in the configuration names, the Modbus nodes will not be
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   904
        # distinguasheble on the web interface!
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   905
        for i in range(0, len(Node_Configuration_Names) - 1):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   906
            for j in range(i + 1, len(Node_Configuration_Names)):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   907
                if Node_Configuration_Names[i][1] == Node_Configuration_Names[j][1]:
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   908
                    error_message = _("Error: Modbus plugin nodes %{a1}.x and %{a2}.x use the same Configuration_Name \"{a3}\".\n").format(
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   909
                                        a1=_lt_to_str(Node_Configuration_Names[i][0]),
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   910
                                        a2=_lt_to_str(Node_Configuration_Names[j][0]),
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   911
                                        a3=Node_Configuration_Names[j][1])
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   912
                    self.FatalError(error_message)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   913
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   914
        # Search for use of duplicate port numbers by Modbus/IP servers
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   915
        # Note: We only consider duplicate port numbers if using the same network interface!
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   916
        i = 0
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   917
        for loc1, addr1, port1 in IPServer_port_numbers[:-1]:
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   918
            i = i + 1
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   919
            for loc2, addr2, port2 in IPServer_port_numbers[i:]:
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   920
                if (port1 == port2) and (
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   921
                          (addr1 == addr2)   # on the same network interface
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   922
                       or (addr1 == "") or (addr1 == "*") or (addr1 == "#ANY#") # or one (or both) of the servers
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   923
                       or (addr2 == "") or (addr2 == "*") or (addr2 == "#ANY#") # use all available network interfaces
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   924
                   ):
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   925
                    error_message = _("Error: Modbus plugin nodes %{a1}.x and %{a2}.x use same port number \"{a3}\" " + 
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   926
                                      "on the same (or overlapping) network interfaces \"{a4}\" and \"{a5}\".\n").format(
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   927
                                        a1=_lt_to_str(loc1), a2=_lt_to_str(loc2), a3=port1, a4=addr1, a5=addr2)
2648
e4ab768170f9 modbus_plugin: generate nicer error message, instead of a traceback
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   928
                    self.FatalError(error_message)
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   929
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   930
        # Determine the current location in Beremiz's project configuration
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   931
        # tree
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   932
        current_location = self.GetCurrentLocation()
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   933
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   934
        # define a unique name for the generated C and h files
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   935
        prefix = "_".join(map(str, current_location))
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   936
        Gen_MB_c_path = os.path.join(buildpath, "MB_%s.c" % prefix)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   937
        Gen_MB_h_path = os.path.join(buildpath, "MB_%s.h" % prefix)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   938
        c_filename = os.path.join(os.path.split(__file__)[0], "mb_runtime.c")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   939
        h_filename = os.path.join(os.path.split(__file__)[0], "mb_runtime.h")
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   940
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   941
        tcpclient_reqs_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   942
        rtuclient_reqs_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   943
        ascclient_reqs_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   944
        tcpclient_node_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   945
        rtuclient_node_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   946
        ascclient_node_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   947
        tcpserver_node_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   948
        rtuserver_node_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   949
        ascserver_node_count = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   950
        nodeid = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   951
        client_nodeid = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   952
        client_requestid = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   953
        server_id = 0
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   954
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   955
        server_node_list = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   956
        client_node_list = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   957
        client_request_list = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   958
        server_memarea_list = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   959
        loc_vars = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   960
        loc_vars_list = []  # list of variables already declared in C code!
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   961
        for child in self.IECSortedChildren():
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   962
            # print "<<<<<<<<<<<<<"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   963
            # print "child (self.IECSortedChildren())----->"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   964
            # print child.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   965
            # print ">>>>>>>>>>>>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   966
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   967
            if child.PlugType == "ModbusTCPserver":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   968
                tcpserver_node_count += 1
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   969
                new_node = GetTCPServerNodePrinted(self, child)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   970
                if new_node is None:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   971
                    return [], "", False
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   972
                server_node_list.append(new_node)                
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   973
                #        We currently add 4 flags/counters to each Modbus server/slave
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   974
                #
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   975
                #        We add the Modbus read/write counter/flag to each Modbus slave/server
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   976
                #        to allow the user program to determine if the slave is being actively
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   977
                #        read from or written by by a remote Modbus client.
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   978
                for iecvar in child.GetLocations():
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   979
                    #print "child" + repr(iecvar)
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   980
                    if (len(iecvar["LOC"]) == 3) and (str(iecvar["NAME"]) not in loc_vars_list):
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   981
                        # Add if it is a "Modbus Read Request Counter" (mapped onto %MDa.b.0), so last number is a '0'
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   982
                        if iecvar["LOC"][2] == 0:
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   983
                            loc_vars.append("u32 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.flag_read_req_counter;" % (server_id))
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   984
                            loc_vars_list.append(str(iecvar["NAME"]))
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   985
                        # Add if it is a "Modbus Write Request Counter" (mapped onto %MDa.b.1), so last number is a '1'
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   986
                        if iecvar["LOC"][2] == 1:
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   987
                            loc_vars.append("u32 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.flag_write_req_counter;" % (server_id))
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   988
                            loc_vars_list.append(str(iecvar["NAME"]))
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   989
                        # Add if it is a "Modbus Read Request Flag" (mapped onto %MDa.b.2), so last number is a '2'
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   990
                        if iecvar["LOC"][2] == 2:
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   991
                            loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.flag_read_req_flag;" % (server_id))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   992
                            loc_vars_list.append(str(iecvar["NAME"]))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   993
                        # Add if it is a "Modbus Write Request Counter" (mapped onto %MDa.b.3), so last number is a '3'
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   994
                        if iecvar["LOC"][2] == 3:
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   995
                            loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.flag_write_req_flag;" % (server_id))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
   996
                            loc_vars_list.append(str(iecvar["NAME"]))
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   997
                
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   998
                for subchild in child.IECSortedChildren():
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   999
                    new_memarea = GetTCPServerMemAreaPrinted(self, subchild, nodeid)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1000
                    if new_memarea is None:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1001
                        return [], "", False
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1002
                    server_memarea_list.append(new_memarea)
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1003
                    function = subchild.GetParamsAttributes()[0]["children"][0]["value"]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1004
                    # 'ro_bits', 'rw_bits', 'ro_words' or 'rw_words'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1005
                    memarea = modbus_memtype_dict[function][1]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1006
                    for iecvar in subchild.GetLocations():
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1007
                        if len(iecvar["LOC"]) == 4:
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1008
                            #print "subchild" + repr(iecvar)
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1009
                            absloute_address = iecvar["LOC"][3]
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1010
                            start_address = int(GetCTVal(subchild, 2))
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1011
                            relative_addr = absloute_address - start_address
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1012
                            # test if relative address in request specified range
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1013
                            if relative_addr in xrange(int(GetCTVal(subchild, 1))):
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1014
                                if str(iecvar["NAME"]) not in loc_vars_list:
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1015
                                    loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.%s[%d];" % (
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1016
                                        server_id, memarea, absloute_address))
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1017
                                    loc_vars_list.append(str(iecvar["NAME"]))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1018
                server_id += 1
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1019
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1020
            if child.PlugType == "ModbusRTUslave":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1021
                rtuserver_node_count += 1
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1022
                new_node = GetRTUSlaveNodePrinted(self, child)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1023
                if new_node is None:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1024
                    return [], "", False
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1025
                server_node_list.append(new_node)
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1026
                #        We currently add 4 flags/counters to each Modbus server/slave
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1027
                #
2722
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1028
                #        We add the Modbus read/write counter/flag to each Modbus slave/server
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1029
                #        to allow the user program to determine if the slave is being actively
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1030
                #        read from or written by by a remote Modbus client.
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1031
                for iecvar in child.GetLocations():
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1032
                    #print "child" + repr(iecvar)
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1033
                    if (len(iecvar["LOC"]) == 3) and (str(iecvar["NAME"]) not in loc_vars_list):
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1034
                        # Add if it is a "Modbus Read Request Counter" (mapped onto %MDa.b.0), so last number is a '0'
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1035
                        if iecvar["LOC"][2] == 0:
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1036
                            loc_vars.append("u32 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.flag_read_req_counter;" % (server_id))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1037
                            loc_vars_list.append(str(iecvar["NAME"]))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1038
                        # Add if it is a "Modbus Write Request Counter" (mapped onto %MDa.b.1), so last number is a '1'
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1039
                        if iecvar["LOC"][2] == 1:
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1040
                            loc_vars.append("u32 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.flag_write_req_counter;" % (server_id))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1041
                            loc_vars_list.append(str(iecvar["NAME"]))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1042
                        # Add if it is a "Modbus Read Request Flag" (mapped onto %MDa.b.2), so last number is a '2'
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1043
                        if iecvar["LOC"][2] == 2:
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1044
                            loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.flag_read_req_flag;" % (server_id))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1045
                            loc_vars_list.append(str(iecvar["NAME"]))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1046
                        # Add if it is a "Modbus Write Request Counter" (mapped onto %MDa.b.3), so last number is a '3'
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1047
                        if iecvar["LOC"][2] == 3:
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1048
                            loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.flag_write_req_flag;" % (server_id))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1049
                            loc_vars_list.append(str(iecvar["NAME"]))
5d72a52b8f9c modbus plugin: add (user accessible) transaction bool flags for servers/slaves; FIX BUG in previous commit affecting RTU slaves.
Mario de Sousa <msousa@fe.up.pt>
parents: 2721
diff changeset
  1050
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1051
                for subchild in child.IECSortedChildren():
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1052
                    new_memarea = GetTCPServerMemAreaPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1053
                        self, subchild, nodeid)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1054
                    if new_memarea is None:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1055
                        return [], "", False
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1056
                    server_memarea_list.append(new_memarea)
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1057
                    function = subchild.GetParamsAttributes()[0]["children"][0]["value"]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1058
                    # 'ro_bits', 'rw_bits', 'ro_words' or 'rw_words'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1059
                    memarea = modbus_memtype_dict[function][1]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1060
                    for iecvar in subchild.GetLocations():
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1061
                        if len(iecvar["LOC"]) == 4:
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1062
                            # print repr(iecvar)
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1063
                            absloute_address = iecvar["LOC"][3]
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1064
                            start_address = int(GetCTVal(subchild, 2))
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1065
                            relative_addr = absloute_address - start_address
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1066
                            # test if relative address in request specified range
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1067
                            if relative_addr in xrange(int(GetCTVal(subchild, 1))):
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1068
                                if str(iecvar["NAME"]) not in loc_vars_list:
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1069
                                    loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.%s[%d];" % (
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1070
                                        server_id, memarea, absloute_address))
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
  1071
                                    loc_vars_list.append(str(iecvar["NAME"]))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1072
                server_id += 1
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1073
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1074
            if child.PlugType == "ModbusTCPclient":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1075
                tcpclient_reqs_count += len(child.IECSortedChildren())
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1076
                new_node = GetTCPClientNodePrinted(self, child)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1077
                if new_node is None:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1078
                    return [], "", False
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1079
                client_node_list.append(new_node)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1080
                for subchild in child.IECSortedChildren():
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1081
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1082
                        self, subchild, client_nodeid)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1083
                    if new_req is None:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1084
                        return [], "", False
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1085
                    client_request_list.append(new_req)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1086
                    for iecvar in subchild.GetLocations():
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1087
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
  1088
                        relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1089
                        # test if the located variable 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1090
                        #    (a) has relative address in request specified range
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1091
                        #  AND is NOT
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1092
                        #    (b) is a flag added by this modbus plugin.
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1093
                        #        We currently add 3 flags: An execution control flag
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1094
                        #        and another two status flags.
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1095
                        #        We add the "Execution Control Flag" to each client request (one flag per request)
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1096
                        #        to allow the user program to control when to execute the request (if not executed periodically)
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1097
                        #        While all Modbus registers/coils are mapped onto a location
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1098
                        #        with 4 numbers (e.g. %QX0.1.2.55), this control flag is mapped
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1099
                        #        onto a location with 4 numbers (e.g. %QX0.1.2.0.0), where the last
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1100
                        #        two numbers are always '0.0', and the first two identify the request.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1101
                        #        In the following if, we check for this condition by checking
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1102
                        #        if there are at least 4 or more number in the location's address.
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1103
                        if (        relative_addr in xrange(int(GetCTVal(subchild, 2)))  # condition (a) explained above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1104
                            and len(iecvar["LOC"]) < 5):                                  # condition (b) explained above
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1105
                            if str(iecvar["NAME"]) not in loc_vars_list:
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1106
                                loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].plcv_buffer[%d];" % (client_requestid, relative_addr))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1107
                                loc_vars_list.append(str(iecvar["NAME"]))
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1108
                        # Now add the located variable in case it is a flag (condition (b) above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1109
                        if  len(iecvar["LOC"]) >= 5:       # condition (b) explained above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1110
                            if str(iecvar["NAME"]) not in loc_vars_list:
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1111
                                # Add if it is a Execution Request Flag (mapped onto %QXa.b.c.0.0), so last number is a '0'
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1112
                                if iecvar["LOC"][4] == 0:
2715
1215a6b741d5 Modbus plugin: fix bug - map BOOL flag onto C u8 (was incorrectly mapped onto u16)
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
  1113
                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1114
                                    loc_vars_list.append(str(iecvar["NAME"]))
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1115
                                # Add if it is a "Modbus Request Status flag" (mapped onto %QWa.b.c.0.1), so last number is a '1'
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1116
                                #    -> will store the result of the last executed MB transaction
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1117
                                #         1 -> error accessing IP network, or serial interface
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1118
                                #         2 -> reply received from server was an invalid frame
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1119
                                #         3 -> server did not reply before timeout expired
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1120
                                #         4 -> server returned a valid Modbus error frame
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1121
                                #    -> will be reset (set to 0) once this MB transaction has completed sucesfully 
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1122
                                if iecvar["LOC"][4] == 1:
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1123
                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_tn_error_code;" % (client_requestid))
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1124
                                    loc_vars_list.append(str(iecvar["NAME"]))
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1125
                                # Add if it is a "Modbus Error code" (mapped onto %QWa.b.c.0.2), so last number is a '2'
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1126
                                #    -> if "Modbus Request Status flag" is 4, this flag will store the MB error code returned by the MB server in a MB error frame
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1127
                                #    -> will be reset (set to 0) once this MB transaction has completed succesfully                                
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1128
                                if iecvar["LOC"][4] == 2:
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1129
                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_mb_error_code;" % (client_requestid))
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1130
                                    loc_vars_list.append(str(iecvar["NAME"]))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1131
                    client_requestid += 1
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1132
                tcpclient_node_count += 1
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1133
                client_nodeid += 1
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1134
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1135
            if child.PlugType == "ModbusRTUclient":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1136
                rtuclient_reqs_count += len(child.IECSortedChildren())
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1137
                new_node = GetRTUClientNodePrinted(self, child)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1138
                if new_node is None:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1139
                    return [], "", False
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1140
                client_node_list.append(new_node)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1141
                for subchild in child.IECSortedChildren():
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1142
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1143
                        self, subchild, client_nodeid)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1144
                    if new_req is None:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1145
                        return [], "", False
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1146
                    client_request_list.append(new_req)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1147
                    for iecvar in subchild.GetLocations():
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1148
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
  1149
                        relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1150
                        # test if the located variable 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1151
                        #    (a) has relative address in request specified range
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1152
                        #  AND is NOT
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1153
                        #    (b) is a flag added by this modbus plugin.
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1154
                        #        We currently add 3 flags: An execution control flag
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1155
                        #        and another two status flags.
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1156
                        #        We add the "Execution Control Flag" to each client request (one flag per request)
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1157
                        #        to allow the user program to control when to execute the request (if not executed periodically)
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1158
                        #        While all Modbus registers/coils are mapped onto a location
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1159
                        #        with 4 numbers (e.g. %QX0.1.2.55), this control flag is mapped
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1160
                        #        onto a location with 4 numbers (e.g. %QX0.1.2.0.0), where the last
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1161
                        #        two numbers are always '0.0', and the first two identify the request.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1162
                        #        In the following if, we check for this condition by checking
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1163
                        #        if there are at least 4 or more number in the location's address.
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1164
                        if (        relative_addr in xrange(int(GetCTVal(subchild, 2)))  # condition (a) explained above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1165
                            and len(iecvar["LOC"]) < 5):                                  # condition (b) explained above
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1166
                            if str(iecvar["NAME"]) not in loc_vars_list:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1167
                                loc_vars.append(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1168
                                    "u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].plcv_buffer[%d];" % (client_requestid, relative_addr))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1169
                                loc_vars_list.append(str(iecvar["NAME"]))
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1170
                        # Now add the located variable in case it is a flag (condition (b) above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1171
                        if  len(iecvar["LOC"]) >= 5:       # condition (b) explained above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
  1172
                            if str(iecvar["NAME"]) not in loc_vars_list:
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1173
                                # Add if it is a Execution Request Flag (mapped onto %QXa.b.c.0.0), so last number is a '0'
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1174
                                if iecvar["LOC"][4] == 0:
2715
1215a6b741d5 Modbus plugin: fix bug - map BOOL flag onto C u8 (was incorrectly mapped onto u16)
Mario de Sousa <msousa@fe.up.pt>
parents: 2714
diff changeset
  1175
                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1176
                                    loc_vars_list.append(str(iecvar["NAME"]))
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1177
                                # Add if it is a "Modbus Request Status flag" (mapped onto %QWa.b.c.0.1), so last number is a '1'
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1178
                                #    -> will store the result of the last executed MB transaction
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1179
                                #         1 -> error accessing IP network, or serial interface
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1180
                                #         2 -> reply received from server was an invalid frame
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1181
                                #         3 -> server did not reply before timeout expired
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1182
                                #         4 -> server returned a valid Modbus error frame
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1183
                                #    -> will be reset (set to 0) once this MB transaction has completed sucesfully 
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1184
                                if iecvar["LOC"][4] == 1:
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1185
                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_tn_error_code;" % (client_requestid))
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1186
                                    loc_vars_list.append(str(iecvar["NAME"]))
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1187
                                # Add if it is a "Modbus Error code" (mapped onto %QWa.b.c.0.2), so last number is a '2'
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1188
                                #    -> if "Modbus Request Status flag" is 4, this flag will store the MB error code returned by the MB server in a MB error frame
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1189
                                #    -> will be reset (set to 0) once this MB transaction has completed succesfully                                
2714
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1190
                                if iecvar["LOC"][4] == 2:
0b636dc947a2 Modbus plugin: map status of MB transaction onto 2 located BYTE vars (instead of a singlw WORD var).
Mario de Sousa <msousa@fe.up.pt>
parents: 2713
diff changeset
  1191
                                    loc_vars.append("u8 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_mb_error_code;" % (client_requestid))
2713
680ea4684209 Modbus plugin: provide user programs feedback on current connection status mapped onto a WORD located variable
Mario de Sousa <msousa@fe.up.pt>
parents: 2663
diff changeset
  1192
                                    loc_vars_list.append(str(iecvar["NAME"]))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1193
                    client_requestid += 1
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1194
                rtuclient_node_count += 1
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1195
                client_nodeid += 1
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1196
            nodeid += 1
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1197
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1198
        loc_dict["loc_vars"] = "\n".join(loc_vars)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1199
        loc_dict["server_nodes_params"] = ",\n\n".join(server_node_list)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1200
        loc_dict["client_nodes_params"] = ",\n\n".join(client_node_list)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1201
        loc_dict["client_req_params"] = ",\n\n".join(client_request_list)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1202
        loc_dict["tcpclient_reqs_count"] = str(tcpclient_reqs_count)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1203
        loc_dict["tcpclient_node_count"] = str(tcpclient_node_count)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1204
        loc_dict["tcpserver_node_count"] = str(tcpserver_node_count)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1205
        loc_dict["rtuclient_reqs_count"] = str(rtuclient_reqs_count)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1206
        loc_dict["rtuclient_node_count"] = str(rtuclient_node_count)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1207
        loc_dict["rtuserver_node_count"] = str(rtuserver_node_count)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1208
        loc_dict["ascclient_reqs_count"] = str(ascclient_reqs_count)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1209
        loc_dict["ascclient_node_count"] = str(ascclient_node_count)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1210
        loc_dict["ascserver_node_count"] = str(ascserver_node_count)
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1211
        loc_dict["total_tcpnode_count"] = str(total_node_count[0])
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1212
        loc_dict["total_rtunode_count"] = str(total_node_count[1])
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1213
        loc_dict["total_ascnode_count"] = str(total_node_count[2])
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1214
        loc_dict["max_remote_tcpclient"] = int(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1215
            self.GetParamsAttributes()[0]["children"][0]["value"])
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1216
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1217
        # get template file content into a string, format it with dict
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1218
        # and write it to proper .h file
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1219
        mb_main = open(h_filename).read() % loc_dict
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1220
        f = open(Gen_MB_h_path, 'w')
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1221
        f.write(mb_main)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1222
        f.close()
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1223
        # same thing as above, but now to .c file
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1224
        mb_main = open(c_filename).read() % loc_dict
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1225
        f = open(Gen_MB_c_path, 'w')
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1226
        f.write(mb_main)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1227
        f.close()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1228
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1229
        LDFLAGS = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1230
        LDFLAGS.append(" \"-L" + ModbusPath + "\"")
2333
81abf93b4684 Path may contain spaces... sequel of d1359cb
Edouard Tisserant
parents: 2291
diff changeset
  1231
        LDFLAGS.append(" \"" + os.path.join(ModbusPath, "libmb.a") + "\"")
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1232
        LDFLAGS.append(" \"-Wl,-rpath," + ModbusPath + "\"")
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1233
        # LDFLAGS.append("\"" + os.path.join(ModbusPath, "mb_slave_and_master.o") + "\"")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1234
        # LDFLAGS.append("\"" + os.path.join(ModbusPath, "mb_slave.o") + "\"")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1235
        # LDFLAGS.append("\"" + os.path.join(ModbusPath, "mb_master.o") + "\"")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1236
        # LDFLAGS.append("\"" + os.path.join(ModbusPath, "mb_tcp.o")    + "\"")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1237
        # LDFLAGS.append("\"" + os.path.join(ModbusPath, "mb_rtu.o")    + "\"")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1238
        # LDFLAGS.append("\"" + os.path.join(ModbusPath, "mb_ascii.o")    + "\"")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1239
        # LDFLAGS.append("\"" + os.path.join(ModbusPath, "sin_util.o")  + "\"")
1913
338e2f51b685 modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents: 1909
diff changeset
  1240
        # Target is ARM with linux and not win on x86 so winsock2 (ws2_32) library is useless !!!
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1241
        # if os.name == 'nt':   # other possible values: 'posix' 'os2' 'ce' 'java' 'riscos'
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1242
        # LDFLAGS.append(" -lws2_32 ")  # on windows we need to load winsock
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1243
        # library!
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1244
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1245
        websettingfile = open(paths.AbsNeighbourFile(__file__, "web_settings.py"), 'r')
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1246
        websettingcode = websettingfile.read()
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1247
        websettingfile.close()
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1248
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1249
        location_str = "_".join(map(str, self.GetCurrentLocation()))
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1250
        websettingcode = websettingcode % locals()
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1251
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1252
        runtimefile_path = os.path.join(buildpath, "runtime_modbus_websettings.py")
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1253
        runtimefile = open(runtimefile_path, 'w')
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1254
        runtimefile.write(websettingcode)
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1255
        runtimefile.close()
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1256
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1257
        return ([(Gen_MB_c_path, ' -I"' + ModbusPath + '"')], LDFLAGS, True,
2703
32ffdb32b14e Python Runtime: order of execution of extension's init() and cleanup() now reflects order of appearance of extensions in configuration tree.
Edouard Tisserant
parents: 2677
diff changeset
  1258
                ("runtime_%s_modbus_websettings.py" % location_str, open(runtimefile_path, "rb")),
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1259
        )