Structured Text Functions

UniLogic offers a Structured Text editor that enables you to create functions.

Use these functions in the same way you use standard ladder functions. You can call them just like standard ladder functions, reuse them, and export/import them via the Library. You can also refer to Ladder functions.

The following example shows how to find the location of characters in a string, using three tags and the FIND function provided in the Structured Text library.

  1. To create a function, right-click a module and select Add Structured Text Function.
    UniLogic adds a structured text function and opens the Structured Text editor. These functions are marked with an ST icon; you can right-click and rename them.



     
  2. Add Function In/Out/Local tags. In the next image, the tags String_1 and String_2  are displayed in the editor.
    In this example, there are two input tags, String_1 and String_2, and one output tag, IndexLocation.


     
  3. Click in the editor under the text (*User code starts below this comment), and begin typing the output tag name IndexLocation, click it when it appears.
  4. Enter an = sign, and begin typing Find; click it when it appears.

  5. The Find function is automatically inserted, along with space holders for the two input tags.


     
  6. Click on the IN1 and IN2 placeholders, and begin typing the name of the input tags; select String_1 for IN1, and String_2 for IN2.

  7. Drag & drop the Structured Text function to the  ladder, and link tags to contain the values.


     
  8. Download and run the program; in online mode, the function locates the text of String_2, with the first character 11th position. If the function does not find the text, the value -1 would be returned.



     

Including Used Global Tags

This enables you to view global tags in Structured Text functions via online mode, without having to pass them as a parameter.

In the Properties Window:

  1. Click Used Globals.

  2. Begin typing the global name.

  3. Select the desired globals; click Close.
    The global tags are included as Var_External

  4. In the function, begin typing the name of the desired tag; click to include it in your code.

Structured Text Function Library

TO_REAL, TO_DINT, TO_INT, TO_SINT, TO_UDINT, TO_UINT, TO_USINT - all receive a single parameter of any primitive type and return it casted to the relevant type (f.e. TO_REAL returns the parameter as a real number).

ABS - absolute value function

SQRT -  square root function

LN - natural logarithm

LOG - base 10 logarithm

EXP - the exponent function

SIN, COS, TAN, ASIN, ACOS, ATAN - trigonometric functions

SEL(G, IN0, IN1) - G is a boolean, IN0 and IN1 are of any primitive type, but must be the same type. If G evaluates to true, return IN1, otherwise return IN0

MAX, MIN - receive 2 parameters of the same primitive type and return the max \ min of the two

LIMIT(MN, IN, MX) - all three parameters must be of the same primitive type. equivalent to MIN(MAX(MN, IN), MX)

LEN(IN) - return the length of a string

LEFT, RIGHT (IN, L) - return the left \ right substring of length L of IN (LEFT("hello", 4) -> "hell")

MID(IN, L, P) - return the substring of length L starting at index P of L (MID("hello", 2, 2) -> "el")

DELETE(IN, L, P) - return IN with the substring of length L starting at index P deleted (DELETE("hello", 2, 2) -> "hlo")

INSERT(IN1, IN2, P) - insert the string IN2 to IN1 at index P

REPLACE(IN1, IN2, L, P) - replace L characters of IN1 starting at index P by IN2

FIND(IN1, IN2) - find the character index of the first occurrence of IN2 in IN1. If not found - return -1