1

I am trying to automate the hardware configuration in TIA Portal V17 with the help of Python & TIA Openness API. I have created a subnet & connected all the network interfaces to the subnet as shown below:

#creating a list of all found network interfaces on all stations in the station list
n_interfaces = []
for device in myproject.Devices:
    device_item_aggregation = device.DeviceItems[1].DeviceItems
    for deviceitem in device_item_aggregation:
        network_service = tia.IEngineeringServiceProvider(deviceitem).GetService[hwf.NetworkInterface]()
        if type(network_service) is hwf.NetworkInterface:
            n_interfaces.append(network_service)
# Creating subnet and IO system on the first item in the list
# Connects to subnet for remaining devices, if IO device it gets assigned to the IO system
for n in n_interfaces:
    if n_interfaces.index(n) == 0:
        subnet = n_interfaces[0].Nodes[0].CreateAndConnectToSubnet("Profinet")
        ioSystem = n_interfaces[0].IoControllers[0].CreateIoSystem("PNIO");
    else:
        n_interfaces[n_interfaces.index(n)].Nodes[0].ConnectToSubnet(subnet)
        if (n_interfaces[n_interfaces.index(n)].IoConnectors.Count) >> 0:
            n_interfaces[n_interfaces.index(n)].IoConnectors[0].ConnectToIoSystem(ioSystem);

Could anyone say how to achieve port to port connections (under Topology view)?

Thanks in advance.

Regards, Mukara

mukara
  • 97
  • 2
  • 14

2 Answers2

2

We are searching for networkports within the wrong list. We have to get one stage deeper to find a list with networkports. (see picture below)enter image description here

It should be:

n_NetworkPorts = []
for device in myproject.Devices:
    device_item_aggregation = device.DeviceItems[1].DeviceItems
    for device in device_item_aggregation:
        DeviceItems = device.DeviceItems
        for deviceitem in DeviceItems:
            networkPort = tia.IEngineeringServiceProvider(deviceitem).GetService[hwf.NetworkPort]()
            if type(networkPort) is hwf.NetworkPort:
                n_NetworkPorts.append(networkPort)

n_NetworkPorts[0].ConnectToPort(n_NetworkPorts[3]);
mukara
  • 97
  • 2
  • 14
1

You have to query all NetworkPort that you have available in your devices:

The following line in from the c# documentation, i have implemented it before in that language.

 ((IEngineeringServiceProvider)deviceItem).GetService<NetworkPort>();

Anyway comparing your code and the c# documentation i conclude you can implement the following code in Python, please take note that is almost the same implementation you performed but with a different service hwf.NetworkPort

n_NetworkPorts = []
for device in myproject.Devices:
    device_item_aggregation = device.DeviceItems[1].DeviceItems
    for deviceitem in device_item_aggregation:
        networkPort = tia.IEngineeringServiceProvider(deviceitem).GetService[hwf.NetworkPort]()
        if type(networkPort) is hwf.NetworkPort:
            n_NetworkPorts.append(networkPort)

With the same logic you can find all NetworkPort available, and after that you can extend the logic to connect them:

 # You can connect your network ports like this and it will be shown in the Topology
 n_NetworkPorts[0].ConnectToPort(n_NetworkPorts[1]);

Unfortunately i can not test it in python but with c# this code idea is definetily working

DonMiguelSanchez
  • 376
  • 3
  • 15
  • networkPort = tia.IEngineeringServiceProvider(deviceitem).GetService[hwf.NetworkPort]() returns always 'None', so the length of 'n_networkPorts' is 0! – mukara Jul 18 '23 at 09:51
  • 1
    I need to check if there is documentation for python, i think we are not making the right call with `hwf.NetworkPort`, is it defined? – DonMiguelSanchez Jul 18 '23 at 09:56
  • there is no documentation for Python, thats the big problem here! Some people check it with the help of TIA Openness Explorer and implement them in python, but I am not so sure! – mukara Jul 18 '23 at 10:00
  • 1
    the question here is why network port is None, it should be returning something in c# is working – DonMiguelSanchez Jul 18 '23 at 10:04
  • 1
    An expert from siemens says "You can't get the network port directly from the deviceitem. You would have to get the networkInterface-service first. After that you can call the networkport-service on either node[0] or node[1]. In addition i would recommend to use the 'Openness Explorer' " – mukara Jul 19 '23 at 13:09