String-SCL; I need help

Sir Xico

Member
Join Date
Jun 2010
Location
spain
Posts
2
Hey guys,

Before of all, congratulation for this wonderful forum. The comunity have help me a lot in the last months so I have decided to post my problem hoping your help.

I have a string in a DB under "BufferR: ARRAY [0..41] OF BYTE"; I dont know his lenght (the string is changing continuosly in the buss) but I know that the ends in "END".

The problems are: How I can copy this string as string (no as byte) in my OB1? and How I can delete all characters after "E" of "END"?

Thanks

Note: I'm programming in SCL.
 
Hi sir xico.

I guess that all the characters in the array are to be used. It is not that the first byte is the full length ad the second byte is the actual length.
If so, I think this will do it:

Code:
FUNCTION FC_string_capture : VOID
 
VAR_INPUT
    char_array : ARRAY[1..42] OF CHAR ;
END_VAR
 
VAR_OUTPUT
    out_string : STRING[42] ;
END_VAR
 
VAR_TEMP
    i : INT ;
    temp_string : STRING[42] ;
    temp_string_elements AT temp_string : STRUCT
        set_length : BYTE ;
        act_length : BYTE ;
        chars : ARRAY[1..42] OF CHAR ;
    END_STRUCT ;
END_VAR
 
// copy all the chars from the source
temp_string_elements.chars := char_array ; 
 
// setting the actual length of the string to 3 less than the full length will ignore the last three chracters "END".
    FOR i:=1 TO 39 BY 1 DO
        IF char_array[i] = 'E' AND char_array[i+1] = 'N' AND char_array[i+2] = 'D' THEN
                temp_string_elements.act_length := INT_TO_BYTE(i - 1) ; 
        END_IF ;
    END_FOR;
 
out_string := temp_string ;    
    
END_FUNCTION
It is not tested.
You may have to tweak it.
You should add some error checking code as well.
 
Hello,

Thanks for replying so soon. I don't know if the code will have bugs but I didn't have any idea of what i had to do and at least you have given me a pretty good idea of how I can do it. No doubt that it will be very useful.:p

Thank you
 
This is a bit better:

Code:
FUNCTION FC_string_capture : VOID
 
VAR_INPUT
    char_array : ARRAY[1..42] OF CHAR ;
END_VAR
 
VAR_OUTPUT
    out_string : STRING[42] ;
END_VAR
 
VAR_TEMP
    END_found : BOOL ;
    i : INT ;
    temp_string : STRING[42] ;
    temp_string_elements AT temp_string : STRUCT
        set_length : BYTE ;
        act_length : BYTE ;
        chars : ARRAY[1..42] OF CHAR ;
    END_STRUCT ;
END_VAR
 
// copy all the chars from the source
temp_string_elements.chars := char_array ; 
 
// setting the actual length of the string to 3 less than the full length will ignore the last three chracters "END".
    END_found := FALSE ;
    FOR i:=1 TO 40 BY 1 DO
        IF NOT END_found AND char_array[i] = 'E' AND char_array[i+1] = 'N' AND char_array[i+2] = 'D' THEN
                temp_string_elements.act_length := INT_TO_BYTE(i - 1) ; 
                END_found := TRUE ;
        END_IF ;
    END_FOR;
 
IF END_found THEN
    out_string := temp_string ;    
ELSE
    out_string := 'no END' ;
END_IF ;
    
END_FUNCTION
 
I was hoping that it doesnt make any difference to him.
If the input absolutely must be an ARRAY of BYTEs then another AT can fix the problem:

Code:
VAR_INPUT
     byte_array : ARRAY[1..42] OF BYTE ;
     char_array AT byte_array : ARRAY[1..42] OF CHAR ;
END_VAR
 
Last edited:
Here's another implementation that uses the SCL string processing functions FIND and DELETE:

Code:
DATA_BLOCK DB99
    STRUCT
    BufferR:ARRAY[0..49] OF BYTE;
    END_STRUCT
BEGIN
END_DATA_BLOCK

ORGANIZATION_BLOCK OB1
VAR_TEMP
    info : ARRAY[0..19] OF BYTE;
    pos:INT;
    sLocal : STRING[48];
    sBufferR : STRING[48];
    acBuffer AT sBufferR:ARRAY[0..49] of BYTE;
    acLocal AT sLocal:ARRAY[0..49] of BYTE;
    iSFC20Return : INT ;
END_VAR
iSFC20Return:=BLKMOV(SRCBLK := DB99.BufferR, DSTBLK :=  acBuffer);
pos:=FIND(IN1:=sBufferR, IN2:='END');
sLocal:='';
sLocal:=DELETE (IN:= sBufferR, L:= 2, P:= POS+1);
END_ORGANIZATION_BLOCK
 

Similar Topics

I feel like I'm going crazy, new to Siemens, coming from AB, and I cannot get a dang string copied in SCL. This is a S7-300, V16 PLC. I have a...
Replies
10
Views
3,501
I am trying to take several individual CHAR bytes and turn them into a string. The CHAR bytes are located in a DB. I am trying to figure out the...
Replies
4
Views
3,502
Hello everyone, I have a question. I'm using SCL code to create a function block. With that function block I want to get from a DB 3 strings...
Replies
10
Views
3,152
Hi I am playing around with SCL & Strings, what is the problem with the string assignment VAR_TEMP MyString : STRING[20] ; END_VAR MyString...
Replies
5
Views
3,056
Hello all, my task is to send an array of ASCII codes through VIPA CP341. The device which I want to connect uses its own framing like this...
Replies
5
Views
8,112
Back
Top Bottom