modbus/modbus.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 09 Apr 2019 13:08:03 +0200
changeset 2584 adfdeaba5a6a
parent 2459 21164625b393
child 2589 d94d4f396bbf
permissions -rw-r--r--
merge
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
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    33
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    34
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
    35
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
    36
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
    37
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    38
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    39
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    40
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    41
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    42
# 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
    43
#
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
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    46
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    48
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
    49
    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
    50
    <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
    51
      <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
    52
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
          <xsd:attribute name="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
    54
          <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
    55
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
                <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
    57
                    <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
    58
                    <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
    59
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    60
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    61
          </xsd:attribute>
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 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
    63
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    64
                <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
    65
                    <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
    66
                    <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
    67
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    68
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    69
          </xsd:attribute>
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 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
    71
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    72
                <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
    73
                    <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
    74
                    <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
    75
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    76
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
          </xsd:attribute>
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 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
    79
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    80
                <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
    81
                    <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
    82
                    <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
    83
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    84
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    85
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    87
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    88
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    90
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    91
    def GetParamsAttributes(self, path=None):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    92
        infos = ConfigTreeNode.GetParamsAttributes(self, path=path)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    93
        for element in infos:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    94
            if element["name"] == "ModbusRequest":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    95
                for child in element["children"]:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    96
                    if child["name"] == "Function":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    97
                        list = modbus_function_dict.keys()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    98
                        list.sort()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    99
                        child["type"] = list
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   100
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   101
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   102
    def GetVariableLocationTree(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   103
        current_location = self.GetCurrentLocation()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   104
        name = self.BaseParams.getName()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   105
        address = self.GetParamsAttributes()[0]["children"][3]["value"]
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   106
        count = self.GetParamsAttributes()[0]["children"][2]["value"]
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   107
        function = self.GetParamsAttributes()[0]["children"][0]["value"]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   108
        # 'BOOL' or 'WORD'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   109
        datatype = modbus_function_dict[function][3]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   110
        # 1 or 16
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   111
        datasize = modbus_function_dict[function][4]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   112
        # 'Q' for coils and holding registers, 'I' for input discretes and input registers
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   113
        # datazone = modbus_function_dict[function][5]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   114
        # 'X' for bits, 'W' for words
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   115
        datatacc = modbus_function_dict[function][6]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   116
        # 'Coil', 'Holding Register', 'Input Discrete' or 'Input Register'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   117
        dataname = modbus_function_dict[function][7]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   118
        entries = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   119
        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
   120
            entries.append({
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   121
                "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
   122
                "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
   123
                "size": datasize,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   124
                "IEC_type": datatype,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   125
                "var_name": "var_name",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   126
                "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
   127
                "description": "description",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   128
                "children": []})
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   129
        return {"name": name,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   130
                "type": LOCATION_CONFNODE,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   131
                "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
   132
                "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
   133
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   134
    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
   135
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   136
        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
   137
        @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
   138
        @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
   139
            [{"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
   140
            "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
   141
            "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
   142
            "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
   143
            "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
   144
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   145
        @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
   146
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   147
        return [], "", False
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   148
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   149
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   150
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   151
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   152
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   153
# 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
   154
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   155
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   156
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   157
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   158
# dictionary implementing:
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   159
# 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
   160
# 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
   161
modbus_memtype_dict = {
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   162
    "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
   163
    "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
   164
    "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
   165
    "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
   166
}
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   167
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   168
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   169
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
   170
    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
   171
    <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
   172
      <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
   173
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   174
          <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
   175
          <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
   176
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   177
                <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
   178
                    <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
   179
                    <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
   180
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   181
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   182
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   183
          <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
   184
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   185
                <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
   186
                    <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
   187
                    <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
   188
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   189
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   190
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   191
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   192
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   193
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   194
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   195
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   196
    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
   197
        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
   198
        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
   199
            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
   200
                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
   201
                    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
   202
                        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
   203
                        list.sort()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   204
                        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
   205
        return infos
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   206
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   207
    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
   208
        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
   209
        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
   210
        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
   211
        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
   212
        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
   213
        # '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
   214
        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
   215
        # 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
   216
        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
   217
        # '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
   218
        # 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
   219
        # '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
   220
        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
   221
        # '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
   222
        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
   223
        entries = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   224
        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
   225
            entries.append({
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   226
                "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
   227
                "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
   228
                "size": datasize,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   229
                "IEC_type": datatype,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   230
                "var_name": "var_name",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   231
                "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
   232
                "description": "description",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   233
                "children": []})
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   234
        return {"name": name,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   235
                "type": LOCATION_CONFNODE,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   236
                "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
   237
                "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
   238
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   239
    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
   240
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   241
        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
   242
        @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
   243
        @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
   244
            [{"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
   245
            "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
   246
            "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
   247
            "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
   248
            "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
   249
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   250
        @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
   251
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   252
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   253
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   254
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   255
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   256
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   257
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   258
# 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
   259
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   260
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   261
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   262
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   263
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
   264
    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
   265
    <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
   266
      <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
   267
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   268
          <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
   269
          <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
   270
          <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
   271
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   272
                <xsd:restriction base="xsd:unsignedLong">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   273
                    <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
   274
                    <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
   275
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   276
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   277
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   278
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   279
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   280
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   281
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   282
    # 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
   283
    # 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
   284
    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
   285
    # 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
   286
    PlugType = "ModbusTCPclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   287
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   288
    # 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
   289
    #   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
   290
    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
   291
        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
   292
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   293
    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
   294
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   295
        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
   296
        @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
   297
        @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
   298
            [{"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
   299
            "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
   300
            "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
   301
            "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
   302
            "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
   303
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   304
        @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
   305
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   306
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   307
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   308
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   309
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   310
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   311
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   312
# 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
   313
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   314
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   315
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   316
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   317
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
   318
    # 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
   319
    # 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
   320
    # (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
   321
    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
   322
    <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
   323
      <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
   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: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
   326
          <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
   327
          <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
   328
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   329
                <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
   330
                    <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
   331
                    <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
   332
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   333
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   334
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   335
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   336
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   337
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   338
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   339
    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
   340
    # 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
   341
    PlugType = "ModbusTCPserver"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   342
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   343
    # 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
   344
    #   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
   345
    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
   346
        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
   347
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   348
    # Return a list with a single tuple conatining the (location, port number)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   349
    #     location: location of this node in the configuration tree
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   350
    #     port number: IP port used by this Modbus/IP server
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   351
    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
   352
        port = self.GetParamsAttributes()[0]["children"][1]["value"]
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   353
        return [(self.GetCurrentLocation(), port)]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   354
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   355
    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
   356
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   357
        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
   358
        @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
   359
        @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
   360
            [{"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
   361
            "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
   362
            "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
   363
            "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
   364
            "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
   365
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   366
        @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
   367
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   368
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   369
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   370
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   371
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   372
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   373
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   374
# 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
   375
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   376
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   377
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   378
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   379
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
   380
    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
   381
    <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
   382
      <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
   383
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   384
          <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
   385
          <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
   386
          <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
   387
          <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
   388
          <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
   389
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   390
                <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
   391
                    <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
   392
                    <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
   393
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   394
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   395
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   396
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   397
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   398
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   399
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   400
    # 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
   401
    # 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
   402
    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
   403
    # 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
   404
    PlugType = "ModbusRTUclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   405
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   406
    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
   407
        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
   408
        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
   409
            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
   410
                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
   411
                    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
   412
                        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
   413
                    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
   414
                        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
   415
                    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
   416
                        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
   417
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   418
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   419
    # 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
   420
    #   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
   421
    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
   422
        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
   423
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   424
    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
   425
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   426
        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
   427
        @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
   428
        @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
   429
            [{"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
   430
            "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
   431
            "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
   432
            "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
   433
            "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
   434
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   435
        @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
   436
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   437
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   438
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   439
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   440
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   441
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   442
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   443
# 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
   444
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   445
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   446
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   447
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   448
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   449
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
   450
    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
   451
    <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
   452
      <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
   453
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   454
          <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
   455
          <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
   456
          <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
   457
          <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
   458
          <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
   459
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   460
                <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
   461
                    <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
   462
                    <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
   463
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   464
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   465
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   466
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   467
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   468
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   469
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   470
    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
   471
    # 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
   472
    PlugType = "ModbusRTUslave"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   473
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   474
    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
   475
        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
   476
        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
   477
            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
   478
                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
   479
                    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
   480
                        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
   481
                    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
   482
                        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
   483
                    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
   484
                        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
   485
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   486
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   487
    # 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
   488
    #   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
   489
    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
   490
        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
   491
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   492
    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
   493
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   494
        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
   495
        @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
   496
        @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
   497
            [{"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
   498
            "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
   499
            "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
   500
            "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
   501
            "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
   502
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   503
        @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
   504
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   505
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   506
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   507
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   508
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
   509
    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
   510
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   511
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   512
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   513
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   514
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   515
# 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
   516
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   517
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   518
#
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   519
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
   520
    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
   521
    <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
   522
      <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
   523
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   524
          <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
   525
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   526
                <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
   527
                    <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
   528
                    <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
   529
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   530
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   531
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   532
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   533
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   534
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   535
    """
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   536
    CTNChildrenTypes = [("ModbusTCPclient", _ModbusTCPclientPlug, "Modbus TCP Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   537
                        ("ModbusTCPserver", _ModbusTCPserverPlug, "Modbus TCP Server"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   538
                        ("ModbusRTUclient", _ModbusRTUclientPlug, "Modbus RTU Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   539
                        ("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
   540
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   541
    # 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
   542
    #   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
   543
    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
   544
        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
   545
            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
   546
        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
   547
        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
   548
            # 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
   549
            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
   550
                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
   551
        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
   552
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   553
    # Return a list with tuples of the (location, port numbers) used by all
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   554
    # 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
   555
    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
   556
        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
   557
        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
   558
            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
   559
                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
   560
        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
   561
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   562
    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
   563
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   564
        # print self.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   565
        # print type(self)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   566
        # print "self.CTNType >>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   567
        # print self.CTNType
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   568
        # 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
   569
        # 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
   570
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   571
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   572
        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
   573
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   574
        # 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
   575
        #   total_node_count: (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
   576
        # Also get a list with tuples of (location, IP port numbers) used by all the Modbus/IP server nodes
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   577
        #   This list is later used to search for duplicates in port numbers!
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   578
        #   IPServer_port_numbers = [(location ,IPserver_port_number), ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   579
        #       location: tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   580
        # IPserver_port_number: a number (i.e. port number used by the
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   581
        # Modbus/IP server)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   582
        total_node_count = (0, 0, 0)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   583
        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
   584
        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
   585
            if CTNInstance.CTNType == "modbus":
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   586
                # ask each modbus plugin instance how many nodes it needs, and
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   587
                # add them all up.
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   588
                total_node_count = tuple(x1 + x2 for x1, x2 in zip(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   589
                    total_node_count, CTNInstance.GetNodeCount()))
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   590
                IPServer_port_numbers.extend(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   591
                    CTNInstance.GetIPServerPortNumbers())
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   592
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   593
        # Search for use of duplicate port numbers by Modbus/IP servers
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   594
        # print IPServer_port_numbers
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   595
        # ..but first define a lambda function to convert a tuple with the config tree location to a nice looking string
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   596
        #   for e.g., convert the tuple (0, 3, 4) to "0.3.4"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   597
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   598
        for i in range(0, len(IPServer_port_numbers) - 1):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   599
            for j in range(i + 1, len(IPServer_port_numbers)):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   600
                if IPServer_port_numbers[i][1] == IPServer_port_numbers[j][1]:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   601
                    self.GetCTRoot().logger.write_warning(
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2179
diff changeset
   602
                        _("Error: Modbus/IP Servers %{a1}.x and %{a2}.x use the same port number {a3}.\n").
2179
84c4e56b38d6 Fix internatialization in UriLocationEditor, PYRO dialog and Modbus configurator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1919
diff changeset
   603
                        format(
84c4e56b38d6 Fix internatialization in UriLocationEditor, PYRO dialog and Modbus configurator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1919
diff changeset
   604
                            a1=_lt_to_str(IPServer_port_numbers[i][0]),
84c4e56b38d6 Fix internatialization in UriLocationEditor, PYRO dialog and Modbus configurator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1919
diff changeset
   605
                            a2=_lt_to_str(IPServer_port_numbers[j][0]),
84c4e56b38d6 Fix internatialization in UriLocationEditor, PYRO dialog and Modbus configurator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1919
diff changeset
   606
                            a3=IPServer_port_numbers[j][1]))
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   607
                    raise Exception
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   608
                    # TODO: return an error code instead of raising an
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   609
                    # exception
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   610
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   611
        # 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
   612
        # tree
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   613
        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
   614
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   615
        # 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
   616
        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
   617
        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
   618
        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
   619
        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
   620
        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
   621
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   622
        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
   623
        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
   624
        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
   625
        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
   626
        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
   627
        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
   628
        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
   629
        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
   630
        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
   631
        nodeid = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   632
        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
   633
        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
   634
        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
   635
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   636
        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
   637
        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
   638
        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
   639
        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
   640
        loc_vars = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   641
        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
   642
        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
   643
            # print "<<<<<<<<<<<<<"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   644
            # 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
   645
            # print child.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   646
            # print ">>>>>>>>>>>>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   647
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   648
            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
   649
                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
   650
                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
   651
                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
   652
                    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
   653
                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
   654
                #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   655
                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
   656
                    new_memarea = GetTCPServerMemAreaPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   657
                        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
   658
                    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
   659
                        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
   660
                    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
   661
                    function = subchild.GetParamsAttributes()[
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   662
                        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
   663
                    # '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
   664
                    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
   665
                    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
   666
                        # 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
   667
                        absloute_address = iecvar["LOC"][3]
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   668
                        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
   669
                        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
   670
                        # 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
   671
                        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
   672
                            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
   673
                                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
   674
                                    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
   675
                                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
   676
                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
   677
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   678
            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
   679
                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
   680
                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
   681
                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
   682
                    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
   683
                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
   684
                #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   685
                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
   686
                    new_memarea = GetTCPServerMemAreaPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   687
                        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
   688
                    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
   689
                        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
   690
                    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
   691
                    function = subchild.GetParamsAttributes()[
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   692
                        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
   693
                    # '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
   694
                    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
   695
                    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
   696
                        # 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
   697
                        absloute_address = iecvar["LOC"][3]
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   698
                        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
   699
                        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
   700
                        # 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
   701
                        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
   702
                            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
   703
                                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
   704
                                    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
   705
                                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
   706
                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
   707
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   708
            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
   709
                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
   710
                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
   711
                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
   712
                    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
   713
                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
   714
                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
   715
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   716
                        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
   717
                    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
   718
                        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
   719
                    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
   720
                    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
   721
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   722
                        relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   723
                        # 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
   724
                        if relative_addr in xrange(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
   725
                            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
   726
                                loc_vars.append(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   727
                                    "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
   728
                                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
   729
                    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
   730
                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
   731
                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
   732
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   733
            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
   734
                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
   735
                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
   736
                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
   737
                    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
   738
                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
   739
                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
   740
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   741
                        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
   742
                    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
   743
                        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
   744
                    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
   745
                    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
   746
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   747
                        relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   748
                        # 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
   749
                        if relative_addr in xrange(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
   750
                            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
   751
                                loc_vars.append(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   752
                                    "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
   753
                                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
   754
                    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
   755
                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
   756
                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
   757
            nodeid += 1
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   758
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   759
        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
   760
        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
   761
        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
   762
        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
   763
        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
   764
        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
   765
        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
   766
        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
   767
        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
   768
        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
   769
        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
   770
        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
   771
        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
   772
        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
   773
        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
   774
        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
   775
        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
   776
            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
   777
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   778
        # 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
   779
        # 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
   780
        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
   781
        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
   782
        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
   783
        f.close()
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   784
        # 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
   785
        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
   786
        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
   787
        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
   788
        f.close()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   789
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   790
        LDFLAGS = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   791
        LDFLAGS.append(" \"-L" + ModbusPath + "\"")
2333
81abf93b4684 Path may contain spaces... sequel of d1359cb
Edouard Tisserant
parents: 2291
diff changeset
   792
        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
   793
        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
   794
        # 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
   795
        # 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
   796
        # 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
   797
        # 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
   798
        # 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
   799
        # 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
   800
        # 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
   801
        # 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
   802
        # 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
   803
        # 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
   804
        # library!
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   805
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   806
        return [(Gen_MB_c_path, ' -I"' + ModbusPath + '"')], LDFLAGS, True