Access tags periodically 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) >

Access tags periodically from script Ver2

overview

Here is a sample of script Ver2 that reads tags at regular intervals and writes the results of calculations between tags to another tag.

 

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_sample04.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_0391

 

Verify that the sum of the values of the tags U01.F01.SD0000 and SD0001 is written to U01.F01.D0000 every 5 seconds.

 

c_action_0392

 

 

Commentary

The values of the U01.F01.SD0000 and SD0001 tags are read at a fixed cycle of 5 seconds, and the result of adding up the values of each tag is written to the U01.F01.D0000 tag.

 

1)Initial Processing

 

Initial processing is performed in the OnInitialize event. The key point here is that when accessing a tag from a script, you must always register the tag using the AddTag method. Also, if you want to read the tag value immediately after processing begins, you can refresh the tag value by calling the ReadRefresh method after AddTag.

 

Example script

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

	::AddTag(c("U01.F01.D0000", "U01.F01.SD0000", "U01.F01.SD0001"));
	::ReadRefresh(c("U01.F01.SD0000", "U01.F01.SD0001"));
	::SetTimer(0, 5000, -1);
}

 

Processing content

i.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.

ii.Call the ReadRefresh method to update the tag value. To avoid unnecessary update processing load, only update tags that are read.

iii.SetTimer sets the timer. In this example, a timer of 5000 milliseconds (5 seconds) is set with an infinite number of executions (-1), so the OnTimer event will occur repeatedly every 5 seconds.

 

 

hint

Immediately after FA-Server goes online, various processes will run in stages. For example, for tags that communicate with PLC, the tag will be in a state before the value is read (the tag status is UNCEARTAIN) until the communication driver obtains the initial value. If the processing description of the script reads the tag value and performs processing, it is safe to explicitly update the tag value using the ReadRefresh method so that the tag value is updated. The ReadRefresh method has an optional argument that allows you to specify where to obtain the tag value (either the device value or the server cache value. If omitted, the server cache value will be used). Please specify an appropriate argument for this purpose. Note that if you specify a device value in the ReadRefresh method and call it frequently, it may affect the entire tag update cycle. Therefore, do not call the ReadRefresh method every time during a repetitive process, but only call it when initializing the process or when you definitely want the actual tag value.

 

 

2)Read two tags at regular intervals and write the calculation results to the tags.

 
In the OnTimer event that occurs every 5 seconds, the GetVal method is called to read the tags U01.F01.SD0000 and SD0001, and the values of the read tags are added together and written using the WriteVal method.D0000Write on the tag.

 

Example script

event OnTimer(timerid,counter)
{
	::SvsDump("OnTimer Id:" & ::CStr(timerid) & " counter:" & ::CStr(counter));

	var vVals = ::GetVal(c("U01.F01.SD0000", "U01.F01.SD0001"));
	var vVal = vVals[0] + vVals[1];
	
	if(::WriteVal("U01.F01.D0000", vVal)){
		::SvsDump("WriteVal " & ::CStr(vVal)& " to U01.F01.D0000");
	} else {
		::SvsDump("WriteVal failed.");
	}
}

 

Processing content

i.The GetVal method can process the reading of multiple tags at once. By passing the tag paths of multiple tags as an array, you can receive the values of each tag as an array. In this example, two tags are read at once.

ii.Add the result received by GetVal and assign the result to the variable.

iii.The WriteVal method is called to write the value added above to D0000. This method returns TRUE if successful. In this example, the log output content of the SvsDump method is switched depending on whether the write process was successful or not.

 

 

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.SD0000

U01.F01.SD0001

 

Furthermore, the SD tag of a virtual device is a tag called a special register, and its value changes automatically.

 

 

 


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.