modbus/modbus.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 03 Mar 2023 09:47:39 +0100
branchwxPython4
changeset 3739 99263915a91d
parent 3733 d1acf20e8e7c
child 3750 f62625418bff
permissions -rw-r--r--
Fix typo in toolchain_gcc.py : LDLAGS -> LDFLAGS
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>
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   322
          <xsd:attribute name="Request_Delay_in_ms" use="optional" default="0">
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   323
            <xsd:simpleType>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   324
                <xsd:restriction base="xsd:integer">
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   325
                    <xsd:minInclusive value="0"/>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   326
                    <xsd:maxInclusive value="2147483647"/>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   327
                </xsd:restriction>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   328
            </xsd:simpleType>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   329
          </xsd:attribute>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   330
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   331
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   332
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   333
    """
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   334
    # NOTE: Max value of 2147483647 (i32_max) for Invocation_Rate_in_ms and Request_Delay_in_ms
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   335
    # 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
   336
    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
   337
    # 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
   338
    PlugType = "ModbusTCPclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   339
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
   340
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
    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
   342
        # 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
   343
        # 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
   344
        # 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
   345
        # 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
   346
        
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
        # 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
   348
        # 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
   349
        # _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
   350
        # 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
   351
        #   (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
   352
        #   (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
   353
        #   (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
   354
        #   (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
   355
        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
   356
        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
   357
        
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   358
    # 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
   359
    #   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
   360
    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
   361
        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
   362
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   363
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   364
        """ 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
   365
        return self.ModbusTCPclient.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   366
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   367
    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
   368
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   369
        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
   370
        @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
   371
        @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
   372
            [{"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
   373
            "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
   374
            "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
   375
            "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
   376
            "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
   377
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   378
        @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
   379
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   380
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   381
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   382
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   383
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   384
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   385
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   386
# 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
   387
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   388
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   389
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   390
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   391
# 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
   392
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   393
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
   394
    # 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
   395
    # 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
   396
    # (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
   397
    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
   398
    <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
   399
      <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
   400
        <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
   401
          <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
   402
          <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
   403
          <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
   404
          <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
   405
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   406
                <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
   407
                    <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
   408
                    <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
   409
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   410
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   411
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   412
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   413
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   414
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   415
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   416
    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
   417
    # 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
   418
    PlugType = "ModbusTCPserver"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   419
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
   420
    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
   421
        # 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
   422
        # 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
   423
        # 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
   424
        # 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
   425
        
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
        # 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
   427
        # 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
   428
        # _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
   429
        # 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
   430
        #   (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
   431
        #   (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
   432
        #   (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
   433
        #   (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
   434
        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
   435
        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
   436
        
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   437
    # 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
   438
    #   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
   439
    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
   440
        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
   441
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
   442
    # 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
   443
    #     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
   444
    #     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
   445
    #     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
   446
    #                  ("", "*", 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
   447
    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
   448
        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
   449
        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
   450
        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
   451
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   452
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   453
        """ 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
   454
        return self.ModbusServerNode.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   455
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   456
    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
   457
        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
   458
        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
   459
        # 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
   460
        # 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
   461
        # 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
   462
        # 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
   463
        # 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
   464
        # 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
   465
        #          %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
   466
        #       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
   467
        #       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
   468
        #       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
   469
        #       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
   470
        #
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   471
        # 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
   472
        # 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
   473
        #          %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
   474
        # 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
   475
        # 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
   476
        # 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
   477
        #           %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
   478
        # 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
   479
        #           %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
   480
        #           %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
   481
        #           %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
   482
        #           %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
   483
        # 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
   484
        entries = []
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   485
        entries.append({
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   486
            "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
   487
            "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
   488
            "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
   489
            "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
   490
            "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
   491
            "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
   492
            "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
   493
            "children": []})        
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   494
        entries.append({
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   495
            "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
   496
            "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
   497
            "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
   498
            "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
   499
            "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
   500
            "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
   501
            "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
   502
            "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
   503
        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
   504
            "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
   505
            "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
   506
            "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
   507
            "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
   508
            "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
   509
            "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
   510
            "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
   511
            "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
   512
        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
   513
            "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
   514
            "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
   515
            "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
   516
            "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
   517
            "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
   518
            "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
   519
            "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
   520
            "children": []})        
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   521
        # 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
   522
        # 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
   523
        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
   524
            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
   525
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   526
        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
   527
                "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
   528
                "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
   529
                "children": entries}
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   530
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   531
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   532
    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
   533
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   534
        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
   535
        @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
   536
        @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
   537
            [{"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
   538
            "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
   539
            "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
   540
            "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
   541
            "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
   542
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   543
        @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
   544
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   545
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   546
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   547
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   548
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   549
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   550
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   551
# 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
   552
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   553
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   554
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   555
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   556
# 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
   557
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   558
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
   559
    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
   560
    <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
   561
      <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
   562
        <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
   563
          <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
   564
          <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
   565
          <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
   566
          <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
   567
          <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
   568
          <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
   569
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   570
                <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
   571
                    <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
   572
                    <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
   573
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   574
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   575
          </xsd:attribute>
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   576
          <xsd:attribute name="Request_Delay_in_ms" use="optional" default="0">
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   577
            <xsd:simpleType>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   578
                <xsd:restriction base="xsd:integer">
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   579
                    <xsd:minInclusive value="0"/>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   580
                    <xsd:maxInclusive value="2147483647"/>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   581
                </xsd:restriction>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   582
            </xsd:simpleType>
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   583
          </xsd:attribute>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   584
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   585
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   586
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   587
    """
3733
d1acf20e8e7c Modbus plugin: add optional delay between two consecutive modbus requests sent by master/client
Mario de Sousa <msousa@fe.up.pt>
parents: 2736
diff changeset
   588
    # NOTE: Max value of 2147483647 (i32_max) for Invocation_Rate_in_ms and Request_Delay_in_ms
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   589
    # 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
   590
    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
   591
    # 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
   592
    PlugType = "ModbusRTUclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   593
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
   594
    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
   595
        # 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
   596
        # 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
   597
        # 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
   598
        # 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
   599
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   600
        # 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
   601
        # 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
   602
        # _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
   603
        # 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
   604
        #   (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
   605
        #   (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
   606
        #   (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
   607
        #   (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
   608
        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
   609
        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
   610
        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   611
    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
   612
        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
   613
        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
   614
            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
   615
                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
   616
                    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
   617
                        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
   618
                    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
   619
                        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
   620
                    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
   621
                        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
   622
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   623
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   624
    # 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
   625
    #   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
   626
    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
   627
        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
   628
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   629
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   630
        """ 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
   631
        return self.ModbusRTUclient.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   632
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   633
    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
   634
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   635
        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
   636
        @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
   637
        @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
   638
            [{"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
   639
            "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
   640
            "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
   641
            "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
   642
            "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
   643
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   644
        @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
   645
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   646
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   647
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   648
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   649
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   650
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   651
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   652
# 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
   653
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   654
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   655
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   656
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   657
# 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
   658
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   659
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
   660
    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
   661
    <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
   662
      <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
   663
        <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
   664
          <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
   665
          <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
   666
          <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
   667
          <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
   668
          <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
   669
          <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
   670
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   671
                <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
   672
                    <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
   673
                    <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
   674
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   675
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   676
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   677
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   678
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   679
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   680
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   681
    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
   682
    # 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
   683
    PlugType = "ModbusRTUslave"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   684
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
   685
    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
   686
        # 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
   687
        # 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
   688
        # 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
   689
        # 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
   690
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   691
        # 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
   692
        # 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
   693
        # _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
   694
        # 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
   695
        #   (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
   696
        #   (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
   697
        #   (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
   698
        #   (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
   699
        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
   700
        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
   701
        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   702
    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
   703
        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
   704
        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
   705
            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
   706
                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
   707
                    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
   708
                        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
   709
                    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
   710
                        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
   711
                    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
   712
                        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
   713
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   714
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   715
    # 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
   716
    #   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
   717
    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
   718
        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
   719
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   720
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   721
        """ 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
   722
        return self.ModbusRTUslave.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   723
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   724
    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
   725
        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
   726
        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
   727
        # 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
   728
        # 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
   729
        # 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
   730
        # 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
   731
        # 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
   732
        # 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
   733
        #          %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
   734
        #       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
   735
        #       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
   736
        #       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
   737
        #       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
   738
        #
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   739
        # 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
   740
        # 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
   741
        #          %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
   742
        # 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
   743
        # 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
   744
        # 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
   745
        #           %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
   746
        # 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
   747
        #           %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
   748
        #           %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
   749
        #           %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
   750
        #           %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
   751
        # 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
   752
        entries = []
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   753
        entries.append({
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   754
            "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
   755
            "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
   756
            "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
   757
            "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
   758
            "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
   759
            "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
   760
            "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
   761
            "children": []})        
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   762
        entries.append({
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   763
            "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
   764
            "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
   765
            "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
   766
            "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
   767
            "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
   768
            "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
   769
            "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
   770
            "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
   771
        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
   772
            "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
   773
            "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
   774
            "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
   775
            "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
   776
            "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
   777
            "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
   778
            "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
   779
            "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
   780
        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
   781
            "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
   782
            "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
   783
            "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
   784
            "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
   785
            "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
   786
            "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
   787
            "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
   788
            "children": []})        
2721
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   789
        # 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
   790
        # 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
   791
        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
   792
            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
   793
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   794
        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
   795
                "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
   796
                "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
   797
                "children": entries}
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   798
367eb26972b1 modbus plugin: add (user accessible) transaction counters for servers/slaves
Mario de Sousa <msousa@fe.up.pt>
parents: 2717
diff changeset
   799
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   800
    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
   801
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   802
        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
   803
        @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
   804
        @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
   805
            [{"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
   806
            "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
   807
            "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
   808
            "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
   809
            "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
   810
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   811
        @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
   812
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   813
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   814
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   815
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   816
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
   817
    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
   818
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   819
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   820
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   821
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   822
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   823
# 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
   824
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   825
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   826
#
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   827
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
   828
    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
   829
    <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
   830
      <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
   831
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   832
          <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
   833
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   834
                <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
   835
                    <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
   836
                    <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
   837
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   838
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   839
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   840
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   841
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   842
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   843
    """
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   844
    CTNChildrenTypes = [("ModbusTCPclient", _ModbusTCPclientPlug, "Modbus TCP Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   845
                        ("ModbusTCPserver", _ModbusTCPserverPlug, "Modbus TCP Server"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   846
                        ("ModbusRTUclient", _ModbusRTUclientPlug, "Modbus RTU Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   847
                        ("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
   848
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   849
    # 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
   850
    #   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
   851
    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
   852
        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
   853
            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
   854
        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
   855
        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
   856
            # 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
   857
            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
   858
                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
   859
        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
   860
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   861
    # 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
   862
    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
   863
        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
   864
        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
   865
            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
   866
                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
   867
        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
   868
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   869
    # 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
   870
    def GetConfigNames(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   871
        Node_Configuration_Names = []
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   872
        for child in self.IECSortedChildren():
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   873
            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
   874
        return Node_Configuration_Names
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   875
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   876
    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
   877
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   878
        # print self.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   879
        # print type(self)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   880
        # print "self.CTNType >>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   881
        # print self.CTNType
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   882
        # 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
   883
        # 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
   884
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   885
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   886
        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
   887
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   888
        # 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
   889
        #   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
   890
        #
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
   891
        # 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
   892
        #   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
   893
        #   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
   894
        #       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
   895
        #       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
   896
        #       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
   897
        #                             ("", "*", 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
   898
        #
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   899
        # 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
   900
        #   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
   901
        #   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
   902
        #       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
   903
        #       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
   904
        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
   905
        IPServer_port_numbers    = []
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   906
        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
   907
        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
   908
            if CTNInstance.CTNType == "modbus":
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   909
                # 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
   910
                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
   911
                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
   912
                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
   913
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   914
        # 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
   915
        # 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
   916
        # (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
   917
        # 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
   918
        # 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
   919
        # 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
   920
        # distinguasheble on the web interface!
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   921
        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
   922
            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
   923
                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
   924
                    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
   925
                                        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
   926
                                        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
   927
                                        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
   928
                    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
   929
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   930
        # 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
   931
        # 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
   932
        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
   933
        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
   934
            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
   935
            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
   936
                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
   937
                          (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
   938
                       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
   939
                       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
   940
                   ):
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
   941
                    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
   942
                                      "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
   943
                                        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
   944
                    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
   945
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   946
        # 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
   947
        # tree
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   948
        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
   949
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   950
        # 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
   951
        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
   952
        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
   953
        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
   954
        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
   955
        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
   956
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   957
        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
   958
        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
   959
        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
   960
        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
   961
        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
   962
        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
   963
        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
   964
        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
   965
        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
   966
        nodeid = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   967
        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
   968
        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
   969
        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
   970
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   971
        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
   972
        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
   973
        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
   974
        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
   975
        loc_vars = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   976
        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
   977
        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
   978
            # print "<<<<<<<<<<<<<"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   979
            # 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
   980
            # print child.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   981
            # print ">>>>>>>>>>>>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   982
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   983
            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
   984
                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
   985
                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
   986
                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
   987
                    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
   988
                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
   989
                #        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
   990
                #
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
   991
                #        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
   992
                #        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
   993
                #        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
   994
                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
   995
                    #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
   996
                    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
   997
                        # 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
   998
                        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
   999
                            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
  1000
                            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
  1001
                        # 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
  1002
                        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
  1003
                            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
  1004
                            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
  1005
                        # 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
  1006
                        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
  1007
                            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
  1008
                            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
  1009
                        # 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
  1010
                        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
  1011
                            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
  1012
                            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
  1013
                
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1014
                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
  1015
                    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
  1016
                    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
  1017
                        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
  1018
                    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
  1019
                    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
  1020
                    # '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
  1021
                    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
  1022
                    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
  1023
                        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
  1024
                            #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
  1025
                            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
  1026
                            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
  1027
                            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
  1028
                            # 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
  1029
                            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
  1030
                                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
  1031
                                    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
  1032
                                        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
  1033
                                    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
  1034
                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
  1035
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1036
            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
  1037
                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
  1038
                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
  1039
                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
  1040
                    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
  1041
                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
  1042
                #        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
  1043
                #
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
  1044
                #        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
  1045
                #        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
  1046
                #        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
  1047
                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
  1048
                    #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
  1049
                    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
  1050
                        # 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
  1051
                        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
  1052
                            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
  1053
                            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
  1054
                        # 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
  1055
                        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
  1056
                            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
  1057
                            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
  1058
                        # 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
  1059
                        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
  1060
                            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
  1061
                            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
  1062
                        # 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
  1063
                        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
  1064
                            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
  1065
                            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
  1066
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1067
                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
  1068
                    new_memarea = GetTCPServerMemAreaPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1069
                        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
  1070
                    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
  1071
                        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
  1072
                    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
  1073
                    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
  1074
                    # '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
  1075
                    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
  1076
                    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
  1077
                        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
  1078
                            # 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
  1079
                            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
  1080
                            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
  1081
                            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
  1082
                            # 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
  1083
                            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
  1084
                                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
  1085
                                    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
  1086
                                        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
  1087
                                    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
  1088
                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
  1089
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1090
            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
  1091
                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
  1092
                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
  1093
                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
  1094
                    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
  1095
                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
  1096
                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
  1097
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1098
                        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
  1099
                    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
  1100
                        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
  1101
                    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
  1102
                    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
  1103
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
  1104
                        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
  1105
                        # 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
  1106
                        #    (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
  1107
                        #  AND is NOT
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1108
                        #    (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
  1109
                        #        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
  1110
                        #        and another two status flags.
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1111
                        #        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
  1112
                        #        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
  1113
                        #        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
  1114
                        #        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
  1115
                        #        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
  1116
                        #        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
  1117
                        #        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
  1118
                        #        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
  1119
                        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
  1120
                            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
  1121
                            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
  1122
                                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
  1123
                                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
  1124
                        # 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
  1125
                        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
  1126
                            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
  1127
                                # 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
  1128
                                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
  1129
                                    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
  1130
                                    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
  1131
                                # 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
  1132
                                #    -> 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
  1133
                                #         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
  1134
                                #         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
  1135
                                #         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
  1136
                                #         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
  1137
                                #    -> 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
  1138
                                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
  1139
                                    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
  1140
                                    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
  1141
                                # 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
  1142
                                #    -> 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
  1143
                                #    -> 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
  1144
                                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
  1145
                                    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
  1146
                                    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
  1147
                    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
  1148
                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
  1149
                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
  1150
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1151
            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
  1152
                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
  1153
                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
  1154
                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
  1155
                    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
  1156
                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
  1157
                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
  1158
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1159
                        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
  1160
                    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
  1161
                        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
  1162
                    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
  1163
                    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
  1164
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
  1165
                        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
  1166
                        # 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
  1167
                        #    (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
  1168
                        #  AND is NOT
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1169
                        #    (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
  1170
                        #        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
  1171
                        #        and another two status flags.
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
  1172
                        #        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
  1173
                        #        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
  1174
                        #        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
  1175
                        #        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
  1176
                        #        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
  1177
                        #        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
  1178
                        #        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
  1179
                        #        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
  1180
                        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
  1181
                            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
  1182
                            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
  1183
                                loc_vars.append(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1184
                                    "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
  1185
                                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
  1186
                        # 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
  1187
                        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
  1188
                            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
  1189
                                # 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
  1190
                                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
  1191
                                    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
  1192
                                    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
  1193
                                # 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
  1194
                                #    -> 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
  1195
                                #         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
  1196
                                #         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
  1197
                                #         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
  1198
                                #         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
  1199
                                #    -> 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
  1200
                                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
  1201
                                    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
  1202
                                    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
  1203
                                # 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
  1204
                                #    -> 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
  1205
                                #    -> 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
  1206
                                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
  1207
                                    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
  1208
                                    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
  1209
                    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
  1210
                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
  1211
                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
  1212
            nodeid += 1
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1213
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1214
        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
  1215
        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
  1216
        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
  1217
        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
  1218
        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
  1219
        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
  1220
        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
  1221
        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
  1222
        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
  1223
        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
  1224
        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
  1225
        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
  1226
        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
  1227
        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
  1228
        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
  1229
        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
  1230
        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
  1231
            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
  1232
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1233
        # 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
  1234
        # 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
  1235
        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
  1236
        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
  1237
        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
  1238
        f.close()
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1239
        # 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
  1240
        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
  1241
        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
  1242
        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
  1243
        f.close()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1244
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1245
        LDFLAGS = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1246
        LDFLAGS.append(" \"-L" + ModbusPath + "\"")
2333
81abf93b4684 Path may contain spaces... sequel of d1359cb
Edouard Tisserant
parents: 2291
diff changeset
  1247
        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
  1248
        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
  1249
        # 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
  1250
        # 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
  1251
        # 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
  1252
        # 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
  1253
        # 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
  1254
        # 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
  1255
        # 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
  1256
        # 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
  1257
        # 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
  1258
        # 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
  1259
        # library!
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1260
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1261
        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
  1262
        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
  1263
        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
  1264
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1265
        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
  1266
        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
  1267
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1268
        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
  1269
        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
  1270
        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
  1271
        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
  1272
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1273
        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
  1274
                ("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
  1275
        )