modbus/modbus.py
changeset 2663 54f5b8e5c8d2
parent 2662 654583c4af83
child 2669 be233279d179
child 2713 680ea4684209
equal deleted inserted replaced
2662:654583c4af83 2663:54f5b8e5c8d2
   407     # Return the number of (modbus library) nodes this specific TCP server will need
   407     # Return the number of (modbus library) nodes this specific TCP server will need
   408     #   return type: (tcp nodes, rtu nodes, ascii nodes)
   408     #   return type: (tcp nodes, rtu nodes, ascii nodes)
   409     def GetNodeCount(self):
   409     def GetNodeCount(self):
   410         return (1, 0, 0)
   410         return (1, 0, 0)
   411 
   411 
   412     # Return a list with a single tuple conatining the (location, port number)
   412     # Return a list with a single tuple conatining the (location, IP address, port number)
   413     #     location: location of this node in the configuration tree
   413     #     location   : location of this node in the configuration tree
   414     #     port number: IP port used by this Modbus/IP server
   414     #     port number: IP port used by this Modbus/IP server
       
   415     #     IP address : IP address of the network interface on which the server will be listening
       
   416     #                  ("", "*", or "#ANY#" => listening on all interfaces!)
   415     def GetIPServerPortNumbers(self):
   417     def GetIPServerPortNumbers(self):
   416         port = self.GetParamsAttributes()[0]["children"][1]["value"]
   418         port = self.ModbusServerNode.getLocal_Port_Number()
   417         return [(self.GetCurrentLocation(), port)]
   419         addr = self.ModbusServerNode.getLocal_IP_Address()
       
   420         return [(self.GetCurrentLocation(), addr, port)]
   418 
   421 
   419     def GetConfigName(self):
   422     def GetConfigName(self):
   420         """ Return the node's Configuration_Name """
   423         """ Return the node's Configuration_Name """
   421         return self.ModbusServerNode.getConfiguration_Name()
   424         return self.ModbusServerNode.getConfiguration_Name()
   422 
   425 
   689 
   692 
   690         loc_dict = {"locstr": "_".join(map(str, self.GetCurrentLocation()))}
   693         loc_dict = {"locstr": "_".join(map(str, self.GetCurrentLocation()))}
   691 
   694 
   692         # Determine the number of (modbus library) nodes ALL instances of the modbus plugin will need
   695         # Determine the number of (modbus library) nodes ALL instances of the modbus plugin will need
   693         #   total_node_count: (tcp nodes, rtu nodes, ascii nodes)
   696         #   total_node_count: (tcp nodes, rtu nodes, ascii nodes)
   694         # Also get a list with tuples of (location, IP port numbers) used by all the Modbus/IP server nodes
   697         #
       
   698         # Also get a list with tuples of (location, IP address, port number) used by all the Modbus/IP server nodes
   695         #   This list is later used to search for duplicates in port numbers!
   699         #   This list is later used to search for duplicates in port numbers!
   696         #   IPServer_port_numbers = [(location ,IPserver_port_number), ...]
   700         #   IPServer_port_numbers = [(location, IP address, port number), ...]
   697         #       location: tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
   701         #       location            : tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
   698         #       IPserver_port_number: a number (i.e. port number used by the Modbus/IP server)
   702         #       IPserver_port_number: a number (i.e. port number used by the Modbus/IP server)
       
   703         #       IP address          : IP address of the network interface on which the server will be listening
       
   704         #                             ("", "*", or "#ANY#" => listening on all interfaces!)
       
   705         #
   699         # Also get a list with tuples of (location, Configuration_Name) used by all the Modbus nodes
   706         # Also get a list with tuples of (location, Configuration_Name) used by all the Modbus nodes
   700         #   This list is later used to search for duplicates in Configuration Names!
   707         #   This list is later used to search for duplicates in Configuration Names!
   701         #   Node_Configuration_Names = [(location, Configuration_Name), ...]
   708         #   Node_Configuration_Names = [(location, Configuration_Name), ...]
   702         #       location: tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
   709         #       location          : tuple similar to (0, 3, 1) representing the location in the configuration tree "0.3.1.x"
   703         #       Configuration_Name: the "Configuration_Name" string
   710         #       Configuration_Name: the "Configuration_Name" string
   704         total_node_count = (0, 0, 0)
   711         total_node_count = (0, 0, 0)
   705         IPServer_port_numbers    = []
   712         IPServer_port_numbers    = []
   706         Node_Configuration_Names = []
   713         Node_Configuration_Names = []
   707         for CTNInstance in self.GetCTRoot().IterChildren():
   714         for CTNInstance in self.GetCTRoot().IterChildren():
   726                                         a2=_lt_to_str(Node_Configuration_Names[j][0]),
   733                                         a2=_lt_to_str(Node_Configuration_Names[j][0]),
   727                                         a3=Node_Configuration_Names[j][1])
   734                                         a3=Node_Configuration_Names[j][1])
   728                     self.FatalError(error_message)
   735                     self.FatalError(error_message)
   729 
   736 
   730         # Search for use of duplicate port numbers by Modbus/IP servers
   737         # Search for use of duplicate port numbers by Modbus/IP servers
   731         # print IPServer_port_numbers
   738         # Note: We only consider duplicate port numbers if using the same network interface!
   732         # ..but first define a lambda function to convert a tuple with the config tree location to a nice looking string
   739         i = 0
   733         #   for e.g., convert the tuple (0, 3, 4) to "0.3.4"
   740         for loc1, addr1, port1 in IPServer_port_numbers[:-1]:
   734         for i in range(0, len(IPServer_port_numbers) - 1):
   741             i = i + 1
   735             for j in range(i + 1, len(IPServer_port_numbers)):
   742             for loc2, addr2, port2 in IPServer_port_numbers[i:]:
   736                 if IPServer_port_numbers[i][1] == IPServer_port_numbers[j][1]:
   743                 if (port1 == port2) and (
   737                     error_message = _("Error: Modbus/IP Servers %{a1}.x and %{a2}.x use the same port number {a3}.\n").format(
   744                           (addr1 == addr2)   # on the same network interface
   738                                         a1=_lt_to_str(IPServer_port_numbers[i][0]),
   745                        or (addr1 == "") or (addr1 == "*") or (addr1 == "#ANY#") # or one (or both) of the servers
   739                                         a2=_lt_to_str(IPServer_port_numbers[j][0]),
   746                        or (addr2 == "") or (addr2 == "*") or (addr2 == "#ANY#") # use all available network interfaces
   740                                         a3=IPServer_port_numbers[j][1])
   747                    ):
       
   748                     error_message = _("Error: Modbus plugin nodes %{a1}.x and %{a2}.x use same port number \"{a3}\" " + 
       
   749                                       "on the same (or overlapping) network interfaces \"{a4}\" and \"{a5}\".\n").format(
       
   750                                         a1=_lt_to_str(loc1), a2=_lt_to_str(loc2), a3=port1, a4=addr1, a5=addr2)
   741                     self.FatalError(error_message)
   751                     self.FatalError(error_message)
   742 
   752 
   743         # Determine the current location in Beremiz's project configuration
   753         # Determine the current location in Beremiz's project configuration
   744         # tree
   754         # tree
   745         current_location = self.GetCurrentLocation()
   755         current_location = self.GetCurrentLocation()