Twincat 语法

Keep Open and Learning

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: PLC

Post by jiataifeng » 30 Aug 2018 01:55

上升沿

现在用一个光电开关来做一个产品计数器,原理是一个产品经过开关,计数器加一;假设你的开关接到X0(输入点)上,那么你在程序中就要用一个X0的上升沿驱动计数器,才能保证经过一个产品,计数器只加一,若没有上升沿,PLC会在X0接通时的每一个扫描周期加一。扫描周期知道是啥概念吧,就是说若没有上升沿,开关接通一秒,计数器就加了几十上百次了

上升沿主要是用来保证只在变量改变的那个周期触发。

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: PLC

Post by jiataifeng » 01 Sep 2018 00:05

A Variable has up to 5 properties
1. Name
2. Size (Defined by the Type)
3. Value
4. Memory Location
5. PLC Address

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: PLC

Post by jiataifeng » 01 Sep 2018 00:37

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

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: PLC

Post by jiataifeng » 01 Sep 2018 00:58

data type
1.png
1.png (31.35 KiB) Viewed 3890 times
2.png
2.png (61.7 KiB) Viewed 3890 times
3.png
3.png (57.34 KiB) Viewed 3890 times
4.png
4.png (27.11 KiB) Viewed 3890 times
5.png
5.png (49.77 KiB) Viewed 3890 times
6.png
6.png (44.5 KiB) Viewed 3890 times
7.png
7.png (26.21 KiB) Viewed 3890 times
8.png
8.png (49.18 KiB) Viewed 3890 times

类型转换:
这里

字符串函数:
这里

取变量地址:ADR(变量)

STRING_TO_WSTRING2 here

转义字符:$

byt :=DT_TO_BYTE(DT#1970-01-15-05:05:05); (* Result is 129 *)
str:=DT_TO_STRING(DT#1998-02-13-14:20); (* Result is 'DT#1998-02-13-14:20' *)
vtod:=DT_TO_TOD(DT#1998-02-13-14:20); (* Result is TOD#14:20 *)
vdate:=DT_TO_DATE(DT#1998-02-13-14:20); (* Result is D#1998-02-13 *)
vdw:=DT_TO_DWORD(DT#1998-02-13-14:20); (* Result is 16#34E45690 *)
Last edited by jiataifeng on 25 Feb 2019 22:23, edited 7 times in total.

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: Twincat 语法

Post by jiataifeng » 07 Sep 2018 21:24

IF A THEN
//
ELSIF B THEN
//
.
.
ELSIF N> THEN
//
ELSE
//
END_IF;

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: Twincat 语法

Post by jiataifeng » 07 Sep 2018 21:27

CASE A OF
1:
//
2,3:
//
4..9:
//
ELSE
//
END_CASE;

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: Twincat 语法

Post by jiataifeng » 07 Sep 2018 21:28

FOR i := 1 TO 5 BY 1 DO
//
END_FOR;

WHILE A>0 DO
//
END_WHILE;


REPEAT
//
UNTIL
A > 0
END_REPEAT;


CONTINUE;//跳出当前循环
EXIT;//跳出循环

RETURN//返回


JMP label;// 类似go to
nVar1 := 0;
_label1 : nVar1 := nVar1+1;
(*instructions*)
IF (nVar1 < 10) THEN
JMP _label1;
END_IF;
Last edited by jiataifeng on 12 Sep 2018 05:03, edited 9 times in total.

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: Twincat 语法

Post by jiataifeng » 07 Sep 2018 21:28

Function A或 Function block B 的 Action成员 Reset 类似于不带参数的成员变量,可以直接调用 Function Block,或Function里的变量。这有利于封装。
Method是带参数的成员变量。

调用的形式为:
<FB-instance>.<action> or <program>.<action>
即:
实例.A.Reset();//通过找到第一个实例,顺藤摸瓜找到对应的A或B,然后调用
实例.B.Reset();



A.Reset(); //不可以,除非A是program
B.Reset(); //不可以,除非B是program
Last edited by jiataifeng on 12 Sep 2018 05:03, edited 1 time in total.

jiataifeng
Posts: 178
Joined: 30 Apr 2009 06:31

Re: Twincat 语法

Post by jiataifeng » 08 Sep 2018 03:56

Property的GET和SET会被自动调用(when a read or write access occurs to the function block that implements the property.)

TwinCAT calls the Set accessor when write access to the property occurs, i.e. when you use the property name as input parameter.
TwinCAT calls the Get accessor when read access to the property occurs, i.e. when you use the property name as output parameter.

使用不使用 REFERENCE TO 和REF= 只是影响到直接访问结构中的成员变量
例子:
TYPE ST_Sample :
STRUCT
bVar : BOOL;
nVar : INT;
END_STRUCT
END_TYPE

FUNCTION_BLOCK FB_Sample
VAR
stLocal : ST_Sample;
END_VAR


Declaration of the property FB_Sample.MyProp with the return type "REFERENCE TO ST_Sample":
PROPERTY MyProp : REFERENCE TO ST_Sample

Implementation of the Get method of the property FB_Sample.MyProp:
MyProp REF= stLocal;

Implementation of the Set method of the property FB_Sample.MyProp:
stLocal := MyProp;

Calling the Get and Set methods in the main program MAIN:
PROGRAM MAIN
VAR
fbSample : FB_Sample;
nSingleGet : INT;
stGet : ST_Sample;
bSet : BOOL;
stSet : ST_Sample;
END_VAR
// Get - single member and complete structure possible
nSingleGet := fbSample.MyProp.nVar;
stGet := fbSample.MyProp;

// Set - only complete structure possible
IF bSet THEN
fbSample.MyProp := stSet;
bSet := FALSE;
END_IF


Through the declaration of the return type of the property MyProp as "REFERENCE TO ST_Sample" and through the use of the reference operator REF= in the Get method of this property, a single element of the returned structured data type can be accessed directly on calling the property.

VAR
fbSample : FB_Sample;
nSingleGet : INT;
END_VAR
nSingleGet := fbSample.MyProp.nVar;
If the return type were only to be declared as "ST_Sample", the structure returned by the property would first have to be assigned to a local structure instance. The individual structure elements could then be queried on the basis of the local structure instance.

VAR
fbSample : FB_Sample;
stGet : ST_Sample;
nSingleGet : INT;
END_VAR
stGet := fbSample.MyProp;
nSingleGet := stGet.nVar;
Last edited by jiataifeng on 28 Sep 2018 22:53, edited 1 time in total.

Post Reply