Read string from DB to FB

SeH

Member
Join Date
Jun 2011
Location
Göteborg
Posts
9
I'm having a problem again. This time I'm trying to read a string from a DB and compare it to a string given by an input variable in the FB. The problem seems to be when reading the string from the DB. It should be a string containing '0', '1' or '2' but in the FB it is a dword containing F013100. Here is some of the code:

Code:
FUNCTION_BLOCK  Recept_hantering

    // *** Variable declarations ***
    
    //Temporary variables
    VAR_TEMP
        loopnumber : INT;
        i : INT;
        temp_article_nr : STRING[15];
    END_VAR
    
    //Static variables
    VAR
        area_start : INT;
        editable_start : INT;
        active_start : INT := 0;
    END_VAR
    
    //Input parameters
    VAR_INPUT
        load : BOOL;
        save : BOOL;
        activate : BOOL;
        recipe : BLOCK_DB;
        article_or_position : BOOL;
        sum_articles : INT;
        article_datalength : INT;
        article_nr : STRING[15];
    END_VAR
    
    // *** Program start ***
    BEGIN
    
    loopnumber := sum_articles;
    editable_start := article_datalength;
    temp_article_nr := '';
    temp_article_nr := article_nr;
        
        area_start := article_datalength;
        i := 0;
        
        //Check for model with matching article number
        REPEAT
            i := i + 1;
        UNTIL temp_article_nr = DINT_TO_STRING(DWORD_TO_DINT(recipe.DD[area_start + article_datalength * i])) OR i > (loopnumber - 1) END_REPEAT;    
    
END_FUNCTION_BLOCK
 
The string is stored in the following format:

Max string length, actual string lenth, char 1....etc,

This ties up with your data: 0f, 01, 31, 00 (Note that '1' is 31hex). You have to copy the bytes from the DB to the destination string, not convert the dword to a dint then convert the dint to a string.
 
Example code (compiled but not tested - supply the DB and the call to the block next time. Create a library and copy the blocks the library, archive the library and then post as an attachment.)

Code:
FUNCTION_BLOCK  FB998
    // *** Variable declarations ***
    
    //Temporary variables
    VAR_TEMP
        loopnumber : INT;
        i,j : INT;
        temp_article_nr : STRING[15];
        sFromDB:STRING[15];
        byFromDB AT sFromDB: ARRAY[1..17] OF BYTE;
    END_VAR
    
    //Static variables
    VAR
        area_start : INT;
        editable_start : INT;
        active_start : INT := 0;
    END_VAR
    
    //Input parameters
    VAR_INPUT
        load : BOOL;
        save : BOOL;
        activate : BOOL;
        recipe : BLOCK_DB;
        article_or_position : BOOL;
        sum_articles : INT;
        article_datalength : INT;
        article_nr : STRING[15];
    END_VAR
    
    // *** Program start ***
    BEGIN
    
    loopnumber := sum_articles;
    editable_start := article_datalength;
    temp_article_nr := '';
    temp_article_nr := article_nr;
        
        area_start := article_datalength;
        i := 0;
        
        //Check for model with matching article number
        REPEAT
            i := i + 1;
            //copy string from db a byte at a time into temp string variable
            FOR j:=0 TO 16 DO
             byFromDB[j]:=recipe.DB[area_start + (article_datalength *i) + j];   
            END_FOR;    
        UNTIL temp_article_nr = sFromDB OR i > (loopnumber - 1) END_REPEAT;    
    
END_FUNCTION_BLOCK
 
Now it seems a bit more promising but it still doesn't work. I've added the DB and the block call and also the code in an attachment (I hope it works). When I debug the program I find that byFromDB[0] = 0F , byFromDB[1] = 01 , byFromDB[2] = 31 and the rest is 0. So far so good. But it seems like sFromDB is 0 or empty. Do you have any idea why?

Another question: is it in any way possible to see the value of a string online?
 
My coding error. Array should be [0..16] and not [1..17]

Code:
        byFromDB AT sFromDB: ARRAY[0..16] OF BYTE;
 

Similar Topics

Hello, So i managed to read the string coming from control logix and put a string display in PanelView 800. Now I am struggling to do the other...
Replies
1
Views
135
Good day. The problem is the following, I am using the Machine shop parker program to program an HMI that communicates with a logix5573 PLC. I can...
Replies
0
Views
1,256
Good day. The problem is the following, I am using the Machine shop parker program to program an HMI that communicates with a logix5573 PLC. I can...
Replies
1
Views
1,392
Hi, I'm pretty new to FactoryTalk and PLC's in general. In View SE, is it possible to read a string contained in a UDT and use it in another tag...
Replies
1
Views
1,226
Back
Top Bottom