Skip to main content
InSource Solutions

TN IT191 Scripting: FOR loops in InTouch

InSource_Logo_Transparent.png

 

Description

When scripting in InTouch it can be useful to loop through a set of steps repeatedly.  One way to do this is with a FOR loop.  This article will describe how to use one, as well as the limitations.

 

  • Author: Dillon Perera
  • Published: 12/31/2015
  • Applies to: InTouch 10.0 and up

 

 

Details

The FOR loop allows you to run a set of steps repeatedly, a specific number of times.  This is typically useful when you have multiple tags with similar names, and need to perform an action on all of them.

 

Limitations

Before using a FOR loop it is important to understand the limitations imposed by InTouch.

1. FOR loops run synchronously, which means that everything else within InTouch will stop and wait for the FOR loop to complete.  This means that while a FOR loop is running InTouch graphics will not update, no other scripts will run, and IO communication will not occur.

- Workaround: You can place the script in an asynchronous QuickFunction script, but be aware that QuickFunctions do have some additional limitations not seen in other script types.

2. Anytime a loop is used in a script, there is the risk of it running indefinitely if there is an error in the script.  To avoid this problem InTouch has a timer that, by default, will only allow a script to execute for a maximum of 5 seconds.  If the script is still running after this timer elapses, the script execution will be terminated.

- Workaround:This timeout period can be modified from the intouch.ini file, by adding "LoopTimeout = X", where X is the number of seconds you would like the software to wait.

 

Best Practice:

1. If these limitations are a concern, you should first look for alternative approaches, such as writing the script in a different way, or moving this functionality somewhere else, such as into a PLC. The workarounds listed above should be used after first considering the alternatives.

2. Due to the script running synchronously, and pausing all other execution, loops like this should be used sparingly.

 

How it works:

The general syntax for a FOR loop is:

FOR [VARIABLE] = [StartingNumber] TO [FinalNumber]

[Steps that you want to complete]

[...]

[...]

NEXT;

 

The FOR loop works like this:

1. VARIABLE will be set to the StartingNumber value.

2. If VARIABLE's value is now greater than FinalNumber it will exit the loop.

2. If VARIABLE's value is less than or equal to FinalNumber it will then run the code within the FOR loop ("Steps you want to complete")

3. When it gets to NEXT it will then increment the value of VARIABLE by 1.

4. Begin again at step 2, comparing StartingNumber to FinalNumber

 

Examples:

 

Example 1: The following script is a simple example of using a FOR loop.  It will loop 10 times, increasing the value of MyValue by 1 each time; in total increasing MyValue by 10 each time it is run:

FOR MyLoopCounter = 1 TO 10

MyValue = MyValue +1;

NEXT;

 

Example 2: A common use case of a FOR loop would be to reset a group of tags which all have similar names, only differing by a number at the end.  The following script will reset a set of Memory Integer tags named MyTag1 through MyTag5 to a value of 0, without requiring the developer to write out multiple lines of repetitive script.  This script will utilize an Indirect Analog tag named indPointer to update MyTag1 through MyTag5.  Comments have been added to explain the script:

 

{Using DIM to create a temporary counter Tag, instead of using one in my Tagname Dictionary}

DIM TempLoopCounter AS INTEGER;

 

{Run this loop 5 times}

FOR TempLoopCounter =  1 TO 5

{Point the Indirect tag indPointer to MyTag + the loop number}

indPointer.Name = "MyTag" + Text(TempLoopCounter,"0");

{Set the MyTag# to 0}

indPointer=0;

NEXT;