msousa@1909: #!/usr/bin/env python msousa@1909: # -*- coding: utf-8 -*- msousa@1909: msousa@1909: # This file is part of Beremiz, a Integrated Development Environment for msousa@1909: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. msousa@1909: # msousa@1909: # Copyright (c) 2016 Mario de Sousa (msousa@fe.up.pt) msousa@1909: # msousa@1909: # This program is free software: you can redistribute it and/or modify msousa@1909: # it under the terms of the GNU General Public License as published by msousa@1909: # the Free Software Foundation, either version 3 of the License, or msousa@1909: # (at your option) any later version. msousa@1909: # msousa@1909: # This program is distributed in the hope that it will be useful, msousa@1909: # but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@1909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@1909: # GNU General Public License for more details. msousa@1909: # msousa@1909: # You should have received a copy of the GNU General Public License msousa@1909: # along with this program. If not, see . msousa@1909: # msousa@1909: # This code is made available on the understanding that it will not be msousa@1909: # used in safety-critical situations without a full and competent review. msousa@1909: msousa@1909: msousa@1909: msousa@1909: msousa@1909: #dictionary implementing: msousa@1909: #key - string with the description we want in the request plugin GUI msousa@1909: #tuple - (modbus function number, request type, max count value, data_type, bit_size) msousa@1909: modbus_function_dict = {"01 - Read Coils" : ( '1', 'req_input', 2000, "BOOL", 1 , "Q", "X", "Coil"), msousa@1909: "02 - Read Input Discretes" : ( '2', 'req_input', 2000, "BOOL", 1 , "I", "X", "Input Discrete"), msousa@1909: "03 - Read Holding Registers" : ( '3', 'req_input', 125, "WORD", 16, "Q", "W", "Holding Register"), msousa@1909: "04 - Read Input Registers" : ( '4', 'req_input', 125, "WORD", 16, "I", "W", "Input Register"), msousa@1909: "05 - Write Single coil" : ( '5','req_output', 1, "BOOL", 1 , "Q", "X", "Coil"), msousa@1909: "06 - Write Single Register" : ( '6','req_output', 1, "WORD", 16, "Q", "W", "Holding Register"), msousa@1909: "15 - Write Multiple Coils" : ('15','req_output', 1968, "BOOL", 1 , "Q", "X", "Coil"), msousa@1909: "16 - Write Multiple Registers" : ('16','req_output', 123, "WORD", 16, "Q", "W", "Holding Register"), msousa@1909: } msousa@1909: msousa@1909: msousa@1909: msousa@1909: def GetTCPServerNodePrinted(self, child): msousa@1909: """ msousa@1909: Outputs a string to be used on C files msousa@1909: params: child - the correspondent subplugin in Beremiz msousa@1909: """ msousa@1909: node_init_template = '''/*node %(locnodestr)s*/ msousa@1909: {"%(locnodestr)s", %(slaveid)s, {naf_tcp, {.tcp = {%(host)s, "%(port)s", DEF_CLOSE_ON_SILENCE}}}, -1 /* mb_nd */, 0 /* init_state */}''' msousa@1909: msousa@1909: location = ".".join(map(str, child.GetCurrentLocation())) msousa@1909: host = child.GetParamsAttributes()[0]["children"][0]["value"] msousa@1909: port = child.GetParamsAttributes()[0]["children"][1]["value"] msousa@1909: slaveid = child.GetParamsAttributes()[0]["children"][2]["value"] msousa@1909: if host=="#ANY#": msousa@1909: host='INADDR_ANY' msousa@1909: else: msousa@1909: host='"'+host+'"' msousa@1909: #slaveid = child.GetParamsAttributes()[0]["children"][2]["value"] msousa@1909: #if int(slaveid) not in xrange(256): msousa@1909: #self.GetCTRoot().logger.write_error("Error: Wrong slave ID in %s server node\nModbus Plugin C code returns empty\n"%location) msousa@1909: #return None msousa@1909: msousa@1909: node_dict = {"locnodestr" : location, msousa@1909: "host" : host, msousa@1909: "port" : port, msousa@1909: "slaveid" : slaveid, msousa@1909: } msousa@1909: return node_init_template % node_dict msousa@1909: msousa@1909: msousa@1909: msousa@1909: msousa@1909: def GetTCPServerMemAreaPrinted(self, child, nodeid): msousa@1909: """ msousa@1909: Outputs a string to be used on C files msousa@1909: params: child - the correspondent subplugin in Beremiz msousa@1909: nodeid - on C code, each request has it's own parent node (sequential, 0..NUMBER_OF_NODES) msousa@1909: It's this parameter. msousa@1909: return: None - if any definition error found msousa@1909: The string that should be added on C code - if everything goes allright msousa@1909: """ msousa@1909: request_dict = {} msousa@1909: msousa@1909: request_dict["locreqstr"] = "_".join(map(str, child.GetCurrentLocation())) msousa@1909: request_dict["nodeid"] = str(nodeid) msousa@1909: request_dict["address"] = child.GetParamsAttributes()[0]["children"][2]["value"] msousa@1909: if int(request_dict["address"]) not in xrange(65536): msousa@1909: self.GetCTRoot().logger.write_error("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) msousa@1909: return None msousa@1909: request_dict["count"] = child.GetParamsAttributes()[0]["children"][1]["value"] msousa@1909: if int(request_dict["count"]) not in xrange(1, 65536): msousa@1909: self.GetCTRoot().logger.write_error("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) msousa@1909: return None msousa@1909: if (int(request_dict["address"]) + int(request_dict["count"])) not in xrange(1,65537): msousa@1909: self.GetCTRoot().logger.write_error("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) msousa@1909: return None msousa@1909: msousa@1909: return "" msousa@1909: msousa@1909: msousa@1909: msousa@1909: msousa@1909: modbus_serial_baudrate_list = ["110", "300", "600", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200"] msousa@1909: modbus_serial_stopbits_list = ["1", "2"] msousa@1909: modbus_serial_parity_dict = {"none": 0, "odd": 1, "even": 2} msousa@1909: msousa@1909: msousa@1909: msousa@1909: def GetRTUSlaveNodePrinted(self, child): msousa@1909: """ msousa@1909: Outputs a string to be used on C files msousa@1909: params: child - the correspondent subplugin in Beremiz msousa@1909: """ msousa@1909: node_init_template = '''/*node %(locnodestr)s*/ msousa@1909: {"%(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 */}''' msousa@1909: msousa@1909: location = ".".join(map(str, child.GetCurrentLocation())) msousa@1909: device = child.GetParamsAttributes()[0]["children"][0]["value"] msousa@1909: baud = child.GetParamsAttributes()[0]["children"][1]["value"] msousa@1909: parity = child.GetParamsAttributes()[0]["children"][2]["value"] msousa@1909: stopbits = child.GetParamsAttributes()[0]["children"][3]["value"] msousa@1909: slaveid = child.GetParamsAttributes()[0]["children"][4]["value"] msousa@1909: msousa@1909: node_dict = {"locnodestr" : location, msousa@1909: "device" : device, msousa@1909: "baud" : baud, msousa@1909: "parity" : modbus_serial_parity_dict[parity], msousa@1909: "stopbits" : stopbits, msousa@1909: "slaveid" : slaveid msousa@1909: } msousa@1909: return node_init_template % node_dict msousa@1909: msousa@1909: msousa@1909: msousa@1909: msousa@1909: msousa@1909: def GetRTUClientNodePrinted(self, child): msousa@1909: """ msousa@1909: Outputs a string to be used on C files msousa@1909: params: child - the correspondent subplugin in Beremiz msousa@1909: """ msousa@1909: node_init_template = '''/*node %(locnodestr)s*/ msousa@1909: {"%(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 */}''' msousa@1909: msousa@1909: location = ".".join(map(str, child.GetCurrentLocation())) msousa@1909: device = child.GetParamsAttributes()[0]["children"][0]["value"] msousa@1909: baud = child.GetParamsAttributes()[0]["children"][1]["value"] msousa@1909: parity = child.GetParamsAttributes()[0]["children"][2]["value"] msousa@1909: stopbits = child.GetParamsAttributes()[0]["children"][3]["value"] msousa@1909: coms_period = child.GetParamsAttributes()[0]["children"][4]["value"] msousa@1909: msousa@1909: node_dict = {"locnodestr" : location, msousa@1909: "device" : device, msousa@1909: "baud" : baud, msousa@1909: "parity" : modbus_serial_parity_dict[parity], msousa@1909: "stopbits" : stopbits, msousa@1909: "coms_period" : coms_period msousa@1909: } msousa@1909: return node_init_template % node_dict msousa@1909: msousa@1909: msousa@1909: msousa@1909: msousa@1909: def GetTCPClientNodePrinted(self, child): msousa@1909: """ msousa@1909: Outputs a string to be used on C files msousa@1909: params: child - the correspondent subplugin in Beremiz msousa@1909: """ msousa@1909: node_init_template = '''/*node %(locnodestr)s*/ msousa@1909: {"%(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 */}''' msousa@1909: msousa@1909: location = ".".join(map(str, child.GetCurrentLocation())) msousa@1909: host = child.GetParamsAttributes()[0]["children"][0]["value"] msousa@1909: port = child.GetParamsAttributes()[0]["children"][1]["value"] msousa@1909: coms_period = child.GetParamsAttributes()[0]["children"][2]["value"] msousa@1909: msousa@1909: node_dict = {"locnodestr" : location, msousa@1909: "host" : host, msousa@1909: "port" : port, msousa@1909: "coms_period" : coms_period msousa@1909: } msousa@1909: return node_init_template % node_dict msousa@1909: msousa@1909: msousa@1909: msousa@1909: msousa@1909: def GetClientRequestPrinted(self, child, nodeid): msousa@1909: """ msousa@1909: Outputs a string to be used on C files msousa@1909: params: child - the correspondent subplugin in Beremiz msousa@1909: nodeid - on C code, each request has it's own parent node (sequential, 0..NUMBER_OF_NODES) msousa@1909: It's this parameter. msousa@1909: return: None - if any definition error found msousa@1909: The string that should be added on C code - if everything goes allright msousa@1909: """ msousa@1909: msousa@1909: req_init_template = '''/*request %(locreqstr)s*/ msousa@1909: {"%(locreqstr)s", %(nodeid)s, %(slaveid)s, %(iotype)s, %(func_nr)s, %(address)s , %(count)s, msousa@1909: DEF_REQ_SEND_RETRIES, 0 /* error_code */, 0 /* prev_code */, {%(timeout_s)d, %(timeout_ns)d} /* timeout */, msousa@1909: {%(buffer)s}, {%(buffer)s}}''' msousa@1909: msousa@1909: timeout = int(child.GetParamsAttributes()[0]["children"][4]["value"]) msousa@1909: timeout_s = int(timeout / 1000) msousa@1909: timeout_ms = timeout - (timeout_s * 1000) msousa@1909: timeout_ns = timeout_ms * 1000000 msousa@1909: msousa@1909: request_dict = {} msousa@1909: msousa@1909: request_dict["locreqstr" ] = "_".join(map(str, child.GetCurrentLocation())) msousa@1909: request_dict["nodeid" ] = str(nodeid) msousa@1909: request_dict["slaveid" ] = child.GetParamsAttributes()[0]["children"][1]["value"] msousa@1909: request_dict["address" ] = child.GetParamsAttributes()[0]["children"][3]["value"] msousa@1909: request_dict["count" ] = child.GetParamsAttributes()[0]["children"][2]["value"] msousa@1909: request_dict["timeout" ] = timeout msousa@1909: request_dict["timeout_s" ] = timeout_s msousa@1909: request_dict["timeout_ns"] = timeout_ns msousa@1909: request_dict["buffer" ] = ",".join(['0'] * int(child.GetParamsAttributes()[0]["children"][2]["value"])) msousa@1909: request_dict["func_nr" ] = modbus_function_dict[child.GetParamsAttributes()[0]["children"][0]["value"]][0] msousa@1909: request_dict["iotype" ] = modbus_function_dict[child.GetParamsAttributes()[0]["children"][0]["value"]][1] msousa@1909: request_dict["maxcount" ] = modbus_function_dict[child.GetParamsAttributes()[0]["children"][0]["value"]][2] msousa@1909: msousa@1909: if int(request_dict["slaveid"]) not in xrange(256): msousa@1909: self.GetCTRoot().logger.write_error("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) msousa@1909: return None msousa@1909: if int(request_dict["address"]) not in xrange(65536): msousa@1909: self.GetCTRoot().logger.write_error("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) msousa@1909: return None msousa@1909: if int(request_dict["count"]) not in xrange(1, 1+int(request_dict["maxcount"])): msousa@1909: self.GetCTRoot().logger.write_error("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) msousa@1909: return None msousa@1909: if (int(request_dict["address"]) + int(request_dict["count"])) not in xrange(1,65537): msousa@1909: self.GetCTRoot().logger.write_error("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) msousa@1909: return None msousa@1909: msousa@1909: return req_init_template % request_dict