Introduction to Variables in CODESYS Programming Software: Mastering the Fundamentals for Industrial Automation
Codesys Group |
Introduction
In PLC programming, variables serve as the foundation for constructing automated control programs. CODESYS, as a powerful PLC programming software, offers a diverse range of variable types and management methods, making programming more flexible and efficient. This article will delve into the specifics of variables in CODESYS, encompassing their definition, types, local and global variables, power-loss retention variables, structured variables, and other relevant aspects.
1
What are Variables
In programming, a variable is a named space used for storing data. They can hold different types of data such as numeric values, characters, or logical values. Variables enable programs to dynamically process data, thereby achieving complex control logic.
2
Naming Conventions for Variables
When naming variables, it is crucial to adhere strictly to the following guidelines to ensure code readability and standardization. Adopting a consistent naming style, such as Hungarian notation, CamelCase, PascalCase, or snake_case, is highly recommended.
Variables must consist only of letters, numbers, and underscores. Variables must consist only of letters, numbers, and underscores. The first character of a variable must be a letter. Variables cannot be named as reserved keywords or operators. Variable names are case-insensitive. There is no limit on the length of variable names.
3
Format Definition for Variables
VariableName : DataType [= InitialValue] ; [// Comment]
Example:
VAR
variables : INT := 10 ; //comment
variables_str : STRING := '10' ; (*comment*)
END_VAR
variables is the placeholder for the variable name;; Followed by a colon (:); INT is the data type; := is the assignment operator; 10 is the initial value; A semicolon (;) should be placed at the end of the statement;
4
Types of Variables in CODESYS
CODESYS supports a wide range of variable types, some of the most common ones are listed below: TIME: TIME OF DAY: 32-bit. (T0D#0:0:0.000 to T0D#23:59:59.999) DATE: 32-bit. (D#1970-1-1 to DATE#2106-2-7) DATE AND TIME: 32-bit. (DT#1970-1-1-00:0:00 to DT#2106-2-7-6:28:15)
Boolean Types:
BOOL :Stores binary values (0 or 1).
Integer Types:Integer Types:
BYTE :BYTE: 8-bit unsigned integer. (0 to 255) WORD :16-bit unsigned integer. (0 to 65535) DWORD :32-bit unsigned integer. (0 to 4294967295) LWORD :64-bit unsigned integer. (0 to 2^64-1) SINT :8-bit signed integer. (-128 to 127) USINT :8-bit unsigned integer. (0 to 255) INT :16-bit signed integer. (-32767 to 32767) UINT :16-bit unsigned integer. (0 to 65535) DINT :32-bit signed integer. (-2147483648 to 2147483648) UDINT : 32-bit unsigned integer. (0 to 4294967295) LINT :64-bit signed integer. (-263 to 263-1) ULINT :64-bit unsigned integer. (0 to 2^64-1)
RealNumber Types:
REAL :32-bit floating-point number. (1.401e-45 to 3.403e+38) LREAL :LREAL: 64-bit floating-point number. (2.2250738585072014e-308 to 1.7976931348623158e+308)
String Types:
STRING :Used to store strings. (Length is (n+1)*8 bytes, where n is the number of characters)。
Time-Related Types:
TIME :32 bit( T#0d0h0m0s0ms to T#49d17h2m47s295ms ) TIME OF DAY :32 bit( T0D#0:0:0.000 to T0D#23:59:59.999 ) DATE :32 bit。( D#1970-1-1 to DATE#2106-2-7 ) DATE AND TIME:32 bit。( DT#1970-1-1-00:0:00 to DT#2106-2-7-6:28:15 )
5
Local Variables and Global Variables
In the context of industrial electrical automation and programming with CODESYS or similar platforms, it's essential to understand the distinction between local variables and global variables.
5.1
Local Variables
Local variables are variables that are defined within a specific program block, such as a function or a function block. They are visible and usable only within the confines of that program block, and once the execution of the program block completes, the values of local variables are typically released. The use of local variables helps to avoid naming conflicts and issues related to data inconsistencies.
example:
FUNCTION_BLOCK FB_Test
VAR_INPUT
button : BOOL;
END_VAR
VAR_OUTPUT
out : BOOL;
END_VAR
VAR
outState : BOOL;
rise : BOOL;
riseHF : BOOL;
END_VAR
5.2
Global Variables
Global variables are variables that are defined at the program-wide level and can be accessed and modified by any part of the program. They are suitable for situations where data needs to be shared across multiple program blocks. However, caution must be exercised when using global variables to avoid accidental modifications and complex debugging processes.
Example:
VAR_GLOBAL
Always0 : BOOL;
Always1 : BOOL;
test_bool : ARRAY[0..100] OF BOOL;
test_ : BOOL;
END_VAR
6
Retentive Variables
Retentive variables are a special type of variables that retain their values even when the PLC experiences a power loss or is restarted. They are useful in situations where it is necessary to preserve the system state or critical data.
Example:
VAR_GLOBAL PERSISTENT RETAIN
runTime : TIME := T#10MS;
stopTime : TIME := T#1000MS;
data : ARRAY [0..1000] OF DUT;
Total_running_TIME : REAL;
p_position_x_start : DINT := 333299;
p_position_y_start : DINT := -100525;
END_VAR
7
Structured Variables
Structured variables are a collection of multiple variables of different types grouped together. They allow for the organization and easy management of related data within a single unit, facilitating access and manipulation.
Example:
TYPE MyStruct :
STRUCT
id: INT;
name: STRING[20];
value: REAL;
END_STRUCT
END_TYPE
VAR
structVar: MyStruct;
END_VAR
Summary
Variables in CODESYS are the cornerstone of PLC programming. Understanding and correctly utilizing variable types, naming conventions, formatting definitions, local and global variables, retentive variables, and structured variables are crucial for developing efficient and reliable control programs. It is hoped that this article has provided valuable insights into variables in CODESYS, enabling you to leverage this knowledge more effectively in practical applications.