modbus/mb_utils.py
branchpython3
changeset 3750 f62625418bff
parent 3733 d1acf20e8e7c
child 3752 9f6f46dbe3ae
equal deleted inserted replaced
3749:fda6c1a37662 3750:f62625418bff
    20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    21 #
    21 #
    22 # This code is made available on the understanding that it will not be
    22 # This code is made available on the understanding that it will not be
    23 # used in safety-critical situations without a full and competent review.
    23 # used in safety-critical situations without a full and competent review.
    24 
    24 
    25 from __future__ import absolute_import
    25 
    26 from __future__ import division
    26 
    27 from six.moves import xrange
    27 from six.moves import xrange
    28 
    28 
    29 # dictionary implementing:
    29 # dictionary implementing:
    30 # key   - string with the description we want in the request plugin GUI
    30 # key   - string with the description we want in the request plugin GUI
    31 # tuple - (modbus function number, request type, max count value,
    31 # tuple - (modbus function number, request type, max count value,
    46     return child.GetParamsAttributes()[0]["children"][index]["value"]
    46     return child.GetParamsAttributes()[0]["children"][index]["value"]
    47 
    47 
    48 
    48 
    49 # Configuration tree value acces helper, for multiple values
    49 # Configuration tree value acces helper, for multiple values
    50 def GetCTVals(child, indexes):
    50 def GetCTVals(child, indexes):
    51     return map(lambda index: GetCTVal(child, index), indexes)
    51     return [GetCTVal(child, index) for index in indexes]
    52 
    52 
    53 
    53 
    54 def GetTCPServerNodePrinted(self, child):
    54 def GetTCPServerNodePrinted(self, child):
    55     """
    55     """
    56     Outputs a string to be used on C files
    56     Outputs a string to be used on C files
    58     """
    58     """
    59     node_init_template = '''/*node %(locnodestr)s*/
    59     node_init_template = '''/*node %(locnodestr)s*/
    60 {"%(locnodestr)s", "%(config_name)s", "%(host)s", "%(port)s", %(slaveid)s, {naf_tcp, {.tcp = {NULL, NULL, DEF_CLOSE_ON_SILENCE}}}, -1 /* mb_nd */, 0 /* init_state */}'''
    60 {"%(locnodestr)s", "%(config_name)s", "%(host)s", "%(port)s", %(slaveid)s, {naf_tcp, {.tcp = {NULL, NULL, DEF_CLOSE_ON_SILENCE}}}, -1 /* mb_nd */, 0 /* init_state */}'''
    61 
    61 
    62     location = ".".join(map(str, child.GetCurrentLocation()))
    62     location = ".".join(map(str, child.GetCurrentLocation()))
    63     config_name, host, port, slaveid = GetCTVals(child, range(4))
    63     config_name, host, port, slaveid = GetCTVals(child, list(range(4)))
    64     if host == "#ANY#":
    64     if host == "#ANY#":
    65         host = ''
    65         host = ''
    66     # slaveid = GetCTVal(child, 2)
    66     # slaveid = GetCTVal(child, 2)
    67     # if int(slaveid) not in xrange(256):
    67     # if int(slaveid) not in xrange(256):
    68         # self.GetCTRoot().logger.write_error("Error: Wrong slave ID in %s server node\nModbus Plugin C code returns empty\n"%location)
    68         # self.GetCTRoot().logger.write_error("Error: Wrong slave ID in %s server node\nModbus Plugin C code returns empty\n"%location)
    88     request_dict = {}
    88     request_dict = {}
    89 
    89 
    90     request_dict["locreqstr"] = "_".join(map(str, child.GetCurrentLocation()))
    90     request_dict["locreqstr"] = "_".join(map(str, child.GetCurrentLocation()))
    91     request_dict["nodeid"] = str(nodeid)
    91     request_dict["nodeid"] = str(nodeid)
    92     request_dict["address"] = GetCTVal(child, 2)
    92     request_dict["address"] = GetCTVal(child, 2)
    93     if int(request_dict["address"]) not in xrange(65536):
    93     if int(request_dict["address"]) not in range(65536):
    94         self.GetCTRoot().logger.write_error(
    94         self.GetCTRoot().logger.write_error(
    95             "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)
    95             "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)
    96         return None
    96         return None
    97     request_dict["count"] = GetCTVal(child, 1)
    97     request_dict["count"] = GetCTVal(child, 1)
    98     if int(request_dict["count"]) not in xrange(1, 65537):
    98     if int(request_dict["count"]) not in range(1, 65537):
    99         self.GetCTRoot().logger.write_error(
    99         self.GetCTRoot().logger.write_error(
   100             "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)
   100             "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)
   101         return None
   101         return None
   102     if (int(request_dict["address"]) + int(request_dict["count"])) not in xrange(1, 65537):
   102     if (int(request_dict["address"]) + int(request_dict["count"])) not in range(1, 65537):
   103         self.GetCTRoot().logger.write_error(
   103         self.GetCTRoot().logger.write_error(
   104             "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)
   104             "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)
   105         return None
   105         return None
   106 
   106 
   107     return ""
   107     return ""
   120     """
   120     """
   121     node_init_template = '''/*node %(locnodestr)s*/
   121     node_init_template = '''/*node %(locnodestr)s*/
   122 {"%(locnodestr)s", "%(config_name)s", "%(device)s", "",%(slaveid)s, {naf_rtu, {.rtu = {NULL, %(baud)s /*baud*/, %(parity)s /*parity*/, 8 /*data bits*/, %(stopbits)s, 0 /* ignore echo */}}}, -1 /* mb_nd */, 0 /* init_state */}'''
   122 {"%(locnodestr)s", "%(config_name)s", "%(device)s", "",%(slaveid)s, {naf_rtu, {.rtu = {NULL, %(baud)s /*baud*/, %(parity)s /*parity*/, 8 /*data bits*/, %(stopbits)s, 0 /* ignore echo */}}}, -1 /* mb_nd */, 0 /* init_state */}'''
   123 
   123 
   124     location = ".".join(map(str, child.GetCurrentLocation()))
   124     location = ".".join(map(str, child.GetCurrentLocation()))
   125     config_name, device, baud, parity, stopbits, slaveid = GetCTVals(child, range(6))
   125     config_name, device, baud, parity, stopbits, slaveid = GetCTVals(child, list(range(6)))
   126 
   126 
   127     node_dict = {"locnodestr": location,
   127     node_dict = {"locnodestr": location,
   128                  "config_name": config_name,
   128                  "config_name": config_name,
   129                  "device": device,
   129                  "device": device,
   130                  "baud": baud,
   130                  "baud": baud,
   141     """
   141     """
   142     node_init_template = '''/*node %(locnodestr)s*/
   142     node_init_template = '''/*node %(locnodestr)s*/
   143 {"%(locnodestr)s", "%(config_name)s", "%(device)s", "", {naf_rtu, {.rtu = {NULL, %(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  (ms)*/, %(coms_delay)s /* inter request delay (ms)*/, 0 /* prev_error */}'''
   143 {"%(locnodestr)s", "%(config_name)s", "%(device)s", "", {naf_rtu, {.rtu = {NULL, %(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  (ms)*/, %(coms_delay)s /* inter request delay (ms)*/, 0 /* prev_error */}'''
   144 
   144 
   145     location = ".".join(map(str, child.GetCurrentLocation()))
   145     location = ".".join(map(str, child.GetCurrentLocation()))
   146     config_name, device, baud, parity, stopbits, coms_period, coms_delay = GetCTVals(child, range(7))
   146     config_name, device, baud, parity, stopbits, coms_period, coms_delay = GetCTVals(child, list(range(7)))
   147 
   147 
   148     node_dict = {"locnodestr": location,
   148     node_dict = {"locnodestr": location,
   149                  "config_name": config_name,
   149                  "config_name": config_name,
   150                  "device": device,
   150                  "device": device,
   151                  "baud": baud,
   151                  "baud": baud,
   163     """
   163     """
   164     node_init_template = '''/*node %(locnodestr)s*/
   164     node_init_template = '''/*node %(locnodestr)s*/
   165 {"%(locnodestr)s", "%(config_name)s", "%(host)s", "%(port)s", {naf_tcp, {.tcp = {NULL, NULL, DEF_CLOSE_ON_SILENCE}}}, -1 /* mb_nd */, 0 /* init_state */, %(coms_period)s /* communication period (ms)*/, %(coms_delay)s /* inter request delay (ms)*/, 0 /* prev_error */}'''
   165 {"%(locnodestr)s", "%(config_name)s", "%(host)s", "%(port)s", {naf_tcp, {.tcp = {NULL, NULL, DEF_CLOSE_ON_SILENCE}}}, -1 /* mb_nd */, 0 /* init_state */, %(coms_period)s /* communication period (ms)*/, %(coms_delay)s /* inter request delay (ms)*/, 0 /* prev_error */}'''
   166 
   166 
   167     location = ".".join(map(str, child.GetCurrentLocation()))
   167     location = ".".join(map(str, child.GetCurrentLocation()))
   168     config_name, host, port, coms_period, coms_delay = GetCTVals(child, range(5))
   168     config_name, host, port, coms_period, coms_delay = GetCTVals(child, list(range(5)))
   169 
   169 
   170     node_dict = {"locnodestr": location,
   170     node_dict = {"locnodestr": location,
   171                  "config_name": config_name,
   171                  "config_name": config_name,
   172                  "host": host,
   172                  "host": host,
   173                  "port": port,
   173                  "port": port,
   209         "buffer": ",".join(['0'] * int(GetCTVal(child, 2))),
   209         "buffer": ",".join(['0'] * int(GetCTVal(child, 2))),
   210         "func_nr": modbus_function_dict[GetCTVal(child, 0)][0],
   210         "func_nr": modbus_function_dict[GetCTVal(child, 0)][0],
   211         "iotype": modbus_function_dict[GetCTVal(child, 0)][1],
   211         "iotype": modbus_function_dict[GetCTVal(child, 0)][1],
   212         "maxcount": modbus_function_dict[GetCTVal(child, 0)][2]}
   212         "maxcount": modbus_function_dict[GetCTVal(child, 0)][2]}
   213 
   213 
   214     if int(request_dict["slaveid"]) not in xrange(256):
   214     if int(request_dict["slaveid"]) not in range(256):
   215         self.GetCTRoot().logger.write_error(
   215         self.GetCTRoot().logger.write_error(
   216             "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)
   216             "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)
   217         return None
   217         return None
   218     if int(request_dict["address"]) not in xrange(65536):
   218     if int(request_dict["address"]) not in range(65536):
   219         self.GetCTRoot().logger.write_error(
   219         self.GetCTRoot().logger.write_error(
   220             "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)
   220             "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)
   221         return None
   221         return None
   222     if int(request_dict["count"]) not in xrange(1, 1 + int(request_dict["maxcount"])):
   222     if int(request_dict["count"]) not in range(1, 1 + int(request_dict["maxcount"])):
   223         self.GetCTRoot().logger.write_error(
   223         self.GetCTRoot().logger.write_error(
   224             "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)
   224             "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)
   225         return None
   225         return None
   226     if (int(request_dict["address"]) + int(request_dict["count"])) not in xrange(1, 65537):
   226     if (int(request_dict["address"]) + int(request_dict["count"])) not in range(1, 65537):
   227         self.GetCTRoot().logger.write_error(
   227         self.GetCTRoot().logger.write_error(
   228             "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)
   228             "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)
   229         return None
   229         return None
   230     if (request_dict["write_on_change"] and (request_dict["iotype"] == 'req_input')):
   230     if (request_dict["write_on_change"] and (request_dict["iotype"] == 'req_input')):
   231         self.GetCTRoot().logger.write_error(
   231         self.GetCTRoot().logger.write_error(