Datatypes and variables

Top  Previous  Next

Variables

 

Variables in a VPL program are used as placeholders for values. A variable can hold some kind of value. The type of value it can hold depends on the type of the variable. In VPL a number of data types exists. Various types are used for storing integer numbers, floating-point numbers and a type exists for storing text strings. It is even possible to make arrays of the simple data types - more on that in the ARRAY section.

 

When a program has variables of different types (which is usually the case), it is possible to use typecast to convert between the different data types. Please also see the section Typecast below for more information how to case between the various data types.

 

 

 


Datatypes

 

The VPL programming language offers a number of different data types.

The different data types are able to hold different types of data - be that integer numbers, floating-point numbers or strings. In a program, it is always best to try to select the smallest possible data type that is able to handle the given values. This will help reduce the space needed for the program. If, for example, one needs a variable with a value between 1 and 10, it is sufficient to declare a variable as the type SINT. On the other hand if mathematical expressions are to be handled a FLOAT needs to be used.

 

The complete list of the native data types are:
 

Type

Description

 Value range

Size

BOOL

Boolean for storing true/false

TRUE or FALSE

1 byte

BYTE

Small unsigned integer value

0..255

1 byte

USINT

Small unsigned integer value

0..255

1 byte

SINT

Small signed integer value

-128 .. 127

1 byte

UINT

Standard unsigned integer value

0..65535

2 bytes

INT

Standard signed integer value

-32768 .. 32767

2 bytes

DINT

Large integer value

-2147483648 ,, 2147483647

4 bytes

FLOAT

Single precision floating-point value

±1.18E-38 .. ±3.4E38

4 bytes

DOUBLE

Double precision floating-point value

±2.23E-308 .. ±1.8E308

8 bytes

STRING

String for storing characters

All characters, except 0.

2 bytes for the handle

PTR

Anonymous address

n/a

4 bytes

CHANNEL

Handle to a channel

n/a

2 bytes

SEMAPHORE

Handle to a semaphore

n/a

2 bytes

MUTEX

Handle to a mutex

n/a

2 bytes

FILE

Handle to a file

n/a

2 bytes

SYSHANDLE

Generic system handle

n/a

4 bytes

CALLBACK

Address of a callback function

n/a

4 bytes

 

 

 

For declaring arrays of data, please have a look at:

 

ARRAY

This declares an ARRAY of a specific data type.

 

 


Typecast

 

Variables of different types cannot be assigned to one another when the destination is of different data type than the source. The VPL language contains a strong type-checking mechanisms, preventing errors when developing programs and the programmer is notified by syntax errors when trying to assign expressions to variables that are not of the correct type. In some cases, however, the programmer needs to assign expressions of the "wrong" type to variables, and because of that, typecasts are part of the VPL language. Typecasts are used to convert one type of expression to another type - for example if one wishes to convert a number to a BOOL variable. The BOOL type can only contain the values TRUE or FALSE, and it is therefore not possible to try to assign the value 1 or 0 (or any other number) to a variable of the type BOOL. If there is a need for such things, it is possible to use typecasting. The typecast is performed by writing the following:

 

 var := TYPE(expression);

 

Here "var" is the destination variable, "TYPE" is one of the built-in data types, and "expression" is the expression that needs to be converted.

 

 

Example:

INCLUDE rtcu.inc
 
VAR
  var_bool : BOOL;
  var_int : INT;
  var_dint : DINT;
END_VAR;
 
PROGRAM test;
 
BEGIN
 var_int := 1;
  var_bool := BOOL(var_int);   // This makes a typecats on the value 1 (an integer) and assigns it to var (the same as TRUE/ON)
  var_int := INT(var_dint);   // This makes a typecats on the var_dint to a int type
  var_dint := var_int;         // This is allowed, as a INT is a "smaller" data type than DINT is
END;
 
END_PROGRAM;