Reading and Writing to Tags
Reading and Writing Tags
Watch the videoIn many cases, the binding system is the most appropriate way to display a Tag value on the screen. However, you may wish to access a Tag's value in a script. Using the system functions, you can read from a Tag and write to a Tag in Ignition.
Script Builder
If you simply need to write to value to a Tag from a script, you can use the Set Tag Value tab of the Script Editor. To learn more, refer to the sections on Script Builders for more information. However, if you need to do more than just send a single write, see the section below.
Know Your Scope
Whenever a script from a Shared resource (such an Alarm Pipeline) attempts to interact with a Tag, the script must specify the Tag Provider, otherwise, the script could return an exception. Tag Providers are always included at the start of the Tag Path and look like the following: [tagProvider]
When interacting with a Tag from the Project scope (such as a script on a component) you may optionally include the Tag provider. If omitted, the Project default Tag provider will be used.
# Reading a Tag without the Tag Provider. You would not want to use this from the Shared scope.
system.tag.read("My/Tag/Path")
# Reading from the same Tag, but specifying the Tag Provider. This format may be used safely in either scope.
system.tag.read("[default]My/Tag/Path")
Manual Tag Reads
Reading from a Single Tag
Reading a Tag from a script is accomplished with the system.tag.read() function, which requires the Tag path you wish to read from. Note, that this function returns a 'Qualified Value', this object is more than just the value. A Qualified Value is the Tag value that has three attributes; Value, Quality, and TimeStamp. If you are getting multiple attributes, make sure to read the Tag once and not once for each attribute.
# get the tag value
value = system.tag.read("tagPath").value
# get all three attributes
tag = system.tag.read("tagPath")
value = tag.value
quality = tag.quality
timestamp= tag.timestamp
Reading from Multiple Tags
In the event you need to read from a large number of Tags, it is more efficient to use system.tag.readAll() instead which reads a Python List of Tag Paths.
# Create a List of Tag Paths to read
paths = ["Scripting/Tags/Alarm_1", "Scripting/Tags/Alarm_2", "Scripting/Tags/Alarm_3"]
# Read the Tags, and store the complex results in a variable
values = system.tag.readAll(paths)
# For each Tag Path, iterate through our results...
for index in range(len(paths)):
# ...and do something with the individual values
print values[index].value
Relative Tag Paths
Like elsewhere in Ignition, relative paths may be used from within a script. This is especially useful when writing a Tag Event script inside of a UDT, as you can specify relative members in the same UDT with "[.]".
# Assuming a UDT with two sub-members, a script from one can read from one member to the other using the following code:
system.tag.read([.]otherMember).value
Manual Tag Writes
Writing to a Single Tag
Writing to a Tag with the system.tag.write() function requires the Tag path, as well as the value you wish to write to the Tag.
system.tag.write("tagPath","Hello World")
Writing to Multiple Tags
Writing to multiple Tags from a single script should use the system.tag.writeAll() function, as it is more efficient than several system.tag.write()
calls.
# Create a List of Tag Paths to write to
paths = ["Scripting/Tags/Alarm_Setpoint_1", "Scripting/Tags/Alarm_Setpoint_2"]
# Create a List of values to write
values = [72, 72]
# Send of the write requests
system.tag.writeAll(paths, values)