Siemens S7 Sort Code

SmooshBall

Member
Join Date
Mar 2014
Location
Stavanger
Posts
6
Hello,

I some help to make an SCL code for sorting of 4 DINTS inn.

The plan is to have 4x Motors, where the total hours comes in to a block which sorts out which of the motors got lowest running hours.
Out of the block I want 4x INT's named Y_Dev1, Y_Dev2 etc.
The block should place the numbers 1-4 on them, where 1 is lowest.

Finding highest and lowest number isn't any problem, but the two inn middle I need some help with if some got the time :geek:
 
Since you only have 4, a quick solution might be to just reuse the same process you're already using to determine #1 and #4. If you process the two leftover ones, the lowest value becomes #2, and the highest becomes #3.

🍻

-Eric
 
So you are feeding 4x DINT into a function. Say IN_1 .. IN_4.

The function returns 4x INT Y_Dev1 .. Y_Dev4.

If IN_3 has the highest value of the 4 inputs, Y_Dev3 will return a 4.
If IN_1 has the second highest value of the 4 inputs, Y_Dev1 will return a 3.
etc.

Is that right?
 
So you are feeding 4x DINT into a function. Say IN_1 .. IN_4.

The function returns 4x INT Y_Dev1 .. Y_Dev4.

If IN_3 has the highest value of the 4 inputs, Y_Dev3 will return a 4.
If IN_1 has the second highest value of the 4 inputs, Y_Dev1 will return a 3.
etc.

Is that right?

Yeah! I'm trying to mix IF command with max /min functions now.
Trying to use fewest lines as possible.
But any advice are gladly welcome!
 
Here's one implementation. (Not attempted to use the fewest lines possible as that request came later)

Code:
FUNCTION_BLOCK FB97
VAR_INPUT
    diMotor1Hours:DINT;
    diMotor2Hours:DINT;
    diMotor3Hours:DINT;
    diMotor4hours:DINT;
END_VAR

VAR_OUTPUT
    Y_dev1:INT;
    Y_dev2:INT;
    Y_dev3:INT;
    Y_dev4:INT;

END_VAR

VAR
Y_Dev:INT;
iLoop:INT;
adiMotor:ARRAY[1..4] OF DINT;
diMotor:DINT;
END_VAR
BEGIN

adiMotor[1]:=diMotor1Hours;
adiMotor[2]:=diMotor2Hours;
adiMotor[3]:=diMotor3Hours;
adiMotor[4]:=diMotor4Hours;
//assume order
Y_Dev1:=1;
Y_Dev2:=2;
Y_Dev3:=3;
Y_Dev4:=4;

FOR iLoop:=1 TO 3 DO 
 IF adiMotor[2] < adiMotor[1] THEN 
   Y_Dev:=Y_Dev1;
   Y_Dev1:=Y_Dev2;
   Y_Dev2:=Y_Dev;
   diMotor:=adiMotor[1];
   adiMotor[1]:=adiMotor[2];
   adiMotor[2]:=diMotor;
 END_IF;
    
 IF adiMotor[3] < adiMotor[2] THEN 
   Y_Dev:=Y_Dev2;
   Y_Dev2:=Y_Dev3;
   Y_Dev3:=Y_Dev;
   diMotor:=adiMotor[2];
   adiMotor[2]:=adiMotor[3];
   adiMotor[3]:=diMotor;

 END_IF;

 IF adiMotor[4] < adiMotor[3] THEN 
   Y_Dev:=Y_Dev3;
   Y_Dev3:=Y_Dev4;
   Y_Dev4:=Y_Dev;
   diMotor:=adiMotor[3];
   adiMotor[3]:=adiMotor[4];
   adiMotor[4]:=diMotor;

 END_IF;
END_FOR;
end_function_Block
 
Shorter version
Code:
FUNCTION_BLOCK FB97
VAR_INPUT
    diMotor1Hours:DINT;
    diMotor2Hours:DINT;
    diMotor3Hours:DINT;
    diMotor4hours:DINT;
END_VAR

VAR_OUTPUT
    Y_dev1:INT;
    Y_dev2:INT;
    Y_dev3:INT;
    Y_dev4:INT;

END_VAR

VAR
Y_Dev:INT;
iLoop:INT;
oLoop:INT;
adiMotor:ARRAY[1..4] OF DINT;
aY_Dev:ARRAY[1..4] OF INT;
diMotor:DINT;
END_VAR
BEGIN

adiMotor[1]:=diMotor1Hours;
adiMotor[2]:=diMotor2Hours;
adiMotor[3]:=diMotor3Hours;
adiMotor[4]:=diMotor4Hours;
//assume order
FOR iLoop:=1 TO 4 do
aY_Dev[iLoop]:=iLoop;
END_FOR;

FOR iLoop:=1 TO 3 DO 
 FOR oLoop:=1 TO 3 DO
  IF adiMotor[oLoop+1] < adiMotor[oLoop] THEN 
   Y_Dev:=aY_Dev[oLoop];
   aY_Dev[oLoop]:=aY_Dev[oLoop+1];
   aY_Dev[oLoop+1]:=Y_Dev;
   diMotor:=adiMotor[oLoop];
   adiMotor[oLoop]:=adiMotor[oLoop+1];
   adiMotor[oLoop+1]:=diMotor;
  END_IF;
 END_FOR;    
END_FOR;
Y_dev1:=aY_dev[1];
Y_dev2:=aY_dev[2];
Y_dev3:=aY_dev[3];
Y_dev4:=aY_dev[4];

END_FUNCTION_BLOCK
 

Similar Topics

I have an application with just one servo positioning axis on a jackscrew, and there is very little room for an electrical enclosure. In...
Replies
1
Views
1,086
Hi all, I need some help here please. I'm trying to understand the SCL programming example included in the SIEMENS SCL official documentation...
Replies
1
Views
7,823
Hi need help why this “failure 5 emergency stop “ appears at every startup in the morning ? Have to shut off main switch at least 10 times on...
Replies
19
Views
195
i have two plc 1. s7-1212dc/dc/dc ip; 192.168.0.1 2. s7-1500 1513-1pn ip; 192.168.3.2 i need to get data from plc1 to plc2. any idea how to do...
Replies
5
Views
71
Hi everyone hope you'll well. Is it possible for me to download a Crack version of tia portal v13..sorry to say this but the software is very...
Replies
5
Views
170
Back
Top Bottom