SCL ile String ataması

x0914667

Üye
Katılım
21 Tem 2006
Mesajlar
446
Puanları
1
Yaş
40
Step7 V5.6 ile SCL kullanılarak string formatında
atama yapabilirmiyiz.
 
Aşağıdaki örnekte SCL için çeşitli veri tipleri için değer ataması yapılmıştır inceleyiniz.

DATA_BLOCK Data27
TITLE = Global data block for Chapter 27

NAME : C27GDB
FAMILY : SCL_Book
AUTHOR : Berger
VERSION : 1.0

STRUCT
Data : ARRAY [1..32] OF BYTE;
Variable1 : INT;
ActVal : INT;
Name1 : STRING[12];
DateTime1 : DT;
MotorNew : STRUCT
ActVal : INT;
Setpoint : INT;
Result : DINT;
END_STRUCT;
MotorOn : ARRAY [1..8] OF STRUCT
ActVal : INT;
Setpoint : INT;
Result : DINT;
END_STRUCT;
Name2 : STRING[12];
DateTime2 : DT;
END_STRUCT

BEGIN
END_DATA_BLOCK


FUNCTION_BLOCK Assignment
TITLE = 'Operators, expressions and value assignments'
//Examples of operators, expressions and value assignments
//Section 27.3 "Operators"
//Section 27.4 "Expressions"
//Section 27.5 "Value Assignments"

NAME : C27ASSIG
FAMILY : SCL_Book
AUTHOR : Berger
VERSION : '1.0'

VAR_INPUT
ActVal : INT;
Name1 : STRING[12];
DateTime1 : DT;
MotorNew : STRUCT
ActVal : INT;
Setpoint : INT;
Result : DINT;
END_STRUCT;
MotorOn : ARRAY [1..8] OF STRUCT
ActVal : INT;
Setpoint : INT;
Result : DINT;
END_STRUCT;
END_VAR

VAR_OUTPUT
Name2 : STRING[12];
DateTime2 : DT;
END_VAR

VAR
Manual_mode : BOOL;
Automatic : BOOL;
TooLarge : BOOL;
Switch_off : BOOL;
Setpoint : INT;
Deviation : INT;
Start_Setp : INT;
Number : INT;
Power : REAL;
Voltage : REAL;
Current : REAL;
Voltage1 : REAL;
Voltage2 : REAL;
Result : DINT;
Display : WORD;
Message : DWORD;
First_name : STRING[8];
Surname : STRING[12];
MotorOld : STRUCT
ActVal : INT;
Setpoint : INT;
Result : DINT;
END_STRUCT;
Motor : ARRAY [1..8] OF STRUCT
ActVal : INT;
Setpoint : INT;
Result : DINT;
END_STRUCT;

END_VAR

VAR_TEMP
DateTime : DT;
t_Setpoint : INT;
MotorTmp : ARRAY [1..4,1..8] OF STRUCT
ActVal : INT;
Setpoint : INT;
Result : DINT;
END_STRUCT;

Pointer1 : ANY;
Pointer2 : ANY;

END_VAR

BEGIN

(*****************************************************************************
Value assignments
******************************************************************************
With the assignment operator ":=", a value is assigned to the variable on the
left. A constant, a variable or an expression can stand to the right of the
assignment operator.
The data types on both sides of the assignment operator must be identical.
******************************************************************************
Assignment of variables with elementary data types
*****************************************************************************)

Manual_mode := I1.0; //Assignment of a binary operand
Automatic := TRUE; //Assignment of a boolean (BOOL) constant
Number := 4; //Assignment of an INT constant
Display := Data27.DW0; //Assignment of a digital operand
Setpoint := Setpoint1; //Global operand of data type INT
Setpoint := Start_Setp; //Assignment of a variable
Deviation := ActVal - Setpoint; //Assignment of an expression

//"implicit" data type conversion
//SCL converts the data types automatically in some cases
Message := Display; //WORD -> DWORD conversion
Message := Data27.DW2;
Result := ActVal - Setpoint; //INT -> DINT conversion
Result := Setpoint1;

