Declaration of STRING variables (incl. access)

Syntax
VAR | VAR_INPUT | VAR_IN_OUT | VAR_OUTPUT | VAR_GLOBAL | VAR_EXTERNAL | VAR_TEMP
name_3, name_4, ..., name_n : STRING[length] := 'initial-value';
...
(* Additional pieces of data, such as partial addresses, are also possible for the variables - depending on the section. *)
END_VAR
 
TYPE
name_5: ARRAY [x..y] OF STRING[length] := ['initial-value_1', 'initial-value_2', .., 'initial-value_n']; (* Up to 4 dimensions are possible. *)
...
name_1: STRUCT
name_e1 : STRING[length] := 'initial-value'; (* Additional pieces of data, such as partial addresses. are also possible for the structure elements. *)
...
END_STRUCT; 
END_TYPE

Meaning

Declaration of one or more variables of →data type STRING (also known as STRING variables or →character string variables) with a maximum length (length), name_3, name_4 etc. must be →IEC-identifiers.
Use the optional →initial value := 'initial-value' to assign a value to the variable (details: see "Initialization of variables in ST").

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg The length and the initial value may be a →constant expression.

  • logi.CAD 3 allows STRING variables with a maximum length of up to 32.767 characters.

  • Special characters, such as umlauts, within the initial value or within the value for an assignment must be entered as a three-character combination of the dollar sign ($) followed by two hexadecimal digits. Example: StringVar1 : STRING[10] := '$A3 30.50'; (for the initial value £ 30.50) – see →character string literal, under "Examples for character string literals" for a list of selected special characters.

The declaration of STRING variables is possible within these sections:
(Consult the respective description of the section about possibly additional possibilities for the variable.)

Section

The declaration of the STRING variable is done as:

VAR ... END_VAR

internal variable (see "Declaration of internal variables in ST")

VAR_INPUT ... END_VAR

input variable (see "Declaration of input variables in ST")

VAR_OUTPUT ... END_VAR

output variable (see "Declaration of output variables in ST")

VAR_IN_OUT ... END_VAR

in-out variable (see ""Declaration of in-out variables in ST)

VAR_GLOBAL ... END_VAR

global variable (see "Declaration of external variables in ST")

VAR_EXTERNAL ... END_VAR

external variable (see "Declaration of external variables in ST")

VAR_TEMP ... END_VAR

temporary variable (see "Declaration of temporary variables in ST")

TYPE ... END_TYPE

a →user-defined data type or as an element of it (see " Declaration of a data type in ST ")

Example
VAR
var4 : STRING[10]; (* 'var4' has a maximum length of 10 characters. *)
END_VAR
 
VAR_OUTPUT
OUT4 : STRING[5] := 'ABC'; (* 'OUT4' has a maximum length of 5 characters and is initialized with character string 'ABC'. *)
END_VAR
 
TYPE
myString : ARRAY [1..3] OF STRING[4] := ['ABC', 2('DEF')];
(* 3 ARRAY elements of data type 'STRING' have a maximum length of 4 characters. *)
(* initialization of the 1st element with 'ABC', initialization of the other 2 elements with 'DEF'. *)
 
RangeString : STRUCT (* structure data type with 2 elements *)
min : STRING[6]; (* 1st element of data type 'STRING' has a maximum length of 6 characters, no initialization. *)
max : STRING[7] := 'ABCD'; (* 2nd element of data type 'STRING' has a maximum length of 7 characters, initialization with 'ABCD'. *)
END_STRUCT;
END_TYPE

Accessing the STRING variables

If you want to access single characters of a STRING variable within →assignments, enter the name of the variable (e.g. var4) and the corresponding position of the character (e.g. 2) enclosed in brackets (e.g. var4[2]). The positions are starting with "1".

Assignments of a STRING variable to another STRING variable with different maximum length is possible. If the STRING variable to which the assignment is executed has a smaller length than the STRING variable from which the assignment is executed, logi.CAD 3 executes the assignment only until the maximum length of the STRING variable to which the assignment is executed. Moreover, the output ENO of the embracing →POU is additionally set to value FALSE (or an equivalent).

If the character combination '$00' is assigned to a single character of a STRING variable, the character string of the STRING variable is terminated at this position. Such character strings are terminated in case of $00 in the Values of Variables view as well.
However, the complete character string is copied in assignments. This makes it possible to have the Values of Variables view display single characters behind $00, if you access the assigned characters behind $00.

