modbus/mb_utils.py
author Edouard Tisserant
Mon, 06 Aug 2018 14:30:43 +0200
changeset 2245 fe3394697b02
parent 2019 92f02bb17c7e
child 2432 dbc065a2f7a5
permissions -rw-r--r--
merged
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: 1909
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
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    26
# dictionary implementing:
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    27
# 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: 1909
diff changeset
    28
# tuple - (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: 1909
diff changeset
    29
# data_type, bit_size)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    30
modbus_function_dict = {
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    31
    "01 - Read Coils":                ('1',  'req_input', 2000, "BOOL",  1, "Q", "X", "Coil"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    32
    "02 - Read Input Discretes":      ('2',  'req_input', 2000, "BOOL",  1, "I", "X", "Input Discrete"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    33
    "03 - Read Holding Registers":    ('3',  'req_input',  125, "WORD", 16, "Q", "W", "Holding Register"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    34
    "04 - Read Input Registers":      ('4',  'req_input',  125, "WORD", 16, "I", "W", "Input Register"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    35
    "05 - Write Single coil":         ('5', 'req_output',    1, "BOOL",  1, "Q", "X", "Coil"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    36
    "06 - Write Single Register":     ('6', 'req_output',    1, "WORD", 16, "Q", "W", "Holding Register"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    37
    "15 - Write Multiple Coils":     ('15', 'req_output', 1968, "BOOL",  1, "Q", "X", "Coil"),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    38
    "16 - Write Multiple Registers": ('16', 'req_output',  123, "WORD", 16, "Q", "W", "Holding Register")}
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    39
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    40
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    41
# Configuration tree value acces helper
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    42
def GetCTVal(child, index):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    43
    return child.GetParamsAttributes()[0]["children"][index]["value"]
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    44
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    45
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    46
# Configuration tree value acces helper, for multiple values
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    47
def GetCTVals(child, indexes):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    48
    return map(lambda index: GetCTVal(child, index), indexes)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    49
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    51
def 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
    52
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
    Outputs a string to be used on C files
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
    params: child - the correspondent subplugin in Beremiz
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    55
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
    node_init_template = '''/*node %(locnodestr)s*/
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
{"%(locnodestr)s", %(slaveid)s, {naf_tcp, {.tcp = {%(host)s, "%(port)s", DEF_CLOSE_ON_SILENCE}}}, -1 /* mb_nd */, 0 /* init_state */}'''
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    58
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    59
    location = ".".join(map(str, child.GetCurrentLocation()))
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    60
    host, port, slaveid = GetCTVals(child, range(3))
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    61
    if host == "#ANY#":
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    62
        host = 'INADDR_ANY'
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    63
    else:
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    64
        host = '"' + host + '"'
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    65
    # slaveid = GetCTVal(child, 2)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    66
    # if int(slaveid) not in xrange(256):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    67
        # self.GetCTRoot().logger.write_error("Error: Wrong slave ID in %s server node\nModbus Plugin C code returns empty\n"%location)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    68
        # return None
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    69
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    70
    node_dict = {"locnodestr": location,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    71
                 "host": host,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    72
                 "port": port,
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
    73
                 "slaveid": slaveid}
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    74
    return node_init_template % node_dict
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    75
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    76
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
def GetTCPServerMemAreaPrinted(self, child, nodeid):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    78
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    79
    Outputs a string to be used on C files
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    80
    params: child - the correspondent subplugin in Beremiz
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    81
            nodeid - on C code, each request has it's own parent node (sequential, 0..NUMBER_OF_NODES)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    82
                     It's this parameter.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    83
    return: None - if any definition error found
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    84
            The string that should be added on C code - if everything goes allright
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    85
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
    request_dict = {}
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    87
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    88
    request_dict["locreqstr"] = "_".join(map(str, child.GetCurrentLocation()))
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
    request_dict["nodeid"] = str(nodeid)
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    90
    request_dict["address"] = GetCTVal(child, 2)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    91
    if int(request_dict["address"]) not in xrange(65536):
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    92
        self.GetCTRoot().logger.write_error(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    93
            "Modbus plugin: Invalid Start Address in server memory area node %(locreqstr)s (Must be in the range [0..65535])\nModbus plugin: Aborting C code generation for this node\n" % request_dict)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    94
        return None
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    95
    request_dict["count"] = GetCTVal(child, 1)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    96
    if int(request_dict["count"]) not in xrange(1, 65536):
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    97
        self.GetCTRoot().logger.write_error(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    98
            "Modbus plugin: Invalid number of channels in server memory area node %(locreqstr)s (Must be in the range [1..65536-start_address])\nModbus plugin: Aborting C code generation for this node\n" % request_dict)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
    99
        return None
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   100
    if (int(request_dict["address"]) + int(request_dict["count"])) not in xrange(1, 65537):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   101
        self.GetCTRoot().logger.write_error(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   102
            "Modbus plugin: Invalid number of channels in server memory area node %(locreqstr)s (Must be in the range [1..65536-start_address])\nModbus plugin: Aborting C code generation for this node\n" % request_dict)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   103
        return None
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   104
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   105
    return ""
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   106
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   107
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   108
modbus_serial_baudrate_list = [
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   109
    "110", "300", "600", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200"]
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   110
modbus_serial_stopbits_list = ["1", "2"]
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   111
modbus_serial_parity_dict = {"none": 0, "odd": 1, "even": 2}
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   112
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   113
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   114
def 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
   115
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   116
    Outputs a string to be used on C files
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   117
    params: child - the correspondent subplugin in Beremiz
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   118
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   119
    node_init_template = '''/*node %(locnodestr)s*/
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   120
{"%(locnodestr)s", %(slaveid)s, {naf_rtu, {.rtu = {"%(device)s", %(baud)s /*baud*/, %(parity)s /*parity*/, 8 /*data bits*/, %(stopbits)s, 0 /* ignore echo */}}}, -1 /* mb_nd */, 0 /* init_state */}'''
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   121
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   122
    location = ".".join(map(str, child.GetCurrentLocation()))
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   123
    device, baud, parity, stopbits, slaveid = GetCTVals(child, range(5))
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   124
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   125
    node_dict = {"locnodestr": location,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   126
                 "device": device,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   127
                 "baud": baud,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   128
                 "parity": modbus_serial_parity_dict[parity],
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   129
                 "stopbits": stopbits,
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   130
                 "slaveid": slaveid}
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   131
    return node_init_template % node_dict
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   132
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   133
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   134
def 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
   135
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   136
    Outputs a string to be used on C files
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   137
    params: child - the correspondent subplugin in Beremiz
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   138
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   139
    node_init_template = '''/*node %(locnodestr)s*/
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   140
{"%(locnodestr)s", {naf_rtu, {.rtu = {"%(device)s", %(baud)s /*baud*/, %(parity)s /*parity*/, 8 /*data bits*/, %(stopbits)s, 0 /* ignore echo */}}}, -1 /* mb_nd */, 0 /* init_state */, %(coms_period)s /* communication period */}'''
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   141
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   142
    location = ".".join(map(str, child.GetCurrentLocation()))
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   143
    device, baud, parity, stopbits, coms_period = GetCTVals(child, range(5))
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   144
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   145
    node_dict = {"locnodestr": location,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   146
                 "device": device,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   147
                 "baud": baud,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   148
                 "parity": modbus_serial_parity_dict[parity],
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   149
                 "stopbits": stopbits,
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   150
                 "coms_period": coms_period}
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   151
    return node_init_template % node_dict
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   152
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   153
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   154
def 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
   155
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   156
    Outputs a string to be used on C files
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   157
    params: child - the correspondent subplugin in Beremiz
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   158
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   159
    node_init_template = '''/*node %(locnodestr)s*/
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   160
{"%(locnodestr)s", {naf_tcp, {.tcp = {"%(host)s", "%(port)s", DEF_CLOSE_ON_SILENCE}}}, -1 /* mb_nd */, 0 /* init_state */, %(coms_period)s /* communication period */, 0 /* prev_error */}'''
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   161
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   162
    location = ".".join(map(str, child.GetCurrentLocation()))
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   163
    host, port, coms_period = GetCTVals(child, range(3))
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   164
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   165
    node_dict = {"locnodestr": location,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   166
                 "host": host,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   167
                 "port": port,
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1918
diff changeset
   168
                 "coms_period": coms_period}
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   169
    return node_init_template % node_dict
1909
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
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   172
def GetClientRequestPrinted(self, child, nodeid):
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   173
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   174
    Outputs a string to be used on C files
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   175
    params: child - the correspondent subplugin in Beremiz
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   176
            nodeid - on C code, each request has it's own parent node (sequential, 0..NUMBER_OF_NODES)
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   177
                     It's this parameter.
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   178
    return: None - if any definition error found
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   179
            The string that should be added on C code - if everything goes allright
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   180
    """
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   181
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   182
    req_init_template = '''/*request %(locreqstr)s*/
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   183
{"%(locreqstr)s", %(nodeid)s, %(slaveid)s, %(iotype)s, %(func_nr)s, %(address)s , %(count)s,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   184
DEF_REQ_SEND_RETRIES, 0 /* error_code */, 0 /* prev_code */, {%(timeout_s)d, %(timeout_ns)d} /* timeout */,
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   185
{%(buffer)s}, {%(buffer)s}}'''
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   186
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   187
    timeout = int(GetCTVal(child, 4))
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   188
    timeout_s = int(timeout / 1000)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   189
    timeout_ms = timeout - (timeout_s * 1000)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   190
    timeout_ns = timeout_ms * 1000000
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   191
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   192
    request_dict = {
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   193
        "locreqstr": "_".join(map(str, child.GetCurrentLocation())),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   194
        "nodeid": str(nodeid),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   195
        "slaveid": GetCTVal(child, 1),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   196
        "address": GetCTVal(child, 3),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   197
        "count": GetCTVal(child, 2),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   198
        "timeout": timeout,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   199
        "timeout_s": timeout_s,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   200
        "timeout_ns": timeout_ns,
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   201
        "buffer": ",".join(['0'] * int(GetCTVal(child, 2))),
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   202
        "func_nr": modbus_function_dict[GetCTVal(child, 0)][0],
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   203
        "iotype": modbus_function_dict[GetCTVal(child, 0)][1],
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   204
        "maxcount": modbus_function_dict[GetCTVal(child, 0)][2]}
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   205
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   206
    if int(request_dict["slaveid"]) not in xrange(256):
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   207
        self.GetCTRoot().logger.write_error(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   208
            "Modbus plugin: Invalid slaveID in TCP client request node %(locreqstr)s (Must be in the range [0..255])\nModbus plugin: Aborting C code generation for this node\n" % request_dict)
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   209
        return None
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   210
    if int(request_dict["address"]) not in xrange(65536):
1918
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   211
        self.GetCTRoot().logger.write_error(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   212
            "Modbus plugin: Invalid Start Address in TCP client request node %(locreqstr)s (Must be in the range [0..65535])\nModbus plugin: Aborting C code generation for this node\n" % request_dict)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   213
        return None
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   214
    if int(request_dict["count"]) not in xrange(1, 1 + int(request_dict["maxcount"])):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   215
        self.GetCTRoot().logger.write_error(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   216
            "Modbus plugin: Invalid number of channels in TCP client request node %(locreqstr)s (Must be in the range [1..%(maxcount)s])\nModbus plugin: Aborting C code generation for this node\n" % request_dict)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   217
        return None
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   218
    if (int(request_dict["address"]) + int(request_dict["count"])) not in xrange(1, 65537):
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   219
        self.GetCTRoot().logger.write_error(
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   220
            "Modbus plugin: Invalid number of channels in TCP client request node %(locreqstr)s (start_address + nr_channels must be less than 65536)\nModbus plugin: Aborting C code generation for this node\n" % request_dict)
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   221
        return None
e7b6478b4ebc PEP8 conformity for modbus. Most of it done by autopep8, plus some easy refactoring.
Edouard Tisserant
parents: 1909
diff changeset
   222
1909
bb883e063175 Add support for Modbus (TCP and RTU) working as master & slave
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   223
    return req_init_template % request_dict