提供指针信息
GetPointer函数以数据类型POINTER的预定义数据单元类型(DUT)提供变量的指针信息,可用于函数或功能块。通过此类指针信息和对应的指针函数,可直接在外部数据的地址区域读取或写入函数或功能块。
输入
必须位于DT、FL或Y内存区的任何数据类型的输入变量。
输出
输入变量的指针信息。
用户自定义函数或用户自定义功能块可如下使用指针信息:
指针元素iSize可指定值的数量,即(元素数量 = Pointer.iSize / Size_Of_Var (DataElement))
通过对应的指针函数AreaOffs_ToVar和Var_ToAreaOffs以及指针元素iArea和iOffset,可直接在外部数据的地址区域进行读取或写入。
通过内存区指令Is_AreaDT或Is_AreaFL以及地址指令AdrDT_Of_Offs或AdrFL_Of_Offs,也可以将FP指令用于数据的地址区域。
输入变量必须位于内存区DT或FL,原因如下:
因为内存区指令Is_AreaDT或Is_AreaFL和地址指令AdrDT_Of_Offs或AdrFL_Of_Offs仅用于这些内存区。
因为指针指令AreaOffs_ToVar和Var_ToAreaOffs仅在这些内存区内正确工作。
此指令替代指令AreaOffs_OfVar。
所有用于编程此函数的输入和输出变量已在POU头中声明。 所有编程语言使用相同的POU头。
VAR
arData: ARRAY [0..2] OF REAL:=[3(0.0)];
iNumberOfElements: INT:=0;
rSum: REAL:=0.0;
rMean: REAL:=0.0;
rDeviation: REAL:=0.0;
@'': @'';
END_VAR
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 6 ;
NETWORK_BODY
B(B_COMMENT,,These functions show the write access to an input array by using the GetPointer32 function.ø^The input array arDara is initialized with [10.0~ 20.0~ 30.0],3,0,42,2,);
B(B_VARIN,,arData,6,4,8,6,);
B(B_F,FunCalcStatisticsInitArray1!,,15,3,28,6,,?DprArray);
B(B_F,GetPointer32!,,8,4,15,6,,?D?C);
L(1,0,1,6);
END_NETWORK_BODY
END_NET_WORK
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 9 ;
NETWORK_BODY
B(B_VARIN,,arData,6,4,8,6,);
B(B_F,GetPointer32!,,8,4,15,6,,?D?C);
B(B_VAROUT,,diNumberOfElements,29,4,31,6,);
B(B_F,FunCalcStatistics1!,,15,3,29,9,,?DprArray?AdiNumberOfElements?ArSum?ArMean?ArDeviation);
B(B_VAROUT,,rSum,29,5,31,7,);
B(B_VAROUT,,rMean,29,6,31,8,);
B(B_VAROUT,,rDeviation,29,7,31,9,);
B(B_COMMENT,,These functions show the read access from an input array using the GetPointer function.ø^Some statistical features are calculated from the input array arData [10.0; 20.0; 30.0],3,0,42,2,);
L(1,0,1,9);
END_NETWORK_BODY
END_NET_WORK
END_BODY
此函数初始化由prArray所指向的数据,增量值为10、20、30...
所有用于编程此函数的输入和输出变量已在POU头中声明。所有编程语言使用相同的POU头。
(* Calculate the number of elements from the pointer member diSize *)
diNumberOfElements:=prArray.diSize/INT_TO_DINT(Size_Of_Var(rCurrentElement) );
(* Copy the offset to an internal offset which can be increased later *)
diOffset:=prArray.diOffset;
(* Loop over all elements *)
for di := 1 to diNumberOfElements do
(* Calculate the current element di*10.0 *)
rCurrentElement:=DINT_TO_REAL(di)*10.0;
(* Write the current element to the data to which the pointer refers *)
Var_ToAreaOffs32(Var := rCurrentElement,
iArea => prArray.iArea,
diOffs => diOffset);
(* Increase the offset to the next element *)
diOffset:=diOffset + INT_TO_DINT(Size_Of_Var(rCurrentElement));
end_for;
用户自定义函数FunCalcStatistics1从prArray指向的值计算统计数据。指令AreaOffs_ToVar在内部用于从外部数据的地址区域进行读取。
所有用于编程此函数的输入和输出变量已在POU头中声明。
所有编程语言使用相同的POU头。FunCalcStatistics1
(* Calculate the number of elements from the pointer member iSize *)
diNumberOfElements:=prArray.diSize/INT_TO_DINT(Size_Of_Var(rCurrentElement) );
(* Copy the offset to an internal offset which can be increased later *)
diOffset:=prArray.diOffset;
(* Loop over all elements *)
for di := 1 to diNumberOfElements do
(* Read the current element from the data to which the pointer refers *)
rCurrentElement:=AreaOffs32_ToVar(iArea := prArray.iArea, diOffs := diOffset);
(* Calculate the sum and the square sum *)
rSum:=rSum+rCurrentElement;
rSquareSum:=rSquareSum+rCurrentElement*rCurrentElement;
(* Increase the offset to the next element *)
diOffset:=diOffset+INT_TO_DINT(Size_Of_Var(rCurrentElement));
end_for;
(* Final calculation of the additional statistical parameters *)
rNumberOfElements:=DINT_TO_REAL(diNumberOfElements);
rMean:=rSum/rNumberOfElements;
rDeviation:=SQRT(1.0/(rNumberOfElements-1.0)*rSquareSum);