In case of a smaller length of the assigned STRING variable and a character combination '$00' in it, logi.CAD 3 executes the assignment only until the maximum length of the STRING variable to which the assignment is excecuted. In this case the output ENO of the embracing POU is not set to value FALSE (or an equivalent).

Examples
VAR
var4, var5 : STRING[10]; (* 'var4' and 'var5' have a maximum length of 10 characters. *)
var6 : STRING[5]; (* 'var6' has a maximum length of 5 characters. *)
Char1 : CHAR;
END_VAR
VAR_OUTPUT
OUT4 : STRING[5] := 'ABC'; (* 'OUT4' has a maximum length of 5 characters and is initialized with character string 'ABC'. *)
OUT5 : STRING[10] := '1234567890'; (* 'OUT5' has a maximum length of 10 characters and is initialized with character string '1234567890'. *)
END_VAR
 
(* assignments: accessing single characters of a STRING variable *)
Char1 := OUT4[3]; (* The character of the 3rd position of 'OUT4' , i.e. 'C', is assigned to 'Char1'. Result: Char1 := 'C' *)
OUT4[2] := 'D'; (* The character 'D' is assigned to the 2nd position of 'OUT4'. Result for 'OUT4' := 'ADC' *)
var4[1] := 'A'; (* The character 'A' is assigned to the 1st position of 'var4'. Result for 'var4' := 'A' *)
var4[2] := OUT4[2]; (* The character 'D' is assigned to the 2nd position of 'var4'. Result for 'var4' := 'AD' *) 
 
(* assignments: STRING variable to STRING variable *)
var5 := OUT4; (* OK, because max. length of 'OUT4' = 5 and max. length of 'var5' = 10. Result for 'var5' := 'ADC' and ENO of the embracing POU := TRUE *)
var6 := OUT5; (* OK, but assignment only to max. length of 'var6', i.e. to the character position '5'. Result for 'var6' := '12345' and ENO of the embracing POU := FALSE *)
(* special case: character strings with $00 *)
VAR
varA, varB : STRING[10] := '1234567890'; (* 'varA' and 'varB' are initialized with character string '1234567890'. *)
varC, varD : STRING[10];
varE : STRING[10] := '1234567890'; (* 'varE' is initialized with character string '1234567890'. *)
varF : STRING[5];
CharA, CharB : CHAR;
END_VAR
VarA[6] := '$00'; (* Due to these assignments, 'varA' and 'varB' are now terminated at the 6th position. Displayed value in 'Values of Variables' view: '12345' *)
VarB[6] := '$00';
VarE[4] := '$00'; (* Due to this assignment, 'varE' is now terminated at the 4th position. Displayed value in 'Values of Variables' view: '123' *)
 
varC := varA; (* Inspite of $00, the complete character string is copied for 'varC'. Result for 'varC' := '12345$007890' - but displayed value in 'Values of Variables' view: '12345' - and ENO of the embracing POU := TRUE *)
varD := MOVE(IN := varB); (* Inspite of $00, the complete character string is copied for 'varD'. Result for 'varC' := '12345$007890' - but displayed value in 'Values of Variables' view: '12345' - and ENO of the embracing POU := TRUE *)
varF := varE; (* Inspite of $00, the complete character string is copied until the max. length of 'varF'. Result for 'varF' := '123$005' - but displayed value in 'Values of Variables' view: '123' - and ENO of the embracing POU := TRUE *)
CharA := varC[8]; (* Result for 'CharA' := '8' - inspite of $00 on the 6th position of 'varC' *)
CharB := varF[5]; (* Result for 'CharB' := '5' - inspite of $00 on the 4th position of 'varF' *)

Declaring and accessing STRING variables of ARRAY data type

If you want to declare a STRING variable that is based on an ARRAY data type, enter the STRING data type with the maximum length as data type when declaring the ARRAY data type.

Example for declaring
TYPE
myArray : ARRAY [1..3] OF STRING[4]; (* declaring 3 STRING elements, each element has a maximum length of 4 characters *)
END_TYPE

You may access single characters of such variables by means of the following construction:

Example for accessing
VAR
var2 : myArray;
accessA, accessB : CHAR; (* variables for the access *)
accessC : STRING[4]; (* auxiliary variable for the alternate access *)
END_VAR
 
accessA := var2[1][2]; (* accessing the 2nd character of the 1st ARRAY element *)
 
(* alternate access *)
accessC := var2[1]; (* accessing the 1st ARRAY element *)
accessB := accessC[2]; (* accessing the 2nd character of the 1st ARRAY element *)