pythonautomationsiemenstia-portal

How to create Topology connection using Siemens TIA Openness & Python?


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


Solution

  • 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]);