Das Skript öffnet eine Applikation in EcoStruxure Machine Expert und loggt sich in das Gerät ein. Wenn die Steuerung sich nicht im Modus RUN befindet, wird sie auf RUN gesetzt. Danach wird die Variable iVar1 gelesen und in der Ansicht Meldungen oder in der Befehlszeile angezeigt. Zum Schluss wird die Applikation geschlossen.
# Skript-Beispiel ReadVariable.py
import os
# Define the necessary fields for this example
project_name = "Ohne Titel.project"
project_path = r"C:\Python"
value_name = "POU.testoutput"
try:
# Clean up any open project
if projects.primary:
projects.primary.close()
# Create the new project
proj = projects.open(os.path.join(project_path, project_name))
# Set the active application
app = proj.active_application
# Create the online application
onlineapp = online.create_online_application(app)
# Login to device
onlineapp.login(OnlineChangeOption.Try, True)
# Set status of application to "run" if not in "run"
if not onlineapp.application_state == ApplicationState.run:
onlineapp.start()
# Wait 1 second
system.delay(1000)
# Read value of iVar1
value = onlineapp.read_value(value_name)
# Display value in message view or command line
print (value_name + ": " + str(value))
# Log out from device and close the project
onlineapp.logout()
#proj.close()
except Exception as exception:
print("Error: " + str(exception))
print("Please turn on the 'Script Tracing' function to get detailed information about the script execution.")