Using tags in scripts
Sequences and functions read and write tags with TAG["Name"]. The member signatures are also listed in XScript — TAG.
Members
| Expression | Meaning |
|---|---|
TAG["Name"].Value | Read, or assign (assignment takes the same path as Write) |
TAG["Name"].Write(value) | Write and wait until done; true on success (number, bool, or string) |
TAG["Name"].IsGood | Whether the value is good |
TAG["Name"].Quality | Quality name ("Good", "Bad", "Stale", "Uncertain", "NotConnected", "Unlicensed") |
TAG["Name"].Unit, .Name | Unit, name |
TAG["Name"].Refresh() | Reads from the device now and waits; true if read and good |
Caution
TAG["Name"]alone is not a value. Always read through.Value. Writingdouble v = TAG["Tank1.Level"];is a build error.
Example
This function checks that the tank level is good, turns the pump off above 90, and rereads the recipe code now.
FUNCTION CheckTank()
{
double level = TAG["Tank1.Level"].Value;
string quality = TAG["Tank1.Level"].Quality;
if( TAG["Tank1.Level"].IsGood == false )
{
Log("Tank1.Level quality = {0}", quality);
return false;
}
if( level > 90.0 )
{
bool ok = TAG["Pump1.Run"].Write(false);
if( ok == false )
{
Log("Pump1.Run write failed");
return false;
}
}
if( TAG["Recipe.Code"].Refresh() )
{
string code = TAG["Recipe.Code"].Value;
Log("Recipe = {0}", code);
}
return true;
}Read behaviour
- LOCAL: taken straight from the variable.
- Communication: a tag being polled returns the value already received. Otherwise it reads on the spot and waits, then stays polled for 5 seconds, so repeated reads wait only the first time.
- Back-to-back reads: a tag already read within 0.1 s is not asked again. Checking
IsGoodand then reading.Valuecommunicates only once. - Unresponsive device: returns
Badat once without asking the device. UseRefresh()if you must ask now. - Screen event scripts: scripts that run on the screen (UI) thread, such as button events, do not wait for a tag whose last read failed; they return the value already held. This keeps the screen from freezing for the timeout of an unresponsive PLC.
- Check
IsGoodbefore using the value..Valuereturns the last value even after a failed read.
Write behaviour
- Waits until done (10 seconds at most by default). Communication tags join the same write queue as screen writes, in the order received.
- The value is read back after the write, so reading
.Valueright after shows the written value. - Failed writes are not retried automatically, to avoid running a command bit twice. The script decides from the result (bool).
- User Write Level is not checked. Read Only and Min/Max are. (Write protection and audit)
Missing tags
A name not in the table reads as the default value and writes are ignored. If the tag table has at least one tag, the code check (F7) and the build report names that are not in the table as warnings. If a tag differs only in case, its name is shown too.
If a tag is missing while you write a script, type the name after TAG[ and pick + New Tag at the end of the autocomplete list. Creating it in the New Tag window inserts TAG["Name"].. For the New Tag window rules see 'Creating a missing tag on the spot' in Using tags on screens.