恭喜,你发布的帖子
发布于 2016-11-08 15:38:54
8楼
Sort源代码:
FUNCTION_BLOCK Sort
NAME:Sort
FAMILY:SCAMPLE
AUTHOR:NC
VERSION:'1.0'
VAR_INPUT
Start:BOOL:=FALSE;
Stop :BOOL:=FALSE;
Source:ANY;
Num:INT:=8;
END_VAR
VAR_OUTPUT
First:INT;
Last :INT;
Error:BOOL;
Start1:BOOL;
Start2:BOOL;
Start3:BOOL;
Start4:BOOL;
Start5:BOOL;
Start6:BOOL;
Start7:BOOL;
Start8:BOOL;
Stop1:BOOL;
Stop2:BOOL;
Stop3:BOOL;
Stop4:BOOL;
Stop5:BOOL;
Stop6:BOOL;
Stop7:BOOL;
Stop8:BOOL;
END_VAR
VAR_TEMP
RET_VAL1 :INT;
DataSwip:MotorInfo ;
ExtCyc,IntCyc:INT;
Index:INT;
END_VAR
VAR
DataCopy:ARRAY[1..8]OF MotorInfo;
MotorStartAry:ARRAY[1..8]OF BOOL;
MotorStopAry:ARRAY[1..8]OF BOOL;
CycCount:INT;
END_VAR
BEGIN
// Keep output
IF (First<>0) OR (Last<>0) THEN
CycCount:=CycCount + 1;
IF (CycCount >0) THEN
First:=0;
Last :=0;
FOR index:=1 TO Num BY 1 DO
MotorStartAry[index]:=FALSE;
MotorStopAry[index]:=FALSE;
END_FOR;
END_IF;
ELSE
CycCount:=0;
END_IF;
//Copy motor information ,get ready for sort runtimes
IF Start OR Stop THEN
RET_VAL1:= BLKMOV(SRCBLK :=Source,DSTBLK :=DataCopy );//Call SFC20
FOR index:=1 TO Num BY 1 DO
DataCopy[index].Number:=index;
END_FOR;
//bubble sort runtime, swip motor number and motor state at the same time
FOR ExtCyc:=1 TO (Num - 1) BY 1 DO
FOR IntCyc:=1 TO (Num - ExtCyc) BY 1 DO
IF DataCopy[IntCyc].Runtime > DataCopy[IntCyc + 1].Runtime THEN
DataSwip:=DataCopy[IntCyc];
DataCopy[IntCyc]:=DataCopy[IntCyc + 1];
DataCopy[IntCyc + 1]:=DataSwip;
END_IF;
END_FOR;
END_FOR;
END_IF;
//Motor Start Auto
IF Start THEN
FOR Index:=1 TO Num BY 1 DO
IF DataCopy[Index].StartEn THEN
First:=DataCopy[Index].Number;
MotorStartAry[First]:=TRUE;
MotorStopAry[First]:=FALSE;
EXIT;
END_IF;
END_FOR;
END_IF;
//Motor Stop Auto
IF Stop THEN
FOR Index:=Num TO 1 BY -1 DO
IF DataCopy[Index].StopEn THEN
last:=DataCopy[Index].Number;
MotorStartAry[Last]:=FALSE;
MotorStopAry[Last]:=TRUE;
EXIT;
END_IF;
END_FOR;
END_IF;
Start1:=MotorStartAry[1]; Stop1:=MotorStopAry[1];
Start2:=MotorStartAry[2]; Stop2:=MotorStopAry[2];
Start3:=MotorStartAry[3]; Stop3:=MotorStopAry[3];
Start4:=MotorStartAry[4]; Stop4:=MotorStopAry[4];
Start5:=MotorStartAry[5]; Stop5:=MotorStopAry[5];
Start6:=MotorStartAry[6]; Stop6:=MotorStopAry[6];
Start7:=MotorStartAry[7]; Stop7:=MotorStopAry[7];
Start8:=MotorStartAry[8]; Stop8:=MotorStopAry[8];
Start:=FALSE;
Stop:= FALSE;
END_FUNCTION_BLOCK
感谢楼上两位。
我贴出我的程序,至少逻辑上感觉是可以的,只是还不够简单,等待以后再优化。
请填写推广理由:
分享
只看
楼主