• No results found

Data Types and Conversions

In document TwinCAT 2 Manual v2_1_0 (Page 61-76)

III. PLC Overview 8. IDE

10. Data Types and Conversions

Elementary data types form the foundation of the programmer’s tools to represent and use information.

The elementary data types within TwinCAT Plc Control are below.

The BOOL data type is used to define a Boolean or Bit-wise variable.

The BOOL data type takes the value of either TRUE or FALSE at runtime.

The conversion operator BOOL_TO_INT may be used to convert a TRUE/FALSE into „1/0‟, respectively.

Declaration syntax:

VariableName : BOOL := InitialValue ; Example:

pushButton01 : BOOL ; Declaration with Initial Value:

drainValveOpen : BOOL := TRUE ;

Use the {BYTE, WORD, DWORD, SINT, USINT, INT, UINT, REAL, LREAL} data set is to define an appropriate value range for a variable.

Declaration syntax:

VariableName : DataType { := <Initial_Value } ; Example:

(*Declares a double-word sized variable named „MyDWord‟ with the initial value of „12345‟*) MyDWord : DWORD := 12345 ;

The data set {BYTE, WORD, DWORD} are considered bitwise data types; an individual bit of the data type may be extracted using the following syntax:

VariableName.bitOffset

NOTE: bitOffset cannot be a variable e.g. it must be a constant Example:

Declaration:

VAR

Status : WORD; (*Example Status Word*) END_VAR

Use:

IF Status.6 THEN (* Evaluate Bit 6 of the WORD Status *) (* Execute necessary logic *)

END_IF

Floating point number representation and subsequently numerical calculations are realized with the {REAL, LREAL} data set. The Plc Control editor recognizes a number as an integer unless explicitly defined as a REAL type. For example,

“15/9” input into a typical calculator will result in 1.667; however, the Plc Control editor evaluates such an expression in integer division resulting in the value 2. One must explicitly code “15.0/9.0” or, equally, “REAL#15/REAL#9” to evaluate as a floating point calculation.

The STRING data type is utilized to define and use ASCII character strings.

Declaration Example:

VariableName : STRING { := „StringValue‟ } ;

(* Declares STRING-type variable MyString with initialized value of “This is my string” *) MyString : STRING := „This is my string‟ ;

A STRING always occupies a memory size equal to the string size plus one byte for a null termination

character. The default size for a STRING is 80 bytes + one byte for a null terminating character, the maximum size is 255 bytes + one byte for a null terminating character.

The {TIME, TIME_OF_DAY, DATE, DATE_TIME} data set supports duration measurement and/or time stamping. The necessary data type is selected depending on scope of measurement. For example, DATE_TIME for a time stamp vs.

TIME for, say, the duration of a timer.

The Standard attempts to remove a major source of errors compared to conventional Ladder-Language PLC

programming errors with a mandate of Strong Data Types. The Plc Program Compiler should be able to detect when a programmer, for example, attempts to assign a WORD variable to another variable of type TIME. As such, Conversion Functions are integrated within Plc Control to provide explicit conversion from one elementary data type to another. The conversion operation is defined as a function.

The function returns the argument‟s value as the desired, converted data type. The general scheme is defined as

DataType1_TO_DataType2 ( VariableToConvert ) Where DataType1 is the data type of the variable being converted and DataType2 is the desired data type. For example, the code snippet converts MyWordVariable from WORD to INT

(integer).

WORD_TO_INT(MyWordVariable)

The variable, MyReturnedInt, is assigned to this converted value.

MyReturnedInt := WORD_TO_INT(MyWordVariable) ;

11. Variables

§ A Variable is a name given to a location in memory that stores a value

§ A Variable has up to 5 properties 1. Name

2. Size (Defined by the Type) 3. Value

4. Memory Location 5. PLC Address

§ In accordance with IEC 61131-3 a variable name must adhere to the following rules 1. Must begin with a Letter or an Underscore

2. Can followed by Letters, Underscores, or Numbers

§ No distinction is made between Uppercase and Lowercase Letters

