恭喜,你发布的帖子
发布于 2025-02-02 22:10:09
18楼
AI助手还是可以干点活的,代码调优。
FUNCTION "FC_NewCalculateAND" : Void
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_OUTPUT
ob_error : Bool;
oStr_errorInfo : String; // error information
END_VAR
VAR_IN_OUT
ioArr_conditions : Array[*] of Bool; // contactors connected to PLC or FB/FC
ioArr_types : Array[*] of Bool; // the type of contactors which be used
ioArr_used : Array[*] of Bool; // contactors which be used
ob_result { S7_HiddenAssignment := 'Show'} : Bool; // final calculate result of logic and
END_VAR
VAR_TEMP
tempIndex : DInt; // index of array
tempElementsConditionCount : DInt; // count of elements
tempElementsTypeCount : DInt;
tempElementsUsedCount : DInt;
tempUpperBound : DInt;
tempLowerBound : DInt;
tempArrayLenSame : Bool;
tempCurrentStatus : Bool; // current status of contact or which be used
tempLastStatus : Bool; // last status of contactor which be used
END_VAR
VAR CONSTANT
cOne : DInt := 16#0001;
cOneDim : UDInt := 16#0001;
"cError_ArrayLengthIsInconsistent!" : String := 'ArrayLengthIsInconsistent!';
END_VAR
BEGIN
REGION init
#tempUpperBound := UPPER_BOUND(ARR := #ioArr_used, DIM := #cOneDim);
#tempLowerBound := LOWER_BOUND(ARR := #ioArr_used, DIM := #cOneDim);
#tempElementsConditionCount := #tempUpperBound - #tempLowerBound + #cOne;
#tempElementsTypeCount := #tempUpperBound - #tempLowerBound + #cOne;
#tempElementsUsedCount := #tempUpperBound - #tempLowerBound + #cOne;
#tempArrayLenSame := #tempElementsConditionCount = #tempElementsTypeCount
AND #tempElementsConditionCount = #tempElementsUsedCount
AND #tempElementsTypeCount = #tempElementsUsedCount;
#tempLastStatus := true;
END_REGION
REGION logic
IF #tempArrayLenSame THEN
FOR #tempIndex := #tempLowerBound TO #tempUpperBound DO
IF #ioArr_used[#tempIndex] THEN
#tempCurrentStatus := #ioArr_types[#tempIndex] XOR #ioArr_conditions[#tempIndex];
#ob_result := #tempCurrentStatus AND #tempLastStatus;
#tempLastStatus := #tempCurrentStatus;
END_IF;
END_FOR;
ELSE
#ob_result := false;
END_IF;
END_REGION
REGION error
#ob_error := NOT #tempArrayLenSame;
IF #ob_error THEN
#oStr_errorInfo := #"cError_ArrayLengthIsInconsistent!";
ELSE
#oStr_errorInfo := '';
END_IF;
END_REGION
END_FUNCTION
请填写推广理由:
分享
只看
楼主