Reputation: 11
I am trying to retrieve a list of all screens in the project (and in the future, the tags associated with these screens). The screens are present on the devices shown in the attached screenshot. I've tried running various versions of the code, but I have never managed to get a list of screens. I would greatly appreciate any help.
Here is my code:
import clr
clr.AddReference('C:\\Program Files\\Siemens\\Automation\\Portal V15_1\\PublicAPI\\V15.1\\Siemens.Engineering.dll')
import Siemens.Engineering as tia
import Siemens.Engineering.Hmi as tia_hmi
def connect_to_tia_portal():
processes = list(tia.TiaPortal.GetProcesses())
if processes:
print("Attaching to an existing TIA Portal instance...")
return processes[0].Attach()
print("No running TIA Portal instance found. Starting a new instance...")
return tia.TiaPortal(tia.TiaPortalMode.WithUserInterface)
print('Trying to connect to an open TIA Portal instance...')
try:
mytia = connect_to_tia_portal()
print('Connected to an open TIA Portal instance.')
except Exception as e:
print(f"Failed to connect: {e}")
quit()
if mytia.Projects.Count == 0:
print("No open projects found in TIA Portal.")
quit()
myproject = mytia.Projects[0]
print(f"Connected to project: {myproject.Name}")
screens = []
for device in myproject.Devices:
print(f"Searching in device: {device.Name}")
for item in device.DeviceItems:
if isinstance(item, tia_hmi.HmiTarget):
hmi_target = item.GetService(tia_hmi.HmiTagService)
for screen in hmi_target.HmiScreens:
screens.append(screen.Name)
print(f"Screen found: {screen.Name}")
if screens:
print(f"\nTotal screens found: {len(screens)}")
else:
print("No screens found in the project.")
print("Press any key to quit")
input()
quit()
Output:
Trying to connect to an open TIA Portal instance...
Attaching to an existing TIA Portal instance...
Connected to an open TIA Portal instance.
Connected to project: uspk_81_04_07_06_V2
Searching in device: ARMKTU
Searching in device: ARMBUR
Searching in device: S71500/ET200MP station_1
No screens found in the project.
Press any key to quit
I have tried different approaches, but the output was approximately as follows:
Searching in device: ARMBUR
Error retrieving HMI Software in SIMATIC PC station: SoftwareContainer
Error retrieving HMI Software in HMI_RT_2: SoftwareContainer
Error retrieving HMI Software in IE general_1: SoftwareContainer
Upvotes: 1
Views: 63
Reputation: 423
The top level of a device contains one or more DeviceItems, and below this can be child DeviceItems. E.g:
You need to search through this hierarchically and check whether each item is a SoftwareContainer (not a HmiTarget or HmiTagService); if it is a SoftwareContainer then get the service and obtain the child .Software property (of type HmiTarget).
Example C# code (sorry don't have access to Python right now):
public static HmiSoftware GetHmiSoftware(Device hmiDevice)
{
string foo = hmiDevice.Name;
foreach (DeviceItem di in hmiDevice.DeviceItems)
{
string bar = di.Name;
HmiSoftware software = GetHmiSoftware(di);
if (software != null)
return software;
}
return null;
}
private static HmiSoftware GetHmiSoftware(DeviceItem devItem)
{
SoftwareContainer container =
devItem.GetService<SoftwareContainer>();
if (container != null)
{
HmiSoftware hmi = container.Software as HmiSoftware;
if (hmi != null)
{
return hmi;
}
}
return null;
}
Upvotes: 0