modbus/modbus.py
author Edouard Tisserant
Fri, 12 Mar 2021 15:12:38 +0100
branchsvghmi
changeset 3187 ba8ff21fd82b
parent 2719 745b64e7c695
child 2724 3f3f9dce9140
permissions -rw-r--r--
SVGHMI: change default values for JsonTable, leading to scrollbar initialized at annoying non-zero position
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
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    35
base_folder = os.path.split(os.path.dirname(os.path.realpath(__file__)))[0]
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    36
base_folder = os.path.join(base_folder, "..")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    37
ModbusPath = os.path.join(base_folder, "Modbus")
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
#
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
# 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
    44
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    45
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    46
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    49
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
    50
    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
    51
    <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
    52
      <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
    53
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
          <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
    55
          <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
    56
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
                <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
    58
                    <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
    59
                    <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
    60
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    61
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    62
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    63
          <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
    64
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    65
                <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
    66
                    <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
    67
                    <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
    68
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    69
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    70
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    71
          <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
    72
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    73
                <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
    74
                    <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
    75
                    <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
    76
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    78
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    79
          <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
    80
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    81
                <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
    82
                    <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
    83
                    <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
    84
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    85
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
          </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
    87
          <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
    88
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    90
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    91
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    92
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    93
    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
    94
        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
    95
        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
    96
            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
    97
                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
    98
                    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
    99
                        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
   100
                        list.sort()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   101
                        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
   102
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   103
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   104
    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
   105
        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
   106
        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
   107
        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
   108
        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
   109
        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
   110
        # '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
   111
        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
   112
        # 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
   113
        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
   114
        # '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
   115
        # 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
   116
        # '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
   117
        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
   118
        # '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
   119
        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
   120
        # 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
   121
        # 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
   122
        # 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
   123
        # 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
   124
        #          %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
   125
        #       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
   126
        #          %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
   127
        #       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
   128
        #          %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
   129
        #          %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
   130
        #          %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
   131
        #            ..
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   132
        #          %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
   133
        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
   134
        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
   135
            "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
   136
            "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
   137
            "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
   138
            "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
   139
            "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
   140
            "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
   141
            "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
   142
            "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
   143
        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
   144
            "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
   145
            "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
   146
            "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
   147
            "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
   148
            "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
   149
            "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
   150
            "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
   151
            "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
   152
        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
   153
            "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
   154
            "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
   155
            "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
   156
            "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
   157
            "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
   158
            "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
   159
            "description": "Modbus Error Code received in Modbus 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
   160
            "children": []})        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   161
        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
   162
            entries.append({
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   163
                "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
   164
                "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
   165
                "size": datasize,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   166
                "IEC_type": datatype,
2589
d94d4f396bbf Nicer name than 'var_name' for variable auto-renaming in Modbus
Edouard Tisserant
parents: 2459
diff changeset
   167
                "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
   168
                "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
   169
                "description": "description",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   170
                "children": []})
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   171
        return {"name": name,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   172
                "type": LOCATION_CONFNODE,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   173
                "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
   174
                "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
   175
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   176
    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
   177
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   178
        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
   179
        @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
   180
        @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
   181
            [{"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
   182
            "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
   183
            "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
   184
            "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
   185
            "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
   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: [(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
   188
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   189
        return [], "", False
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   190
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   191
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
   192
1918
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
#
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
# 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
   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
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   200
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   201
# dictionary implementing:
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   202
# 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
   203
# 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
   204
modbus_memtype_dict = {
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   205
    "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
   206
    "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
   207
    "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
   208
    "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
   209
}
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   210
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   211
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   212
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
   213
    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
   214
    <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
   215
      <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
   216
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   217
          <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
   218
          <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
   219
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   220
                <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
   221
                    <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
   222
                    <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
   223
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   224
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   225
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   226
          <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
   227
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   228
                <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
   229
                    <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
   230
                    <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
   231
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   232
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   233
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   234
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   235
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   236
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   237
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   238
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   239
    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
   240
        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
   241
        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
   242
            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
   243
                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
   244
                    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
   245
                        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
   246
                        list.sort()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   247
                        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
   248
        return infos
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   249
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   250
    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
   251
        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
   252
        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
   253
        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
   254
        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
   255
        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
   256
        # '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
   257
        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
   258
        # 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
   259
        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
   260
        # '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
   261
        # 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
   262
        # '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
   263
        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
   264
        # '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
   265
        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
   266
        entries = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   267
        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
   268
            entries.append({
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   269
                "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
   270
                "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
   271
                "size": datasize,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   272
                "IEC_type": datatype,
2589
d94d4f396bbf Nicer name than 'var_name' for variable auto-renaming in Modbus
Edouard Tisserant
parents: 2459
diff changeset
   273
                "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
   274
                "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
   275
                "description": "description",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   276
                "children": []})
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   277
        return {"name": name,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   278
                "type": LOCATION_CONFNODE,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   279
                "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
   280
                "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
   281
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   282
    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
   283
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   284
        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
   285
        @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
   286
        @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
   287
            [{"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
   288
            "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
   289
            "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
   290
            "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
   291
            "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
   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: [(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
   294
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   295
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   296
1918
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
#
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
# 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
   302
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   303
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   304
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   305
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   306
# 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
   307
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   308
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
   309
    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
   310
    <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
   311
      <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
   312
        <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
   313
          <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
   314
          <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
   315
          <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
   316
          <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
   317
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   318
                <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
   319
                    <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
   320
                    <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
   321
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   322
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   323
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   324
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   325
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   326
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   327
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   328
    # NOTE: Max value of 2147483647 (i32_max) for Invocation_Rate_in_ms
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   329
    # 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
   330
    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
   331
    # 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
   332
    PlugType = "ModbusTCPclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   333
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
   334
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   335
    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
   336
        # 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
   337
        # 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
   338
        # 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
   339
        # 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
   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
        # 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
   342
        # 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
   343
        # _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
   344
        # 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
   345
        #   (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
   346
        #   (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
   347
        #   (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
   348
        #   (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
   349
        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
   350
        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
   351
        
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   352
    # 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
   353
    #   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
   354
    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
   355
        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
   356
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   357
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   358
        """ 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
   359
        return self.ModbusTCPclient.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   360
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   361
    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
   362
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   363
        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
   364
        @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
   365
        @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
   366
            [{"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
   367
            "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
   368
            "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
   369
            "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
   370
            "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
   371
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   372
        @return: [(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
   373
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   374
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   375
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   376
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   377
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   378
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   379
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   380
# 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
   381
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   382
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   383
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   384
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   385
# 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
   386
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   387
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
   388
    # 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
   389
    # 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
   390
    # (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
   391
    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
   392
    <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
   393
      <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
   394
        <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
   395
          <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
   396
          <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
   397
          <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
   398
          <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
   399
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   400
                <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
   401
                    <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
   402
                    <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
   403
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   404
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   405
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   406
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   407
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   408
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   409
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   410
    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
   411
    # 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
   412
    PlugType = "ModbusTCPserver"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   413
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
   414
    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
   415
        # 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
   416
        # 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
   417
        # 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
   418
        # 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
   419
        
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
        # 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
   421
        # 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
   422
        # _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
   423
        # 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
   424
        #   (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
   425
        #   (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
   426
        #   (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
   427
        #   (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
   428
        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
   429
        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
   430
        
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   431
    # 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
   432
    #   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
   433
    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
   434
        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
   435
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
   436
    # 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
   437
    #     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
   438
    #     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
   439
    #     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
   440
    #                  ("", "*", 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
   441
    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
   442
        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
   443
        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
   444
        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
   445
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   446
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   447
        """ 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
   448
        return self.ModbusServerNode.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   449
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   450
    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
   451
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   452
        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
   453
        @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
   454
        @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
   455
            [{"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
   456
            "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
   457
            "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
   458
            "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
   459
            "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
   460
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   461
        @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
   462
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   463
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   464
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   465
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   466
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   467
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   468
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   469
# 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
   470
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   471
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   472
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   473
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   474
# 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
   475
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   476
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
   477
    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
   478
    <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
   479
      <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
   480
        <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
   481
          <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
   482
          <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
   483
          <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
   484
          <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
   485
          <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
   486
          <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
   487
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   488
                <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
   489
                    <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
   490
                    <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
   491
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   492
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   493
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   494
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   495
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   496
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   497
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   498
    # NOTE: Max value of 2147483647 (i32_max) for Invocation_Rate_in_ms
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   499
    # 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
   500
    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
   501
    # 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
   502
    PlugType = "ModbusRTUclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   503
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
   504
    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
   505
        # 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
   506
        # 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
   507
        # 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
   508
        # 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
   509
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   510
        # 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
   511
        # 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
   512
        # _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
   513
        # 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
   514
        #   (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
   515
        #   (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
   516
        #   (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
   517
        #   (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
   518
        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
   519
        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
   520
        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   521
    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
   522
        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
   523
        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
   524
            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
   525
                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
   526
                    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
   527
                        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
   528
                    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
   529
                        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
   530
                    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
   531
                        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
   532
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   533
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   534
    # 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
   535
    #   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
   536
    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
   537
        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
   538
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   539
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   540
        """ 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
   541
        return self.ModbusRTUclient.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   542
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   543
    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
   544
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   545
        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
   546
        @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
   547
        @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
   548
            [{"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
   549
            "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
   550
            "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
   551
            "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
   552
            "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
   553
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   554
        @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
   555
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   556
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   557
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   558
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   559
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   560
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   561
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   562
# 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
   563
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   564
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   565
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   566
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   567
# 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
   568
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   569
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
   570
    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
   571
    <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
   572
      <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
   573
        <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
   574
          <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
   575
          <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
   576
          <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
   577
          <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
   578
          <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
   579
          <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
   580
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   581
                <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
   582
                    <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
   583
                    <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
   584
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   585
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   586
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   587
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   588
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   589
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   590
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   591
    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
   592
    # 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
   593
    PlugType = "ModbusRTUslave"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   594
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
   595
    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
   596
        # 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
   597
        # 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
   598
        # 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
   599
        # 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
   600
        
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
        # 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
   602
        # 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
   603
        # _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
   604
        # 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
   605
        #   (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
   606
        #   (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
   607
        #   (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
   608
        #   (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
   609
        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
   610
        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
   611
        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   612
    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
   613
        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
   614
        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
   615
            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
   616
                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
   617
                    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
   618
                        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
   619
                    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
   620
                        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
   621
                    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
   622
                        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
   623
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   624
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   625
    # 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
   626
    #   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
   627
    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
   628
        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
   629
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   630
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   631
        """ 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
   632
        return self.ModbusRTUslave.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   633
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   634
    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
   635
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   636
        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
   637
        @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
   638
        @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
   639
            [{"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
   640
            "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
   641
            "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
   642
            "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
   643
            "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
   644
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   645
        @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
   646
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   647
        return [], "", False
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
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
   651
    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
   652
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
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   656
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   657
# 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
   658
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   659
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   660
#
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   661
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
   662
    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
   663
    <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
   664
      <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
   665
        <xsd:complexType>
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="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
   667
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   668
                <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
   669
                    <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
   670
                    <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
   671
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   672
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   673
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   674
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   675
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   676
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   677
    """
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   678
    CTNChildrenTypes = [("ModbusTCPclient", _ModbusTCPclientPlug, "Modbus TCP Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   679
                        ("ModbusTCPserver", _ModbusTCPserverPlug, "Modbus TCP Server"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   680
                        ("ModbusRTUclient", _ModbusRTUclientPlug, "Modbus RTU Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   681
                        ("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
   682
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   683
    # 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
   684
    #   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
   685
    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
   686
        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
   687
            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
   688
        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
   689
        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
   690
            # 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
   691
            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
   692
                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
   693
        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
   694
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   695
    # 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
   696
    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
   697
        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
   698
        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
   699
            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
   700
                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
   701
        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
   702
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   703
    # 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
   704
    def GetConfigNames(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   705
        Node_Configuration_Names = []
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   706
        for child in self.IECSortedChildren():
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   707
            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
   708
        return Node_Configuration_Names
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   709
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   710
    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
   711
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   712
        # print self.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   713
        # print type(self)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   714
        # print "self.CTNType >>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   715
        # print self.CTNType
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   716
        # 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
   717
        # 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
   718
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   719
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   720
        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
   721
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   722
        # 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
   723
        #   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
   724
        #
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
   725
        # 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
   726
        #   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
   727
        #   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
   728
        #       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
   729
        #       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
   730
        #       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
   731
        #                             ("", "*", 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
   732
        #
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   733
        # 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
   734
        #   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
   735
        #   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
   736
        #       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
   737
        #       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
   738
        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
   739
        IPServer_port_numbers    = []
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   740
        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
   741
        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
   742
            if CTNInstance.CTNType == "modbus":
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   743
                # 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
   744
                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
   745
                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
   746
                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
   747
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   748
        # 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
   749
        # 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
   750
        # (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
   751
        # 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
   752
        # 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
   753
        # 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
   754
        # distinguasheble on the web interface!
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   755
        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
   756
            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
   757
                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
   758
                    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
   759
                                        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
   760
                                        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
   761
                                        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
   762
                    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
   763
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   764
        # 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
   765
        # 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
   766
        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
   767
        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
   768
            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
   769
            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
   770
                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
   771
                          (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
   772
                       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
   773
                       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
   774
                   ):
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
   775
                    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
   776
                                      "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
   777
                                        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
   778
                    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
   779
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   780
        # 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
   781
        # tree
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   782
        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
   783
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   784
        # 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
   785
        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
   786
        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
   787
        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
   788
        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
   789
        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
   790
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   791
        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
   792
        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
   793
        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
   794
        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
   795
        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
   796
        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
   797
        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
   798
        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
   799
        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
   800
        nodeid = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   801
        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
   802
        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
   803
        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
   804
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   805
        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
   806
        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
   807
        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
   808
        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
   809
        loc_vars = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   810
        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
   811
        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
   812
            # print "<<<<<<<<<<<<<"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   813
            # 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
   814
            # print child.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   815
            # print ">>>>>>>>>>>>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   816
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   817
            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
   818
                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
   819
                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
   820
                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
   821
                    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
   822
                server_node_list.append(new_node)
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   823
                #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   824
                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
   825
                    new_memarea = GetTCPServerMemAreaPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   826
                        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
   827
                    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
   828
                        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
   829
                    server_memarea_list.append(new_memarea)
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   830
                    function = subchild.GetParamsAttributes()[
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   831
                        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
   832
                    # '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
   833
                    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
   834
                    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
   835
                        # print repr(iecvar)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   836
                        absloute_address = iecvar["LOC"][3]
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   837
                        start_address = int(GetCTVal(subchild, 2))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   838
                        relative_addr = absloute_address - start_address
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   839
                        # test if relative address in request specified range
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   840
                        if relative_addr in xrange(int(GetCTVal(subchild, 1))):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   841
                            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
   842
                                loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.%s[%d];" % (
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   843
                                    server_id, memarea, absloute_address))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   844
                                loc_vars_list.append(str(iecvar["NAME"]))
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   845
                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
   846
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   847
            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
   848
                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
   849
                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
   850
                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
   851
                    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
   852
                server_node_list.append(new_node)
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   853
                #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   854
                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
   855
                    new_memarea = GetTCPServerMemAreaPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   856
                        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
   857
                    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
   858
                        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
   859
                    server_memarea_list.append(new_memarea)
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   860
                    function = subchild.GetParamsAttributes()[
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   861
                        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
   862
                    # '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
   863
                    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
   864
                    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
   865
                        # print repr(iecvar)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   866
                        absloute_address = iecvar["LOC"][3]
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   867
                        start_address = int(GetCTVal(subchild, 2))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   868
                        relative_addr = absloute_address - start_address
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   869
                        # test if relative address in request specified range
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   870
                        if relative_addr in xrange(int(GetCTVal(subchild, 1))):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   871
                            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
   872
                                loc_vars.append("u16 *" + str(iecvar["NAME"]) + " = &server_nodes[%d].mem_area.%s[%d];" % (
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   873
                                    server_id, memarea, absloute_address))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   874
                                loc_vars_list.append(str(iecvar["NAME"]))
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   875
                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
   876
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   877
            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
   878
                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
   879
                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
   880
                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
   881
                    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
   882
                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
   883
                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
   884
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   885
                        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
   886
                    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
   887
                        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
   888
                    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
   889
                    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
   890
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   891
                        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
   892
                        # 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
   893
                        #    (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
   894
                        #  AND is NOT
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
   895
                        #    (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
   896
                        #        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
   897
                        #        and another two status flags.
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
   898
                        #        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
   899
                        #        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
   900
                        #        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
   901
                        #        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
   902
                        #        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
   903
                        #        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
   904
                        #        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
   905
                        #        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
   906
                        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
   907
                            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
   908
                            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
   909
                                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
   910
                                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
   911
                        # 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
   912
                        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
   913
                            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
   914
                                # 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
   915
                                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
   916
                                    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
   917
                                    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
   918
                                # 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
   919
                                #    -> 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
   920
                                #         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
   921
                                #         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
   922
                                #         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
   923
                                #         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
   924
                                #    -> 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
   925
                                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
   926
                                    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
   927
                                    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
   928
                                # 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
   929
                                #    -> 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
   930
                                #    -> 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
   931
                                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
   932
                                    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
   933
                                    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
   934
                    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
   935
                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
   936
                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
   937
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   938
            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
   939
                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
   940
                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
   941
                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
   942
                    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
   943
                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
   944
                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
   945
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   946
                        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
   947
                    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
   948
                        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
   949
                    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
   950
                    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
   951
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   952
                        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
   953
                        # 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
   954
                        #    (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
   955
                        #  AND is NOT
2717
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
   956
                        #    (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
   957
                        #        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
   958
                        #        and another two status flags.
b3b6991f1cb6 modbus plugin: add/fix comments
Mario de Sousa <msousa@fe.up.pt>
parents: 2715
diff changeset
   959
                        #        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
   960
                        #        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
   961
                        #        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
   962
                        #        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
   963
                        #        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
   964
                        #        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
   965
                        #        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
   966
                        #        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
   967
                        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
   968
                            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
   969
                            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
   970
                                loc_vars.append(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   971
                                    "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
   972
                                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
   973
                        # 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
   974
                        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
   975
                            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
   976
                                # 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
   977
                                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
   978
                                    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
   979
                                    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
   980
                                # 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
   981
                                #    -> 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
   982
                                #         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
   983
                                #         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
   984
                                #         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
   985
                                #         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
   986
                                #    -> 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
   987
                                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
   988
                                    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
   989
                                    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
   990
                                # 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
   991
                                #    -> 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
   992
                                #    -> 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
   993
                                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
   994
                                    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
   995
                                    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
   996
                    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
   997
                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
   998
                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
   999
            nodeid += 1
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1000
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1001
        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
  1002
        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
  1003
        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
  1004
        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
  1005
        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
  1006
        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
  1007
        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
  1008
        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
  1009
        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
  1010
        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
  1011
        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
  1012
        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
  1013
        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
  1014
        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
  1015
        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
  1016
        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
  1017
        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
  1018
            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
  1019
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1020
        # 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
  1021
        # 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
  1022
        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
  1023
        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
  1024
        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
  1025
        f.close()
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1026
        # 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
  1027
        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
  1028
        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
  1029
        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
  1030
        f.close()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1031
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1032
        LDFLAGS = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
  1033
        LDFLAGS.append(" \"-L" + ModbusPath + "\"")
2333
81abf93b4684 Path may contain spaces... sequel of d1359cb
Edouard Tisserant
parents: 2291
diff changeset
  1034
        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
  1035
        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
  1036
        # 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
  1037
        # 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
  1038
        # 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
  1039
        # 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
  1040
        # 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
  1041
        # 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
  1042
        # 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
  1043
        # 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
  1044
        # 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
  1045
        # 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
  1046
        # library!
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
  1047
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1048
        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
  1049
        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
  1050
        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
  1051
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1052
        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
  1053
        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
  1054
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1055
        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
  1056
        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
  1057
        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
  1058
        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
  1059
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1060
        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
  1061
                ("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
  1062
        )