You can help by commenting or suggesting your edit directly into the transcript. We'll review any changes before posting them. All comments are completely anonymous. For any comments that need a reply, consider emailing training@inductiveautomation.com.
Version:
LESSON LIST
-
5:08Component Event Handlers
-
4:04Script Builders
-
4:23Event Object
-
7:34Accessing Component Properties
-
4:46Finding Components on Other Windows
-
5:01Reading and Writing Tags
-
6:35Navigation Functions
-
6:26Retargeting
-
1:39Working With Different Datatypes
-
5:19Working with Datasets
-
1:53Exporting Data to CSV
-
4:30Component Extension Functions
-
3:29Component Custom Methods
Take Topic Challenge
LESSON
Reading and Writing Tags
Description
Learn how to read from and write to Tags from a script.
Video recorded using: Ignition 7.7
Transcript
(open in window)[00:00] In this lesson we're going to learn how to read and write to tags from component event scripts. So I'll put a button on the screen here, and I'm going to go ahead and write something to this simulator tag that I have. So we have a value to work with. And we're going to start with reading this value from the tag. So from our buttons actionPerformed script, we're going to call the system.tag.read function, which simply takes a tag path as an argument. Now there's a number of ways to get this tag path without having to type it in. You can right click on a tag here and hit Copy Tag Path. But the most convenient way for component event scripting is just to hit his tag button, find the tag you'd like, and it's path will be copied directly in as a string. Now, when you call a system.tag.read the value you get back is not directly the value of the tag. It's an object called a qualified value. A qualified value has three different things inside of it. It has the value. Which is the qv.value. And it has the quality and it has the timestamp. The quality of a tag is the data quality, I'm sorry the quality of a value is the data quality of whatever tag you read it from. And the timestamp is the timestamp of when that tag last changed. So if we hit okay here and run our script you can see that we're able to read the value, the quality, and the timestamp out of this tag. So it's important to remember whenever you call a system.tag.read that you're going to have to do a .value on the return object from read in order to get the actual value. Next we're going to write to this tag. To do that we called system.tag.write. So in this case, since I've got the tag path here already, I'm just going to say write, because write is very similar to read, except that it takes two arguments. The first argument is the tag path again. The second argument is the value you want to write. So in this case I'm going to write the string world to our tag. And if we run this you can see the tag's value change because we wrote to it. Now it's important to realize that there's a number of more advanced read and write functions inside of system.tag. So if we do a system.tag. and then a control space we can see a whole bunch of extra methods in here. But the ones that are most commonly used are things like readAll, which returns a sequence of qualified values, one for each tag path that you provided, as well as writeAll, which takes a sequence of tag paths and a sequence of values and it'll write the values. They have to be the same length, and it'll write the values that of correspond to each tag path. These are important methods to know about. Because if you need to write or read to a large group of tags, say 100 tags or so, calling read or write 100 times in a row is very inefficient because each one needs to communicate separately with the gateway, whereas, if you call readAll or writeAll, all of those writes are able to be tacked together into a single communication and the efficiency goes way up, and consequently the performance of your application will go way up. Similarly, we have writeSynchronous and writeAllSynchronous. So, the write call that we called earlier is an asynchronous write, meaning the write is going to return immediately, and then the write is going to actually take place sometime later. It's soon afterwards, but you don't know exactly when it's going to be. The writeSynchronous and writeAllSynchronous, those functions don't return until the write has actually been issued. Now it's important to distinguish this idea from the idea that the value has actually made it to its final destination. So for example, suppose you're writing a value down to a PLC through an OPC server, writeSynchronous will only return once the write has actually been communicated to the OPC server. But it may take some time later for that write to get all the way down to the PLC and for that value to be read back from the PLC.