Step7 Comparing an Array of characters to a lookup table

rpop91

Member
Join Date
Mar 2012
Location
Detroit, Mi
Posts
44
Hey Guys..

Here's my problem. I have an array of 9 characters, a "part number". I need to compare this array to another part number that is predefined in a DB. However, I have over 60 part numbers.

The way I do this now is do a CompareBytesToBytes function to compare 9 bytes at a time...60 different times. This is very heavy on the memory.

What I want to know, is there a way to take my array and compare it to all other arrays in a "lookup table" all at once and spit out whether it matches, and if it does, spit out either the part number it matches or a bool array i.e. Part_Number[12].

I've though about maybe writing this in SCL as my STL skill isn't very high.

Thanks for the help!
 
I would convert the part number to a INT. I would use the same algorithm to convert the part numbers in the DB to an INT. Then I would simply compare INTs.

I am writing a ST compiler. I had to solve that same problem myself.
I use a CRC32 algorithm to convert the symbol name to a 32bit number that I can compare and use as an index instead of doing string compares. Is there a CRC16 algorithm that you can use to convert the string to a number? There are simpler methods but you may get "collisions" where two strings have the same part number.
 
This is what I have currently that I'm working on. Is there a way instead of doing 64 IF statements to check against 64 different part numbers?

Code:
VAR
    PartNumber : ARRAY[1..64] OF STRING[9];
    cmpPartNumber : STRING[9];
END_VAR
VAR_INPUT
    Part_Number : STRING[9]; //Input the part number from the DB
END_VAR
VAR_OUTPUT
    Part_Number_Valid : BOOL; //A simple bool that will go high if a part number is found valid
    Part_Number_Found : ARRAY[1..64] OF BOOL; //This will output which part number is found
END_VAR
 
BEGIN
cmpPartNumber := Part_Number;
PartNumber[1] := 'XXXXXXXXX';
IF cmpPartNumber = PartNumber[1] THEN Part_Number_Valid:=TRUE; Part_Number_Found[1]:=TRUE;
ELSE Part_Number_Found[1]:=FALSE;
END_IF;
 
Don't have much time to post right now, but I think you are looking for a "FOR LOOP"


Code:
 FOR i := 1 TO 64 DO        
            IF (BadgeList[i].Number = BadgeNumber ) THEN
              SYS_SearchList := i;  
              Level := BadgeList[i].Level;      
              EXIT;
            END_IF;    
           
        END_FOR;
 
JOLTRON:

I think you are correct sir....is there any way you can go into more detail when you have some time?

Any help would be greatly appreciated.
 
Here is a starting point, see what you can get with this. SCL is not my strongest point so maybe someone will chime in with a better way of doing it.

PS. I didn't test / compile code, it was written in notepad.


Code:
VAR
    PartNumber : ARRAY[1..64] OF STRING[9];
    cmpPartNumber : STRING[9];
    i : INT;
END_VAR
VAR_INPUT
    Part_Number : STRING[9]; //Input the part number from the DB
END_VAR
VAR_OUTPUT
    Part_Number_Valid : BOOL; //A simple bool that will go high if a part number is found valid
    Part_Number_Found : INT; 
END_VAR
 
BEGIN
//Initialize Data
         //Assuming you have all 64 part numbers hard coded here:
         PartNumber[1] := 'XXXXXXXXX';
         PartNumber[2] := 'XXXXXXXXy';
         //Till PartNumber[64]
 
         // -1 is not found, otherwise is set to location part number was found at. I
         PartNumberFound:=-1;
 
         // Seutp PartNumber to be searched for
         cmpPartNumber := Part_Number;
 
//FOR LOOP = Search all 64 part numbers.  If a match is found turn on 
//Part_Number_Valid and set the location it was found at.  
//This will then EXIT the FOR LOOP since there is no longer a need to search.  If not found it will return a -1
         FOR i := 1 TO 64 DO 
               IF ( cmpPartNumber = PartNumber[i] ) THEN
                 Part_Number_Valid:=TRUE; 
                 Part_Number_Found:= i;
                 EXIT; //Exit the FOR LOOP
                END_IF;               
        END_FOR:
 
END_FUNCTION

ADDED:
http://cache.automation.siemens.com...80_FAQ/S7_SCL_String_Parameterzuweisung_e.pdf
A link to information on SCL and working with strings, may have some good info
 
Last edited:
Example coding below. You do not need to initialise the part numbers programatically, you can initialise them as part of the array declaration, I've shown the first four in my example.

Code:
FUNCTION_BLOCK FB888
VAR
//initialise part numbers    
    PartNumber : ARRAY[1..64] OF STRING[9]:=
    'PartNo001',
    'PartNo002',
    'PartNo003',
    'PartNo004';
    iIndex:INT;
END_VAR
VAR_INPUT
    Part_Number : STRING[9]; //Input the part number from the DB
END_VAR
VAR_OUTPUT
    Part_Number_Valid : BOOL; //A simple bool that will go high if a part number is found valid
    Part_Number_Found : ARRAY[1..64] OF BOOL; //This will output which part number is found
END_VAR
 
BEGIN
Part_Number_Valid:=FALSE;
FOR iIndex:=1 TO 64 DO
 Part_Number_Found[iIndex]:=(Part_Number = PartNumber[iIndex]);
 IF  Part_Number_Found[iIndex] THEN Part_Number_Valid:=TRUE;END_IF;
END_FOR;
END_FUNCTION_BLOCK
 
Thanks for all the help guys. This is what ended up working for me.

Code:
FOR i := 1 TO 64 DO
               IF cmpPartNumber1 = PartNumberList[i] THEN
                 Part_Number_Valid:=TRUE; 
                 Part_Number_Found[i] := TRUE;
                 EXIT; //Exit the FOR LOOP
                 ELSE Part_Number_Valid:=FALSE; Part_Number_Found[i] := FALSE;
                            
                END_IF;               
        END_FOR;
 

Similar Topics

This is the first time I am working with Simatic Manager Step7 as I started my siemens journey with TIA which is pretty easy and do a lot of stuff...
Replies
3
Views
161
When you download a DB, the values get overwritten by what is in the "actual" column in offline DB. Does this happen at the start of the PLC...
Replies
6
Views
156
Hello Inside a FB, I´m trying to transfer a string from a DB to a IN_OUT var that was define as a UDT. The problem is that i can´t determine the...
Replies
4
Views
146
Hi all, I am trying to convert RSLogix 5000 program to Step7. I need to bit shift left my array of double integers for tracking the product on...
Replies
2
Views
528
I have a word in some DB which I want to load to AR1 and use as a pointer. In order to do this I need to write L DBxy.DBW xy SLD 3 LAR1 I...
Replies
3
Views
548
Back
Top Bottom