//"explicit" data type conversion
//Data type conversion is implemented via conversion functions
Display := INT_TO_WORD(Setpoint);
Result := REAL_TO_DINT(Voltage1 - Voltage2);
Manual_mode := DWORD_TO_BOOL(Message);

(*****************************************************************************
Assignment of DT variables and STRING variables
*****************************************************************************)

DateTime := DateTime; //Assignment of a variable
DateTime2 := DT#1998-01-01-20:00:00; //Assignment of a constant
First_name := 'John'; //Assignment of a constant
//Assignment of a variable
Surname := Name1; // same length
Name2 := First_name; // shorter length

(*****************************************************************************
Assignment of fields and field components
******************************************************************************
Complete fields can only be assigned if they agree in dimensions and data types.
Part fields can be assigned "according to dimension". Field components are handled as
individual variables of the relevant data types.
*****************************************************************************)

Motor := MotorOn;
Motor[1].Setpoint := MotorOn[2].ActVal;
Motor[2].Setpoint := MotorNew.ActVal;
Motor[3].Setpoint := Setpoint1;
Motor[4].Setpoint := 14000;
WordVAr := INT_TO_WORD(MotorOn[1].Setpoint);

//Assignment of a part field
MotorTmp[1] := Motor;
MotorTmp[Number] := MotorOn;

(*****************************************************************************
Assignment of structures and structure components
******************************************************************************
Complete structures can only be assigned if they agree in data structure and
data type.
Structure components are handled like individual variables of the relevant
data types.
*****************************************************************************)

MotorOld := MotorNew;
MotorOld.Setpoint := MotorNew.ActVal;
MotorOld.Setpoint := Setpoint1;
MotorOld.Setpoint := 12000;
t_Setpoint := MotorOld.Setpoint;
Display := INT_TO_WORD(MotorNew.Setpoint);

(*****************************************************************************
ANY pointer
An ANY pointer, or more precisely, the address of any variable, can be stored
intermediately in a temporary local variable.
*****************************************************************************)

Pointer1 := MW0; //Operand
Pointer1 := "Data27".Variable1; //Variable in a data block
Pointer1 := Setpoint; //Block-local variable
Pointer1 := NIL; //Zero pointer

Pointer2 := Pointer1; //Copying a pointer

(*****************************************************************************
Expressions
******************************************************************************
Constants are combined with each other in expressions.
Depending on the operation (operator), a distinction is made between
> arithmetic expressions,
> comparison expressions and
> logical expressions.
******************************************************************************
Arithmetic expressions
Arithmetic operators: **, *, /, DIV, MOD, +, -
*****************************************************************************)

Power := Voltage * Current;
Deviation := MotorNew.Setpoint - MotorNew.ActVal;
Result := (MotorOn[2].ActVal + MotorOn[3].ActVal)/2;

(*****************************************************************************
Comparison expressions
Comparison operators: <, <=, >, >=, =, <>
*****************************************************************************)

M1.0 := Setpoint < ActVal;
TooLarge := Voltage1 > Voltage2;
Switch_off := (Voltage * Current) > 20_000;

"Data27".DX16.2 := "Data27".Data[1] = "Data27".Data[2];
IF Deviation >= 25_000 THEN Message := 16#0001_0376; END_IF;

(*****************************************************************************
Logic expressions
Logic operators: NOT, AND, &, XOR, OR
*****************************************************************************)

//Binary logic operations
Q4.0 := I1.0 & I1.1;
Q4.1 := I1.2 AND I1.3;
Q4.2 := I1.4 OR NOT I1.5;
Q4.3 := I1.6 XOR I1.7;

//Digital logic operations
MW30 := MW32 XOR MW34;
MW36 := MW38 AND 16#F00F;
IF WORD_TO_INT(Display) < 0
THEN Display := Display OR 16#8000;
ELSE Display := Display AND 16#7FFF;
END_IF;

END_FUNCTION_BLOCK
 
Son düzenleme:

Yeni mesajlar

Forum istatistikleri

Konular
130,172
Mesajlar
933,959
Kullanıcılar
453,299
Son üye
emekli 1970

Yeni konular

Geri
Üst