§ Special characters cannot be used (!@#$%^&*)

§ Blanks or Spaces are not allowed

§ Repeated or Sequential Underscores are not allowed

Descriptive abbreviations aid in understanding the value that is held by the variable

The use of abbreviated data types in the name of the variable help in the understanding of what the variable is. By placing a lower case b in front of all BOOLEAN variables the person reading the program will know that this variable is of type BOOL without having to refer to the variables declaration. Additionally using a Capital letter at the beginning of each word in the variable name will aid in understanding

For example:

bStartConveyor is much easier to read and understand than bstartconveyor

Declaration

§ All variables must be defined between VAR and END_VAR

§ Place the name of the variable to the left of the colon

§ Place the data type to the right of the colon

§ VariableName : VariableType ;

§ bStart : BOOL ; (*bStart is of type BOOL*)

§ iProductNumber : INT; (*iProduct Number is of type INT*)

§ lrPressure : LREAL ; (*lrPressure is of type LREAL*) Variable Scope

§ Global Variables can be read and written to from anywhere in the PLC program

§ Local Variables can only be written to from within the POU where they are defined

§ The local variable of any POU can be read by first accessing the POU instance that the variable is defined in and then using the ‘.’ to access the local variables defined within that POU

§ Local variables cannot be written to from another POU Initial Values

§ All Variables have the option of assigning an initial value

§ This value will be written to memory when the PLC starts, after which the code of the PLC will control the value

§ bStart : BOOL := FALSE ; (*bStart is of type BOOL and has an initial value of FALSE*)

§ iProductNumber : INT := 1 ; (*iProduct Number is of type INT and has an initial value of 1*)

§ lrPressure : LREAL := 2.3 ; (*lrPressure is of type LREAL and has an initial value of 2.3*) It is also possible to assign an initial value to a variable in an instance of a function block

§ fbTON1 : TON := (PT := T#1s) ; (*fbTON1 is of type TON and the PT input has an initial value of 1 second*)

Constants

§ Variables defined as Constants cannot be written to by the PLC

§ Constants are declared similar to initial values

§ Use of the keyword ‘Constant’ at the beginning of the declaration section signals the compiler that the variable is a constant.

Remnant Variables

§ Remnant variables can retain their value throughout the usual program run period. These include Retain variables and Persistent variables.

§ Retained Data

§ These variables maintain their value even after an uncontrolled shutdown of the controller as well as after a normal switching off and on of the controller or at the command 'Online', 'Reset‘. When the program is run again, the stored values will be processed further. A concrete example would be a piece-counter in a production line that recommences counting after a power failure. Retain-Variables are reinitialized at a new download of the program unlike persistent variables. Variables stored with RETAIN are initialized after a "Rebuild all" of the PLC program. With a “Reset all” RETAIN variables are initialized.

§ Persistent Data

§ These variables are stored with the complete symbol. Therefore symbol generation must be selected.

Persistent variables conserve their old values after a "Rebuild all" of the PLC program. To initialize the PERSISTENT variables choose Reset all. On a TwinCAT shutdown the persistent variables are written in a special file. This file contains the old values of the persistent variables and is read on a TwinCAT start.

12. Languages

The IEC 61131-3 specifies 5 languages for writing PLC code. TwinCAT provides these plus 1 extra

§ IL – Instruction List

§ LD – Ladder Diagram

§ FBD – Function Block Diagram

§ SFC – Sequential Function Chart

§ ST – Structured Text

§ CFC – Continuous Function Chart (Non-IEC) IL – Instruction List

§ IL has a similar structure to assembly language and is comparable to the statement list language provided by Siemens.

§ In IL only 1 command can be processed per line of code.

§ The command is then followed by a variable or a literal value.

§ For example the following will increase the variable Speed by a value of 5

LD – Ladder Diagram

§ LD was created with the intention of representing the electrical wiring diagrams of relay logic

§ LD is a graphical language that displays a power rail on each side that represents the supply and the common of the wiring diagram

§ The below examples shows a common latching circuit in LD

FBD – Function Block Diagram

§ FBD is a graphical language that is similar to an electronic circuit diagram

§ The below example has the same functionality as the above latching circuit

SFC – Sequential Function Chart

§ SFC; although defined as a language, is better thought of as a way to organize code and control the sequence of operation

§ Each Step and Transition in SFC has code inside of it that can be written in any of the other languages including SFC

ST – Structured Text

§ ST is a high level language which looks similar in syntax to PASCAL

§ ST is the most powerful and flexible of all the languages

§ When using ST it is important to remember that the variable being written to (the output) is on the left

§ The below example provides the same latching circuit operation as the ones above

CFC – Continuous Function Chart (Non-IEC)

§ CFC is an additional language provided within TwinCAT, yet it is not a part of the IEC 61131-3 Standard

§ CFC is a graphical language very similar to FBD

§ The order of execution is determined by the number, and is able to be modified by the programmer

13. Functions

§ A Function is a re-useable piece of code that will process the defined inputs and return a single result

§ AND, OR, SQRT, SIN, COS, GT, LE are all examples of Functions

§ The programmer can also create their own Functions that normally involve more complicated tasks, such as converting a temperature value from Celsius to Fahrenheit or scaling an analog input value from 0-32767 to 0-10

§ Functions can be called from any other POU type, but are only capable of calling other functions

§ Note: Functions have no memory space and therefore they do not retain any values from one PLC scan to the next. Each function starts new each PLC scan.

Declaration

§ The Declaration of a Function contains 4 parts

§ The Name of the Function

§ The Return type of the Function

§ The Variables to be passed into the Function

§ The local variables used by the Function

§ The Name of the Function

§ Following the Beckhoff coding convention, the name of the Function starts with F_

§ The same IEC rules for naming of variables apply to the naming of Functions

§ Following the Name of the Function is the Return Type

§ A Function can only Return one variable

§ The Variables to be passed into the Function

§ In the below example iTempInCelsius is the Variable that is being passed into the function

Code

§ The working code of the Function

§ Tf := 9/5 * Tc + 32

§ In the example code the integer value iTempInCelsius is converted to a real number. This is a Function that is built into TwinCAT

§ The literal values of 9 and 5 both have a decimal point to signify them as REAL numbers and not integers.

§ Before writing the calculated value to the output the number is converted back to an integer. (Yes, this does cause inaccuracy due to rounding.)

Implementation

§ iTempC is declared as an INT with an initial value of 100

§ iTempF is declared as an INT with no initial value

§ In the code the Function F_CtoF is called and iTempC is passed into it.

§ The result of the Function is then stored in iTempF.

In document TwinCAT 2 Manual v2_1_0 (Page 61-76)

Related documents