SCL + find maximum value

Join Date
Jul 2014
Location
Here
Posts
17
Hi all,


I'm not too familiar with SCL programming and I need some guidance on writing a simple code in SCL that scans through a given number real values and returns the highest value found in the table. Thanks in advance!
 
Here's an example:

Code:
FUNCTION_BLOCK FB988
CONST Size := 9; END_CONST    
VAR Data:ARRAY[1..Size] OF REAL:=[1.0,3.0,2.0,9.0,13.0,7.0,21.0,19.0,9.0];
    i:INT;
    rBiggest:REAL;
END_VAR
BEGIN
rBiggest:=-1e38;
FOR i:=1 TO Size DO
    IF Data[i] > rBiggest THEN
        rBiggest:=Data[i];
    END_IF;    
END_FOR;
rBiggest:=rBiggest; //for monitor only 
END_FUNCTION_BLOCK
 
What if the biggest value is -3e38 ?
OK, not really an issue, but the code would be better if you dont have to consider what is the possible range of the data type.

I suggest:
Code:
rBiggest:= [COLOR="Red"]Data[1][/COLOR];
FOR i:=[COLOR="red"]2[/COLOR] TO Size DO
    IF Data[i] > rBiggest THEN
        rBiggest:=Data[i];
    END_IF;    
END_FOR;
 
Thanks, this was good piece of info. Any experience on PCS7 and arrays? My plan was to use an array as an input to my block but I only receive an error parameter type S7_ARRAY is not supported.
 
Thanks, this was good piece of info. Any experience on PCS7 and arrays? My plan was to use an array as an input to my block but I only receive an error parameter type S7_ARRAY is not supported.

What block in which language was it? PCS7 will usually be programmed in CFC and perhaps you need to input a pointer to the array and not the array itself.
 
The CFC library comes with a function to get the maximum of 2, 4 and 8 Real values. If you need to compare an even number of variables, you can cascade them to the number you need.
See CFC library -> ELEMENTA\Blocks -> MATH_FP.

If the values you want to select are from a driver block and you transfer the result value to an OS block, then I would write similar blocks like the ones from the CFC library for 2, 4, 8 values, but the parameters and return value is of type struct {real, byte}. So you get also the variable status wherever you may need this.
 
What if the biggest value is -3e38 ?
OK, not really an issue, but the code would be better if you dont have to consider what is the possible range of the data type.

I suggest:
Code:
rBiggest:= [COLOR=Red]Data[1][/COLOR];
FOR i:=[COLOR=red]2[/COLOR] TO Size DO
    IF Data[i] > rBiggest THEN
        rBiggest:=Data[i];
    END_IF;    
END_FOR;

That's a good point. It reminds me of the other potential issue OP needs to consider: do they want the largest absolute value, or the most positive number? Do they consider -5 as bigger or smaller than -4?
 
I know the standard function in the library but I thought this would be a "nicer" way to scan 100 variables rather than playing with the boxes. Now I'm bringing in any variable to my scl block which would contain the data that I want to scan and block moving the table to a local table. Let's see how it works out... :unsure:
 
If I remember correctly Siemens CFC does not support using array as parameter types, and they haven't changed this in newer versions of CFC.

Where do these 100 variables come from?
If they come from 100 IO-Data points (e.g. OP_AnL), then even if this are 100 variables, I would use blocks with single parameters, and to get single lines to every IO driver block. This is the advantage of CFC, that you can easily see where one value is coming from, and where it is used without using cross reference.

For 100 variables I would write a MAX FB block with 30 parameters or so, that one block call fits a single CFC sheet, and cascade them up to the number you need.

CFC should be a flow chart, you should see the data flow and the main function blocks, but not too much of the internal details.

If the 100 variables are kind of internal data (e.g. max value of the recent 100 saved values over last 100 seconds), I'd save the values in the stat variables of an FB.
 
I got the search for maximum value function working and seems to work ok for my purposes. Next problem I'm facing is a variable size block move from an FC's array to any. Any ideas? :) My goal is to temporarily save values to an array (during for loop) and then transfer the contents of the array into a DB right after the loop. I got it working with just block moving the array into any and then connecting the any variable to a DB. The problem is that the amount of data I'm processing is not constant and if the length of my target DB is, say 100 real type variables, and I only want to write 60 values the block move command inserts some ******** values to the leftover variables. Everything works fine though when the range of writing is equal to the size of the target DB.
 

Similar Topics

Hi, Anyone had to program something to find the nearest ? I have a list of the folowing X position values: 0 133 213 293 373 453 533 1093 1653...
Replies
9
Views
2,716
Hello nice to meet you, im new in here, I'm currently trying to convert code written in STL for a S7-400 to SCL for an S7-1500, because when i run...
Replies
5
Views
267
Hi everyone, I am new to this amazing world of PLC, well... I mean, in practice, since I already knew electronics, programming languages, IT, and...
Replies
7
Views
608
Hi all, This is my first post; I am new to PLC Controls stuff and English is not my native language, so I apologize in advance if something is...
Replies
4
Views
490
Hi! I am working on a project were we will be replacing an 300 CPU with a 1500 CPU and we are working on migrating / converting code. There is a...
Replies
9
Views
1,026
Back
Top Bottom