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

602 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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]);
5
On

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