如何使用西门子TIA Openness和Python创建拓扑连接?

huangapple go评论312阅读模式
英文:

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

问题

我正在尝试使用Python和TIA Openness API自动化TIA Portal V17中的硬件配置。我已经创建了一个子网,并将所有网络接口连接到该子网,如下所示:

#创建一个列表,其中包含所有在站点列表中找到的所有网络接口
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)
# 在列表中的第一个项目上创建子网和IO系统
# 连接到子网以连接其余设备,如果是IO设备,则分配给IO系统
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)

有人可以告诉我如何实现端口到端口的连接(在拓扑视图下)吗?

提前感谢。

致敬,
Mukara

英文:

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

答案1

得分: 2

我们正在错误的列表中搜索网络端口。我们需要深入一层来找到一个包含网络端口的列表。 (请参见下面的图片) 如何使用西门子TIA Openness和Python创建拓扑连接?

应该是这样的:

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])
英文:

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)如何使用西门子TIA Openness和Python创建拓扑连接?

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

答案2

得分: 1

你需要查询所有在你的设备中可用的NetworkPort

c#文档中的以下代码行,我之前在该语言中已经实现过。

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

无论如何,比较你的代码和c#文档,我得出结论你可以在Python中实现以下代码,请注意,这几乎是你之前执行的相同实现,只是使用了不同的服务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)

使用相同的逻辑,你可以找到所有可用的NetworkPort,然后可以扩展逻辑来连接它们:

# 你可以像这样连接你的网络端口,它将显示在拓扑中
n_NetworkPorts[0].ConnectToPort(n_NetworkPorts[1])

不幸的是,我不能在Python中进行测试,但在c#中,这个代码思路绝对可行。

英文:

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&lt;NetworkPort&gt;();

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

huangapple
  • 本文由 发表于 2023年7月6日 14:32:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定