modbus/modbus.py
author usveticic
Thu, 01 Oct 2020 14:23:27 +0200
branchsvghmi
changeset 3062 9ec338a99a18
parent 2993 b76f303ffce6
child 2719 745b64e7c695
permissions -rw-r--r--
Button fix if no active or inactive state,
Widget animate changed to use anitmateTransform and added option to change rotation
Widget circular slider fixed so it is working on got and reprogramed so it similar to normal slider
Widget slider added support for changing size still need some changes to work properly
Added slider to svghmi test project
Changed svg in svhgmi_v2 project
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     1
#!/usr/bin/env python
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     3
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     6
#
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     7
# Copyright (c) 2016 Mario de Sousa (msousa@fe.up.pt)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     8
#
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     9
# This program is free software: you can redistribute it and/or modify
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    10
# it under the terms of the GNU General Public License as published by
2019
92f02bb17c7e Make Modbus extension license compatible with the rest of Beremiz IDE python code : GPLv2.
Edouard Tisserant
parents: 1919
diff changeset
    11
# the Free Software Foundation, either version 2 of the License, or
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    12
# (at your option) any later version.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    13
#
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    14
# This program is distributed in the hope that it will be useful,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    17
# GNU General Public License for more details.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    18
#
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    19
# You should have received a copy of the GNU General Public License
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    21
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    22
# This code is made available on the understanding that it will not be
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    23
# used in safety-critical situations without a full and competent review.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    24
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    25
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    26
from __future__ import absolute_import
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    27
import os
2432
dbc065a2f7a5 python3 support: pylint, W1613 # (xrange-builtin) xrange built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2291
diff changeset
    28
from six.moves import xrange
dbc065a2f7a5 python3 support: pylint, W1613 # (xrange-builtin) xrange built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2291
diff changeset
    29
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    30
from modbus.mb_utils import *
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    31
from ConfigTreeNode import ConfigTreeNode
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    32
from PLCControler import LOCATION_CONFNODE, LOCATION_VAR_MEMORY
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
    33
