The script opens an application in EcoStruxure Machine Expert and logs in to the device. If the controller is not in RUNNING state, it will be set to RUNNING. Then the variable iVar1 is read and displayed in the Messages view or command line. At the end, the application is closed.
# Script Example 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.")