这是CPU上的SCL源程序,我模仿着写在TP1200里的VB里。但是不知道怎么写?
1.为什么Y3、Y4、Y5等变量定义不了了呢?是不是要在脚本-常规-设置-参数里申明的,请教截图?
2.ByVal和ByRef有什么区别?
程序如下:
FUNCTION_BLOCK Fung_1V_5
VAR_INPUT
In: REAL ;// retain in
ZUIDU_INPUT : REAL;//RETAIN IN
X1: REAL ;// retain in
X2: REAL ;// retain in
X3: REAL ; // retain in
X4: REAL ; // retain in
X5: REAL ; // retain in
//X6: REAL ; // retain in
//X7: REAL ; // retain in
Y1: REAL ; // retain in
Y2: REAL ; // retain in
Y3: REAL ; // retain in
Y4: REAL ; // retain in
Y5: REAL ; // retain in
//Y6: REAL ; // retain in
//Y7: REAL ; // retain in
END_VAR
VAR_OUTPUT
Out: REAL ; // retain out
Error: BOOL; // retain out 0
END_VAR
VAR
K1: REAL;
K2: REAL;
K3: REAL;
K4: REAL;
K5: REAL;
END_VAR
BEGIN
K1:= 1-(ZUIDU_INPUT*Y1)/100;
K2:= 1-(ZUIDU_INPUT*Y2)/100;
K3:= 1-(ZUIDU_INPUT*Y3)/100;
K4:= 1-(ZUIDU_INPUT*Y4)/100;
K5:= 1-(ZUIDU_INPUT*Y5)/100;
IF In >= X1 AND In < X2 THEN
Out := K1 + (In - X1) * (K2 - K1)/(X2 - X1);
ELSIF In >= X2 and In < X3 THEN
Out := K2 + (In - X2) * (K3 - K2)/(X3 - X2);
ELSIF In >= X3 and In < X4 THEN
Out := K3 + (In - X3) * (K4 - K3)/(X4 - X3);
ELSIF In >= X4 and In < X5 THEN
Out := K4 + (In - X4) * (K5 - K4)/(X5 - X4);
//ELSIF In >= X5 AND In < X6 THEN
// Out := Y5 + (In - X5) * (Y6 - Y5)/(X6 - X5);
//ELSIF In >= X6 AND In < X7 THEN
//Out := Y6 + (In - X6) * (Y7 - Y6)/(X7 - X6);
ELSIF In >= X5 THEN
Out := K5;
Error := True;
ELSIF In < X1 THEN
Out := K1;
Error := True;
ELSE
Error := False;
END_IF;
END_IF;
END_FUNCTION_BLOCK
图片说明:
最佳答案
首先:SCL源码多了一个END_IF;
VBS代码
Dim In0, ZUIDU_INPUT, X1, X2, X3, X4, X5, Y1, Y2, Y3, Y4, Y5, K1, K2, K3, K4, K5, Error0, Out
K1 = 1 - (ZUIDU_INPUT * Y1) / 100
K2 = 1 - (ZUIDU_INPUT * Y2) / 100
K3 = 1 - (ZUIDU_INPUT * Y3) / 100
K4 = 1 - (ZUIDU_INPUT * Y4) / 100
K5 = 1 - (ZUIDU_INPUT * Y5) / 100
If In0 >= X1 And In0 < X2 Then
Out = K1 + (In0 - X1) * (K2 - K1) / (X2 - X1)
ElseIf In0 >= X2 And In0 < X3 Then
Out = K2 + (In0 - X2) * (K3 - K2) / (X3 - X2)
ElseIf In0 >= X3 And In0 < X4 Then
Out = K3 + (In0 - X3) * (K4 - K3) / (X4 - X3)
ElseIf In0 >= X4 And In0 < X5 Then
Out = K4 + (In0 - X4) * (K5 - K4) / (X5 - X4)
ElseIf In0 >= X5 Then
Out = K5
Error0 = True
ElseIf In0 < X1 Then
Out = K1
Error0 = True
Else
Error0 = False
End If
图片说明:
提问者对于答案的评价:
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc125748.html