Detecting tag value changes from script Ver2

<< Click to Display Table of Contents >>

Manual > Monitoring system construction guide > Building server logic > Executing your own logic in the background (SC2) >

Detecting tag value changes from script Ver2

overview

We will introduce a sample script Ver. 2 that periodically detects changes in tag values and performs processing according to the changed tag values.

 

Action used: Script Ver2 Action

 

 

 

Download the sample

The creation examples shown on this page are provided with samples.

 

The sample can be downloaded from below.

 

Server configuration file: script2_sample05.txt

Script file: script.txt

 

 

hint

For details about each event and method used in the sample, please refer to the Control Reference.

 

 

 

Operation check

Load the sample server configuration file from FA-Server and bring the application online (yellow arrow).

 

After going online, the script's operation results will be output as a log to Output01.

 

c_action_0394

 

If you change the value of the tag U01.F01.D0000, the value multiplied by 10 will be written to D0002.

Similarly, when you write a tag to U01.F01.D0001, the value multiplied by 10 is written to D0003.

 

c_action_0393

 

 

Commentary

Changes in the values of the U01.F01.D0000 and D0001 tags are monitored, and each time the value of the tag changes, a value multiplied by 10 is written to the U01.F01.D0002 and D0003 tags.

 

1)Initial Processing

 

Initialization is performed in the OnInitialize event. The key point here is that the registration process using the AddTag method is performed twice. In the second call to the AddTag method, "F" is specified as the second argument. By specifying "F" for this argument, the tag value will not be updated, and you can prevent OnTagValueChanged from occurring.

This example also uses global variables to declare read-only and write-only tags as separate arrays, allowing you to consolidate tag path descriptions in one place within the script.

 

Example script

var mvMonitorTags = c("U01.F01.D0000","U01.F01.D0001");
var mvWriteTags = c("U01.F01.D0002","U01.F01.D0003");
var mvTagCnt = 0;
var mvCnt = 0;

event OnInitialize()
{
	::SvsDump("OnInitialize:" & ::SvsGetActionName());

	mvTagCnt = ::GetValueElement(mvMonitorTags, 0);
	::AddTag(mvMonitorTags);
	::AddTag(mvWriteTags, F);
}

 

Processing content

i.The GetValueElement method is used to store the number of elements of the tag to be read in the global variable "mvTagCnt." The purpose of this process is explained separately below.

ii.Use the AddTag method to register the tags you want to use. This method can be used with an array, allowing multiple tags to be processed at once with a single call. If you specify "F" as the second argument to this method, the tag value will not be updated, preventing the occurrence of OnTagValueChanged.

 

 

hint

When monitoring value changes with the OnTagValueChanged event, for tags that only perform writing (i.e. tags that do not need to detect value changes), specify "F" as the second argument to the AddTag method. Note that this argument is optional; if it is omitted, it is assumed that "T" has been passed, and the tag value will be updated.

 

 

2)Detects changes in tag values and writes the calculation results to the tag

 
If you omit the second argument of the AddTag method when you register a tag, the OnTagValueChanged event will occur when the tag value changes. You can receive the target tag path (tagname) and the tag value (value) through the arguments of the OnTagValueChanged event.

Furthermore, the OnTagValueChanged event occurs once immediately after AddTag is performed, regardless of whether the tag value has changed. Therefore, if you want to ignore the first event, you can write processing that skips the event for the number of tags registered with AddTag. In the method used in this sample, the number of elements in the array to be AddTaged in the OnInitialize event is stored in a global variable, and the number of times the event has occurred is counted in the OnTagValueChanged event, and that number is ignored (return).

 

Example script

event OnTagValueChanged(tagname,value)
{
	::SvsDump("OnTagValueChanged(Tag:" & tagname & " Value:" & ::CStr(value) & ")");

	if(mvTagCnt > mvCnt) {
		mvCnt++;
		::SvsDump("Ignore first event.");
		return;
	}

	var vVal = value * 10;

	if(tagname == "U01.F01.D0000"){
		if(::WriteVal("U01.F01.D0002", vVal)){
			::SvsDump("WriteVal " & ::CStr(vVal)& " to U01.F01.D0002");
		} else {
			::SvsDump("WriteVal failed.");
		}
	}else if(tagname == "U01.F01.D0001"){
		if(::WriteVal("U01.F01.D0003", vVal)){
			::SvsDump("WriteVal " & ::CStr(vVal)& " to U01.F01.D0003");
		} else {
			::SvsDump("WriteVal failed.");
		}
	}
}

 

Processing content

i.Count the number of OnTagValueChanged events that occur, compare it with the number stored in the global variable "mvTagCnt" within the OnInitialize event, and skip events until the number of tags is reached.

ii.The tag value received in value is multiplied by 10 and the result is assigned to the variable.

iii.The WriteVal method is called to write the value calculated above to the tag. Here, the tag path received in tagname is checked, and if it is U01.F01.D0000, it is written to D0002, and if it is D0001, it is written to D0003.

 

 

hint

The WriteVal method can only write to one tag. If you want to write to multiple tags at once, use the WriteRefresh method.

 

 

Setup Procedure

Tag configuration

 

1.Please add tags to be used in script Ver2.
 
As an example, let's say that a virtual device unit "U01" has been added and the following tags have been registered.

 

U01.F01.D0000

U01.F01.D0001

U01.F01.D0002

U01.F01.D0003

 

 


Action Settings

 

1.Right-click Action to display the menu and select "Add" - "Action".
 
c_action_0081

 

2.Add script Ver2 action.
 
c_action_0082
 
As an example, let's assume that an action named "A01" has been added.

 

3.Open the properties of the action you added and select the script file.
 
c_action_0083
 
In a good practice, you would write the process description of the script to be executed in the script file beforehand.
 
As an example, select "script.txt" included in the sample.
 

 


Event Settings

 

1.Register the action you added to the event.
 

c_action_0084

 

 

As an example, select Periodic event and add Periodic event with a 1 second period named "E01".

 

c_action_0354

 

In the execution action of Periodic event"E01", register the call of the Script Ver2 action "A01" that you just created. No parameters need to be specified.

 

c_action_0355

 

With this setting, the script Ver2 action "A01" will be executed at one second intervals.