import util.paths as paths
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    34
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    35
base_folder = os.path.split(os.path.dirname(os.path.realpath(__file__)))[0]
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    36
base_folder = os.path.join(base_folder, "..")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    37
ModbusPath = os.path.join(base_folder, "Modbus")
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    38
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    39
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    40
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    41
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    42
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    43
# C L I E N T    R E Q U E S T            #
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    44
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    45
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    46
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    49
class _RequestPlug(object):
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    51
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    52
      <xsd:element name="ModbusRequest">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
          <xsd:attribute name="Function" type="xsd:string" use="optional" default="01 - Read Coils"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    55
          <xsd:attribute name="SlaveID" use="optional" default="1">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    58
                    <xsd:minInclusive value="0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    59
                    <xsd:maxInclusive value="255"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    60
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    61
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    62
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    63
          <xsd:attribute name="Nr_of_Channels" use="optional" default="1">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    64
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    65
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    66
                    <xsd:minInclusive value="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    67
                    <xsd:maxInclusive value="2000"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    68
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    69
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    70
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    71
          <xsd:attribute name="Start_Address" use="optional" default="0">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    72
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    73
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    74
                    <xsd:minInclusive value="0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    75
                    <xsd:maxInclusive value="65535"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    76
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    78
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    79
          <xsd:attribute name="Timeout_in_ms" use="optional" default="10">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    80
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    81
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    82
                    <xsd:minInclusive value="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    83
                    <xsd:maxInclusive value="100000"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    84
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    85
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
          </xsd:attribute>
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
    87
          <xsd:attribute name="Write_on_change" type="xsd:boolean" use="optional" default="false"/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    88
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    90
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    91
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    92
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    93
    def GetParamsAttributes(self, path=None):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
    94
        infos = ConfigTreeNode.GetParamsAttributes(self, path=path)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    95
        for element in infos:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    96
            if element["name"] == "ModbusRequest":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    97
                for child in element["children"]:
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    98
                    if child["name"] == "Function":
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    99
                        list = modbus_function_dict.keys()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   100
                        list.sort()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   101
                        child["type"] = list
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   102
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   103
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   104
    def GetVariableLocationTree(self):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   105
        current_location = self.GetCurrentLocation()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   106
        name = self.BaseParams.getName()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   107
        address = self.GetParamsAttributes()[0]["children"][3]["value"]
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   108
        count = self.GetParamsAttributes()[0]["children"][2]["value"]
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   109
        function = self.GetParamsAttributes()[0]["children"][0]["value"]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   110
        # 'BOOL' or 'WORD'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   111
        datatype = modbus_function_dict[function][3]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   112
        # 1 or 16
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   113
        datasize = modbus_function_dict[function][4]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   114
        # 'Q' for coils and holding registers, 'I' for input discretes and input registers
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   115
        # datazone = modbus_function_dict[function][5]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   116
        # 'X' for bits, 'W' for words
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   117
        datatacc = modbus_function_dict[function][6]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   118
        # 'Coil', 'Holding Register', 'Input Discrete' or 'Input Register'
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   119
        dataname = modbus_function_dict[function][7]
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   120
        # start off with a boolean entry
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   121
        # This is a flag used to allow the user program to control when to 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   122
        # execute the Modbus request.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   123
        # NOTE: If the Modbus request has a 'current_location' of
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   124
        #          %QX1.2.3
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   125
        #       then the execution control flag will be
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   126
        #          %QX1.2.3.0.0
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   127
        #       and all the Modbus registers/coils will be
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   128
        #          %QX1.2.3.0
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   129
        #          %QX1.2.3.1
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   130
        #          %QX1.2.3.2
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   131
        #            ..
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   132
        #          %QX1.2.3.n
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   133
        entries = []
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   134
        entries.append({
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   135
            "name": "Exec. request flag",
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   136
            "type": LOCATION_VAR_MEMORY,
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   137
            "size": 1,           # BOOL flag
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   138
            "IEC_type": "BOOL",  # BOOL flag
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   139
            "var_name": "var_name",
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   140
            "location": "X" + ".".join([str(i) for i in current_location]) + ".0.0",
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   141
            "description": "MB request execution control flag",
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   142
            "children": []})        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   143
        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
   144
            entries.append({
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   145
                "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
   146
                "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
   147
                "size": datasize,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   148
                "IEC_type": datatype,
2589
d94d4f396bbf Nicer name than 'var_name' for variable auto-renaming in Modbus
Edouard Tisserant
parents: 2459
diff changeset
   149
                "var_name": "MB_" + "".join([w[0] for w in dataname.split()]) + "_" + str(offset),
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   150
                "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
   151
                "description": "description",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   152
                "children": []})
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   153
        return {"name": name,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   154
                "type": LOCATION_CONFNODE,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   155
                "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
   156
                "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
   157
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   158
    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
   159
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   160
        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
   161
        @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
   162
        @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
   163
            [{"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
   164
            "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
   165
            "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
   166
            "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
   167
            "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
   168
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   169
        @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
   170
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   171
        return [], "", False
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   172
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   173
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   174
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   175
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   176
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   177
# 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
   178
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   179
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   180
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   181
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   182
# dictionary implementing:
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   183
# 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
   184
# 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
   185
modbus_memtype_dict = {
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   186
    "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
   187
    "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
   188
    "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
   189
    "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
   190
}
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   191
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   192
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   193
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
   194
    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
   195
    <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
   196
      <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
   197
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   198
          <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
   199
          <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
   200
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   201
                <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
   202
                    <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
   203
                    <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
   204
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   205
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   206
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   207
          <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
   208
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   209
                <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
   210
                    <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
   211
                    <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
   212
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   213
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   214
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   215
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   216
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   217
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   218
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   219
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   220
    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
   221
        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
   222
        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
   223
            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
   224
                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
   225
                    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
   226
                        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
   227
                        list.sort()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   228
                        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
   229
        return infos
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   230
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   231
    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
   232
        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
   233
        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
   234
        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
   235
        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
   236
        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
   237
        # '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
   238
        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
   239
        # 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
   240
        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
   241
        # '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
   242
        # 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
   243
        # '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
   244
        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
   245
        # '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
   246
        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
   247
        entries = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   248
        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
   249
            entries.append({
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   250
                "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
   251
                "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
   252
                "size": datasize,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   253
                "IEC_type": datatype,
2589
d94d4f396bbf Nicer name than 'var_name' for variable auto-renaming in Modbus
Edouard Tisserant
parents: 2459
diff changeset
   254
                "var_name": "MB_" + "".join([w[0] for w in dataname.split()]) + "_" + str(offset),
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   255
                "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
   256
                "description": "description",
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   257
                "children": []})
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   258
        return {"name": name,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   259
                "type": LOCATION_CONFNODE,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   260
                "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
   261
                "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
   262
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   263
    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
   264
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   265
        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
   266
        @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
   267
        @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
   268
            [{"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
   269
            "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
   270
            "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
   271
            "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
   272
            "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
   273
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   274
        @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
   275
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   276
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   277
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   278
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   279
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   280
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   281
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   282
# 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
   283
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   284
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   285
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   286
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   287
# XXX TODO "Configuration_Name" should disapear in favor of CTN Name, which is already unique
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   288
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   289
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
   290
    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
   291
    <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
   292
      <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
   293
        <xsd:complexType>
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   294
          <xsd:attribute name="Configuration_Name" type="xsd:string" use="optional" default=""/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   295
          <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
   296
          <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
   297
          <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
   298
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   299
                <xsd:restriction base="xsd:unsignedLong">
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   300
                    <xsd:minInclusive value="0"/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   301
                    <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
   302
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   303
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   304
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   305
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   306
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   307
    </xsd:schema>
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
    # 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
   310
    # 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
   311
    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
   312
    # 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
   313
    PlugType = "ModbusTCPclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   314
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   315
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   316
    def __init__(self):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   317
        # NOTE:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   318
        # The ModbusTCPclient attribute is added dynamically by ConfigTreeNode._AddParamsMembers()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   319
        # It will be an XML parser object created by
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   320
        # GenerateParserFromXSDstring(self.XSD).CreateRoot()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   321
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   322
        # Set the default value for the "Configuration_Name" parameter
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   323
        # The default value will need to be different for each instance of the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   324
        # _ModbusTCPclientPlug class, so we cannot hardcode the default value in the XSD above
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   325
        # This value will be used by the web interface 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   326
        #   (i.e. the extension to the web server used to configure the Modbus parameters).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   327
        #   (The web server is run/activated/started by Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   328
        #   (The web server code is found in runtime/NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   329
        #   (The Modbus extension to the web server is found in runtime/Modbus_config.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   330
        loc_str = ".".join(map(str, self.GetCurrentLocation()))
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   331
        self.ModbusTCPclient.setConfiguration_Name("Modbus TCP Client " + loc_str)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   332
        
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   333
    # 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
   334
    #   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
   335
    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
   336
        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
   337
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   338
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   339
        """ Return the node's Configuration_Name """
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   340
        return self.ModbusTCPclient.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   341
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   342
    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
   343
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   344
        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
   345
        @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
   346
        @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
   347
            [{"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
   348
            "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
   349
            "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
   350
            "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
   351
            "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
   352
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   353
        @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
   354
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   355
        return [], "", False
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
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   358
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   359
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   360
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   361
# 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
   362
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   363
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   364
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   365
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   366
# XXX TODO "Configuration_Name" should disapear in favor of CTN Name, which is already unique
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   367
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   368
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
   369
    # 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
   370
    # 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
   371
    # (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
   372
    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
   373
    <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
   374
      <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
   375
        <xsd:complexType>
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   376
          <xsd:attribute name="Configuration_Name" type="xsd:string" use="optional" default=""/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   377
          <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
   378
          <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
   379
          <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
   380
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   381
                <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
   382
                    <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
   383
                    <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
   384
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   385
            </xsd:simpleType>
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>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   387
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   388
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   389
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   390
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   391
    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
   392
    # 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
   393
    PlugType = "ModbusTCPserver"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   394
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   395
    def __init__(self):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   396
        # NOTE:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   397
        # The ModbusServerNode attribute is added dynamically by ConfigTreeNode._AddParamsMembers()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   398
        # It will be an XML parser object created by
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   399
        # GenerateParserFromXSDstring(self.XSD).CreateRoot()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   400
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   401
        # Set the default value for the "Configuration_Name" parameter
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   402
        # The default value will need to be different for each instance of the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   403
        # _ModbusTCPclientPlug class, so we cannot hardcode the default value in the XSD above
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   404
        # This value will be used by the web interface 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   405
        #   (i.e. the extension to the web server used to configure the Modbus parameters).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   406
        #   (The web server is run/activated/started by Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   407
        #   (The web server code is found in runtime/NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   408
        #   (The Modbus extension to the web server is found in runtime/Modbus_config.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   409
        loc_str = ".".join(map(str, self.GetCurrentLocation()))
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   410
        self.ModbusServerNode.setConfiguration_Name("Modbus TCP Server " + loc_str)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   411
        
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   412
    # 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
   413
    #   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
   414
    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
   415
        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
   416
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   417
    # Return a list with a single tuple conatining the (location, IP address, port number)
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   418
    #     location   : location of this node in the configuration tree
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   419
    #     port number: IP port used by this Modbus/IP server
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   420
    #     IP address : IP address of the network interface on which the server will be listening
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   421
    #                  ("", "*", or "#ANY#" => listening on all interfaces!)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   422
    def GetIPServerPortNumbers(self):
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   423
        port = self.ModbusServerNode.getLocal_Port_Number()
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   424
        addr = self.ModbusServerNode.getLocal_IP_Address()
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   425
        return [(self.GetCurrentLocation(), addr, port)]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   426
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   427
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   428
        """ Return the node's Configuration_Name """
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   429
        return self.ModbusServerNode.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   430
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   431
    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
   432
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   433
        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
   434
        @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
   435
        @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
   436
            [{"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
   437
            "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
   438
            "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
   439
            "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
   440
            "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
   441
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   442
        @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
   443
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   444
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   445
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   446
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   447
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   448
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   449
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   450
# 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
   451
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   452
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   453
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   454
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   455
# XXX TODO "Configuration_Name" should disapear in favor of CTN Name, which is already unique
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   456
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   457
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
   458
    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
   459
    <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
   460
      <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
   461
        <xsd:complexType>
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   462
          <xsd:attribute name="Configuration_Name" type="xsd:string" use="optional" default=""/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   463
          <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
   464
          <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
   465
          <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
   466
          <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
   467
          <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
   468
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   469
                <xsd:restriction base="xsd:integer">
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   470
                    <xsd:minInclusive value="0"/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   471
                    <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
   472
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   473
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   474
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   475
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   476
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   477
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   478
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   479
    # 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
   480
    # 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
   481
    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
   482
    # 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
   483
    PlugType = "ModbusRTUclient"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   484
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   485
    def __init__(self):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   486
        # NOTE:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   487
        # The ModbusRTUclient attribute is added dynamically by ConfigTreeNode._AddParamsMembers()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   488
        # It will be an XML parser object created by
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   489
        # GenerateParserFromXSDstring(self.XSD).CreateRoot()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   490
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   491
        # Set the default value for the "Configuration_Name" parameter
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   492
        # The default value will need to be different for each instance of the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   493
        # _ModbusTCPclientPlug class, so we cannot hardcode the default value in the XSD above
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   494
        # This value will be used by the web interface 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   495
        #   (i.e. the extension to the web server used to configure the Modbus parameters).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   496
        #   (The web server is run/activated/started by Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   497
        #   (The web server code is found in runtime/NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   498
        #   (The Modbus extension to the web server is found in runtime/Modbus_config.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   499
        loc_str = ".".join(map(str, self.GetCurrentLocation()))
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   500
        self.ModbusRTUclient.setConfiguration_Name("Modbus RTU Client " + loc_str)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   501
        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   502
    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
   503
        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
   504
        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
   505
            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
   506
                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
   507
                    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
   508
                        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
   509
                    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
   510
                        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
   511
                    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
   512
                        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
   513
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   514
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   515
    # 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
   516
    #   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
   517
    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
   518
        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
   519
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   520
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   521
        """ Return the node's Configuration_Name """
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   522
        return self.ModbusRTUclient.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   523
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   524
    def CTNGenerate_C(self, buildpath, locations):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   525
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   526
        Generate C code
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   527
        @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   528
        @param locations: List of complete variables locations \
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   529
            [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   530
            "NAME" : name of the variable (generally "__IW0_1_2" style)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   531
            "DIR" : direction "Q","I" or "M"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   532
            "SIZE" : size "X", "B", "W", "D", "L"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   533
            "LOC" : tuple of interger for IEC location (0,1,2,...)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   534
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   535
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   536
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   537
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   538
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   539
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   540
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   541
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   542
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   543
# R T U    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
   544
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   545
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   546
#
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   547
2677
556935640ec0 Modbus: more comments to remember some necessary changes.
Edouard Tisserant
parents: 2669
diff changeset
   548
# XXX TODO "Configuration_Name" should disapear in favor of CTN Name, which is already unique
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   549
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   550
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
   551
    XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   552
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   553
      <xsd:element name="ModbusRTUslave">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   554
        <xsd:complexType>
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   555
          <xsd:attribute name="Configuration_Name" type="xsd:string" use="optional" default=""/>
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   556
          <xsd:attribute name="Serial_Port" type="xsd:string"  use="optional" default="/dev/ttyS0"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   557
          <xsd:attribute name="Baud_Rate"   type="xsd:string"  use="optional" default="9600"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   558
          <xsd:attribute name="Parity"      type="xsd:string"  use="optional" default="even"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   559
          <xsd:attribute name="Stop_Bits"   type="xsd:string"  use="optional" default="1"/>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   560
          <xsd:attribute name="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
   561
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   562
                <xsd:restriction base="xsd:integer">
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   563
                    <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
   564
                    <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
   565
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   566
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   567
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   568
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   569
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   570
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   571
    """
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   572
    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
   573
    # 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
   574
    PlugType = "ModbusRTUslave"
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   575
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   576
    def __init__(self):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   577
        # NOTE:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   578
        # The ModbusRTUslave attribute is added dynamically by ConfigTreeNode._AddParamsMembers()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   579
        # It will be an XML parser object created by
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   580
        # GenerateParserFromXSDstring(self.XSD).CreateRoot()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   581
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   582
        # Set the default value for the "Configuration_Name" parameter
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   583
        # The default value will need to be different for each instance of the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   584
        # _ModbusTCPclientPlug class, so we cannot hardcode the default value in the XSD above
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   585
        # This value will be used by the web interface 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   586
        #   (i.e. the extension to the web server used to configure the Modbus parameters).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   587
        #   (The web server is run/activated/started by Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   588
        #   (The web server code is found in runtime/NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   589
        #   (The Modbus extension to the web server is found in runtime/Modbus_config.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   590
        loc_str = ".".join(map(str, self.GetCurrentLocation()))
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   591
        self.ModbusRTUslave.setConfiguration_Name("Modbus RTU Slave " + loc_str)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2648
diff changeset
   592
        
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   593
    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
   594
        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
   595
        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
   596
            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
   597
                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
   598
                    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
   599
                        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
   600
                    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
   601
                        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
   602
                    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
   603
                        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
   604
        return infos
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   605
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   606
    # 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
   607
    #   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
   608
    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
   609
        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
   610
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   611
    def GetConfigName(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   612
        """ Return the node's Configuration_Name """
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   613
        return self.ModbusRTUslave.getConfiguration_Name()
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
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
    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
   616
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   617
        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
   618
        @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
   619
        @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
   620
            [{"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
   621
            "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
   622
            "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
   623
            "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
   624
            "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
   625
            }, ...]
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   626
        @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
   627
        """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   628
        return [], "", False
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   629
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   630
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   631
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
   632
    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
   633
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   634
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   635
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   636
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   637
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   638
# 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
   639
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   640
#
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   641
#
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   642
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
   643
    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
   644
    <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
   645
      <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
   646
        <xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   647
          <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
   648
            <xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   649
                <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
   650
                    <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
   651
                    <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
   652
                </xsd:restriction>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   653
            </xsd:simpleType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   654
          </xsd:attribute>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   655
        </xsd:complexType>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   656
      </xsd:element>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   657
    </xsd:schema>
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   658
    """
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   659
    CTNChildrenTypes = [("ModbusTCPclient", _ModbusTCPclientPlug, "Modbus TCP Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   660
                        ("ModbusTCPserver", _ModbusTCPserverPlug, "Modbus TCP Server"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   661
                        ("ModbusRTUclient", _ModbusRTUclientPlug, "Modbus RTU Client"),
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   662
                        ("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
   663
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   664
    # 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
   665
    #   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
   666
    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
   667
        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
   668
            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
   669
        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
   670
        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
   671
            # 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
   672
            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
   673
                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
   674
        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
   675
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   676
    # Return a list with tuples of the (location, port numbers) used by all the Modbus/IP servers
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   677
    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
   678
        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
   679
        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
   680
            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
   681
                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
   682
        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
   683
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   684
    # Return a list with tuples of the (location, configuration_name) used by all the Modbus nodes (tcp/rtu, clients/servers)
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   685
    def GetConfigNames(self):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   686
        Node_Configuration_Names = []
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   687
        for child in self.IECSortedChildren():
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   688
            Node_Configuration_Names.extend([(child.GetCurrentLocation(), child.GetConfigName())])
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   689
        return Node_Configuration_Names
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   690
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   691
    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
   692
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   693
        # print self.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   694
        # print type(self)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   695
        # print "self.CTNType >>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   696
        # print self.CTNType
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   697
        # 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
   698
        # 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
   699
        # print "#############"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   700
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   701
        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
   702
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   703
        # 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
   704
        #   total_node_count: (tcp nodes, rtu nodes, ascii nodes)
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   705
        #
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   706
        # Also get a list with tuples of (location, IP address, port number) used by all the Modbus/IP server nodes
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   707
        #   This list is later used to search for duplicates in port numbers!
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   708
        #   IPServer_port_numbers = [(location, IP address, port number), ...]
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   709
        #       location            : tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   710
        #       IPserver_port_number: a number (i.e. port number used by the Modbus/IP server)
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   711
        #       IP address          : IP address of the network interface on which the server will be listening
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   712
        #                             ("", "*", or "#ANY#" => listening on all interfaces!)
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   713
        #
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   714
        # Also get a list with tuples of (location, Configuration_Name) used by all the Modbus nodes
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   715
        #   This list is later used to search for duplicates in Configuration Names!
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   716
        #   Node_Configuration_Names = [(location, Configuration_Name), ...]
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   717
        #       location          : tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   718
        #       Configuration_Name: the "Configuration_Name" string
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   719
        total_node_count = (0, 0, 0)
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   720
        IPServer_port_numbers    = []
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   721
        Node_Configuration_Names = []
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   722
        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
   723
            if CTNInstance.CTNType == "modbus":
2662
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   724
                # ask each modbus plugin instance how many nodes it needs, and add them all up.
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   725
                total_node_count = tuple(x1 + x2 for x1, x2 in zip(total_node_count, CTNInstance.GetNodeCount()))
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   726
                IPServer_port_numbers.   extend(CTNInstance.GetIPServerPortNumbers())
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   727
                Node_Configuration_Names.extend(CTNInstance.GetConfigNames        ())
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   728
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   729
        # Search for use of duplicate Configuration_Names by Modbus nodes
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   730
        # Configuration Names are used by the web server running on the PLC
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   731
        # (more precisely, run by Beremiz_service.py) to identify and allow 
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   732
        # changing the Modbus parameters after the program has been downloaded 
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   733
        # to the PLC (but before it is started)
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   734
        # With clashes in the configuration names, the Modbus nodes will not be
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   735
        # distinguasheble on the web interface!
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   736
        for i in range(0, len(Node_Configuration_Names) - 1):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   737
            for j in range(i + 1, len(Node_Configuration_Names)):
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   738
                if Node_Configuration_Names[i][1] == Node_Configuration_Names[j][1]:
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   739
                    error_message = _("Error: Modbus plugin nodes %{a1}.x and %{a2}.x use the same Configuration_Name \"{a3}\".\n").format(
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   740
                                        a1=_lt_to_str(Node_Configuration_Names[i][0]),
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   741
                                        a2=_lt_to_str(Node_Configuration_Names[j][0]),
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   742
                                        a3=Node_Configuration_Names[j][1])
654583c4af83 Modbus plugin: check for duplicate "Configuration_Name"
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   743
                    self.FatalError(error_message)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   744
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   745
        # Search for use of duplicate port numbers by Modbus/IP servers
2663
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   746
        # Note: We only consider duplicate port numbers if using the same network interface!
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   747
        i = 0
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   748
        for loc1, addr1, port1 in IPServer_port_numbers[:-1]:
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   749
            i = i + 1
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   750
            for loc2, addr2, port2 in IPServer_port_numbers[i:]:
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   751
                if (port1 == port2) and (
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   752
                          (addr1 == addr2)   # on the same network interface
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   753
                       or (addr1 == "") or (addr1 == "*") or (addr1 == "#ANY#") # or one (or both) of the servers
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   754
                       or (addr2 == "") or (addr2 == "*") or (addr2 == "#ANY#") # use all available network interfaces
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   755
                   ):
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   756
                    error_message = _("Error: Modbus plugin nodes %{a1}.x and %{a2}.x use same port number \"{a3}\" " + 
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   757
                                      "on the same (or overlapping) network interfaces \"{a4}\" and \"{a5}\".\n").format(
54f5b8e5c8d2 Modbus plugin: fix algorithm detecting Modbus TCP servers using the same IP port on the same network interface
Mario de Sousa <msousa@fe.up.pt>
parents: 2662
diff changeset
   758
                                        a1=_lt_to_str(loc1), a2=_lt_to_str(loc2), a3=port1, a4=addr1, a5=addr2)
2648
e4ab768170f9 modbus_plugin: generate nicer error message, instead of a traceback
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   759
                    self.FatalError(error_message)
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   760
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   761
        # 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
   762
        # tree
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   763
        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
   764
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   765
        # 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
   766
        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
   767
        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
   768
        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
   769
        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
   770
        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
   771
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   772
        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
   773
        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
   774
        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
   775
        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
   776
        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
   777
        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
   778
        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
   779
        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
   780
        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
   781
        nodeid = 0
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   782
        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
   783
        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
   784
        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
   785
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   786
        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
   787
        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
   788
        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
   789
        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
   790
        loc_vars = []
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   791
        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
   792
        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
   793
            # print "<<<<<<<<<<<<<"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   794
            # 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
   795
            # print child.__class__
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   796
            # print ">>>>>>>>>>>>>"
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   797
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   798
            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
   799
                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
   800
                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
   801
                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
   802
                    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
   803
                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
   804
                #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   805
                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
   806
                    new_memarea = GetTCPServerMemAreaPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   807
                        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
   808
                    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
   809
                        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
   810
                    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
   811
                    function = subchild.GetParamsAttributes()[
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   812
                        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
   813
                    # '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
   814
                    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
   815
                    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
   816
                        # 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
   817
                        absloute_address = iecvar["LOC"][3]
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   818
                        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
   819
                        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
   820
                        # 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
   821
                        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
   822
                            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
   823
                                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
   824
                                    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
   825
                                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
   826
                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
   827
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   828
            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
   829
                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
   830
                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
   831
                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
   832
                    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
   833
                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
   834
                #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   835
                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
   836
                    new_memarea = GetTCPServerMemAreaPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   837
                        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
   838
                    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
   839
                        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
   840
                    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
   841
                    function = subchild.GetParamsAttributes()[
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   842
                        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
   843
                    # '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
   844
                    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
   845
                    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
   846
                        # 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
   847
                        absloute_address = iecvar["LOC"][3]
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   848
                        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
   849
                        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
   850
                        # 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
   851
                        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
   852
                            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
   853
                                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
   854
                                    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
   855
                                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
   856
                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
   857
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   858
            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
   859
                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
   860
                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
   861
                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
   862
                    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
   863
                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
   864
                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
   865
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   866
                        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
   867
                    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
   868
                        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
   869
                    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
   870
                    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
   871
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   872
                        relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   873
                        # test if the located variable 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   874
                        #    (a) has relative address in request specified range
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   875
                        #  AND is NOT
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   876
                        #    (b) is a control flag added by this modbus plugin
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   877
                        #        to control its execution at runtime.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   878
                        #        Currently, we only add the "Execution Control Flag"
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   879
                        #        to each client request (one flag per request)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   880
                        #        to control when to execute the request (if not executed periodically)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   881
                        #        While all Modbus registers/coils are mapped onto a location
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   882
                        #        with 4 numbers (e.g. %QX0.1.2.55), this control flag is mapped
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   883
                        #        onto a location with 4 numbers (e.g. %QX0.1.2.0.0), where the last
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   884
                        #        two numbers are always '0.0', and the first two identify the request.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   885
                        #        In the following if, we check for this condition by checking
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   886
                        #        if their are at least 4 or more number in the location's address.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   887
                        if (        relative_addr in xrange(int(GetCTVal(subchild, 2)))  # condition (a) explained above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   888
                            and len(iecvar["LOC"]) < 5):                                  # condition (b) explained above
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   889
                            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
   890
                                loc_vars.append(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   891
                                    "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
   892
                                loc_vars_list.append(str(iecvar["NAME"]))
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   893
                        # Now add the located variable in case it is a flag (condition (b) above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   894
                        if  len(iecvar["LOC"]) >= 5:       # condition (b) explained above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   895
                            if str(iecvar["NAME"]) not in loc_vars_list:
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   896
                                loc_vars.append(
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   897
                                    "u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   898
                                loc_vars_list.append(str(iecvar["NAME"]))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   899
                    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
   900
                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
   901
                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
   902
            #
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   903
            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
   904
                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
   905
                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
   906
                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
   907
                    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
   908
                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
   909
                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
   910
                    new_req = GetClientRequestPrinted(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   911
                        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
   912
                    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
   913
                        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
   914
                    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
   915
                    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
   916
                        # absloute address - start address
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   917
                        relative_addr = iecvar["LOC"][3] - int(GetCTVal(subchild, 3))
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   918
                        # test if the located variable 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   919
                        #    (a) has relative address in request specified range
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   920
                        #  AND is NOT
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   921
                        #    (b) is a control flag added by this modbus plugin
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   922
                        #        to control its execution at runtime.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   923
                        #        Currently, we only add the "Execution Control Flag"
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   924
                        #        to each client request (one flag per request)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   925
                        #        to control when to execute the request (if not executed periodically)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   926
                        #        While all Modbus registers/coils are mapped onto a location
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   927
                        #        with 4 numbers (e.g. %QX0.1.2.55), this control flag is mapped
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   928
                        #        onto a location with 4 numbers (e.g. %QX0.1.2.0.0), where the last
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   929
                        #        two numbers are always '0.0', and the first two identify the request.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   930
                        #        In the following if, we check for this condition by checking
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   931
                        #        if their are at least 4 or more number in the location's address.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   932
                        if (        relative_addr in xrange(int(GetCTVal(subchild, 2)))  # condition (a) explained above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   933
                            and len(iecvar["LOC"]) < 5):                                  # condition (b) explained above
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   934
                            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
   935
                                loc_vars.append(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   936
                                    "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
   937
                                loc_vars_list.append(str(iecvar["NAME"]))
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   938
                        # Now add the located variable in case it is a flag (condition (b) above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   939
                        if  len(iecvar["LOC"]) >= 5:       # condition (b) explained above
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   940
                            if str(iecvar["NAME"]) not in loc_vars_list:
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   941
                                loc_vars.append(
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   942
                                    "u16 *" + str(iecvar["NAME"]) + " = &client_requests[%d].flag_exec_req;" % (client_requestid))
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2589
diff changeset
   943
                                loc_vars_list.append(str(iecvar["NAME"]))
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   944
                    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
   945
                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
   946
                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
   947
            nodeid += 1
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   948
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   949
        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
   950
        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
   951
        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
   952
        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
   953
        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
   954
        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
   955
        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
   956
        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
   957
        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
   958
        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
   959
        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
   960
        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
   961
        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
   962
        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
   963
        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
   964
        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
   965
        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
   966
            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
   967
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   968
        # 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
   969
        # 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
   970
        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
   971
        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
   972
        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
   973
        f.close()
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   974
        # 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
   975
        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
   976
        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
   977
        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
   978
        f.close()
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   979
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   980
        LDFLAGS = []
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   981
        LDFLAGS.append(" \"-L" + ModbusPath + "\"")
2333
81abf93b4684 Path may contain spaces... sequel of d1359cb
Edouard Tisserant
parents: 2291
diff changeset
   982
        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
   983
        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
   984
        # 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
   985
        # 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
   986
        # 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
   987
        # 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
   988
        # 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
   989
        # 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
   990
        # 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
   991
        # 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
   992
        # 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
   993
        # 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
   994
        # library!
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1913
diff changeset
   995
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
   996
        websettingfile = open(paths.AbsNeighbourFile(__file__, "web_settings.py"), 'r')
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
   997
        websettingcode = websettingfile.read()
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
   998
        websettingfile.close()
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
   999
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1000
        location_str = "_".join(map(str, self.GetCurrentLocation()))
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1001
        websettingcode = websettingcode % locals()
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1002
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1003
        runtimefile_path = os.path.join(buildpath, "runtime_modbus_websettings.py")
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1004
        runtimefile = open(runtimefile_path, 'w')
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1005
        runtimefile.write(websettingcode)
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1006
        runtimefile.close()
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1007
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1008
        return ([(Gen_MB_c_path, ' -I"' + ModbusPath + '"')], LDFLAGS, True,
2993
b76f303ffce6 Python Runtime: order of execution of extension's init() and cleanup() now reflects order of appearance of extensions in configuration tree.
Edouard Tisserant
parents: 2677
diff changeset
  1009
                ("runtime_%s_modbus_websettings.py" % location_str, open(runtimefile_path, "rb")),
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2663
diff changeset
  1010
        )