Reads a variable from the I/O device. The variable tag needs to be defined in the Variable Tags database. Because the variable tag is specified as a string (not as a tag), you can ignore the data type of the variable.
This function is a blocking function. It blocks the calling Cicode task until the operation is complete.
TagRead should only be used when the variable tag name is a calculation such as sAlarmExt+".Paging". For simple assignment of variables use the assignment operator. For example, MyString = MyCluster.MyAlarm.MyProperty.
If you try to read many variables at the same time, the TagRead() function may be slow. The offset index for array variables is checked against the size of the array.
Note: TagRead can only return the values of elements for those tags having "Good" quality. If the quality of a tag is not "Good", TagRead returns an empty string. In order to obtain values of tags having not "Good" quality one can use their 'v' item. For example, TagRead("MyBadQualityTag.v"). However, the value returned by a TagRead call on a tag with not “Good” quality may be obsolete (due to TagRead asynchronous nature). Use the IsError Cicode function to control the returned error when using this function.
TagRead(STRING sTag [, INT nOffset [, STRING ClusterName]])
The string can refer to either the alarm name and the alarm property name or the tag name with optional elements and items as it is defined by Tag Extensions [Cluster.]Tag[.Element][.Item][[N]…] (Refer to Tag Extensions for more information). Currently only v item is supported by TagRead, references to other items will give an error, for eg. TagRead(“PLC_Tag.q”). If the element name is not specified, it will be resolved at runtime as an unqualified tag reference.The name of the tag can be prefixed by the name of the cluster that is "ClusterName.Tag".
To read a particular element in an array, you can enter the array name here, followed by an index to the element as follows:
The above example tells the function to read the 10th element in the array variable PLC_Array (remember, the address of the first element in an array is 0 (zero)).
If you enter an array offset using the nOffset argument, it will be added to the index value specified here. For example, TagRead("PLC_Array", 4) will read the 14th element in PLC_Array (because  means the 10th element, and an offset of 4 means 4 elements after the 10th = element 14).If you want to read the value of <Valid> element in the above example,
The offset for an array variable. This argument is optional - if not specified, it has a default value of 0.
If you enter an array index as part of the sTag argument, it will be added to this offset value. For example, TagRead("PLC_Array", 4) will read the 14th element in PLC_Array (because  means the 10th element, and an offset of 4 means 4 elements after the 10th = element 14).
Specifies the name of the cluster in which the Tag resides. The argument is enclosed in quotation marks.
The function can return:
TagReadEx, TagWrite, IODeviceControl, IODeviceInfo
// Tag1 has a STRING data type.
sStringTagValue = TagRead(“Tag1”);
sStringTagValueField = TagRead(“Tag1.Field”);
// Tag2 has an INTEGER data type.
nIntTagValue = TagRead(“Tag2”);
// Tag3 has a REAL data type.
fRealTagValue = TagRead(“Tag3”);
Tag Reference and TagReadEx() behavior in Cicode Expressions