S7 - 300 SCL string formatting issues

jt919

Member
Join Date
Mar 2012
Location
Kokomo
Posts
2
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 syntax, so that I can have global access to the DB and use the memory addressing (DB700.DBB10) or symbolic addressing to access the different bytes of data that contain my CHAR's.

Then use those address's in the CONCAT function to form one long string of CHAR's.

What I am trying to do.

VAR_TEMP
COMPARE_STRING : STRING[14];
END_VAR

COMPARE_STRING := CONCAT(IN1 := DB700.DBB10, IN2 :=DB700.DBB11, IN3 := DB700.DBB13);
 
Example code:

Code:
DATA_BLOCK DB86
struct
cDBChar1: CHAR := 'D';
cDBChar2: CHAR:= 'E';
END_STRUCT
Begin
END_DATA_BLOCK

FUNCTION_BLOCK FB88
VAR
sView:STRING[20];
end_var
VAR_TEMP
sCompare:STRING[20];
cChar1:CHAR;
cChar2:CHAR;
END_VAR

BEGIN
cChar1:='B';
cChar2:='C';
sCompare:=''; //initialise temp string
sCompare:=CONCAT(In1:=cChar1,In2:=cChar2,In3:=DB86.cDBChar1);
sView:=sCompare; //copy to stat var for monitoring under test
END_FUNCTION_BLOCK
 
What does the STRUCT command allow you to do? It looks like you have just declared static CHAR values is all. Also can you declare values in the declaration table as UDTs from the simatic manager?

The reason I need to access the CHARs from the DB is that this DB buffers 100 different 14 byte long CHAR strings, but it doesn't format them as a string it formats them as an individual CHARs. So I need to be able to pull the new values from the DB everytime I call my SCL FB since the CHAR data in the DB is dynamicaly updating.
 
Not sure what it is you have to achieve exactly.

One method you could possibly use is to use different "views" for the same data.
In SCL you can use "AT" to look at the same data as for example a STRING but also a series of CHARs. This so you dont have to use CONCAT or other code to try and splice many CHARs togethe as a STRING.

A STRING[14], consist of 2 initial BYTE values (max length of string (betw. 0..255) and actual length of string (0..255)) followed by a number of bytes, one for each character in the string.

Here is an example of an FB programmed with SCL accessing the same data as both individual characters and as a string:

Code:
FUNCTION_BLOCK "FB_CHAR_TO_STRING"

VAR_INPUT
    input_max_chars : BYTE ;    
    input_act_chars : BYTE ;    
    input_chars : ARRAY[0..13] OF CHAR ;
END_VAR 

VAR_OUTPUT 
    output_string : STRING[14] ;
END_VAR 

VAR
    char_struct : STRUCT
       max_chars :  BYTE ;
       act_chars :  BYTE ;
       chars :      ARRAY[0..13] OF CHAR ;
    END_STRUCT ;
    outstr AT char_struct : STRING[14] ; 
END_VAR

char_struct.max_chars := input_max_chars ;
char_struct.act_chars := input_act_chars ;
char_struct.chars := input_chars ;

output_string := outstr ;

END_FUNCTION_BLOCK

Warning, not tested, and you should add error handling code, such as when an impossible string length is input.
 
Last edited:
Just changed the code slightly to take into account that the STRING length is fixed 14 characters. So no need to input that in the calling block.

Code:
FUNCTION_BLOCK "FB_CHAR_TO_STRING"

VAR_INPUT
    input_act_chars : BYTE ;    
    input_chars : ARRAY[0..13] OF CHAR ;
END_VAR 

VAR_OUTPUT 
    output_string : STRING[14] ;
END_VAR 

VAR
       char_struct : STRUCT
       max_chars :  BYTE ;
       act_chars :  BYTE ;
       chars :      ARRAY[0..13] OF CHAR ;
    END_STRUCT ;
    outstr AT char_struct : STRING[14] ; 
END_VAR

char_struct.max_chars := B#16#E ; // Ehex =14dec
char_struct.act_chars := input_act_chars ;
char_struct.chars := input_chars ;

output_string := outstr ;

END_FUNCTION_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
Dear Sir, I try to write a function use SCL to read the string from a DB and convert it to real and transfer to another DB. But the simple...
Replies
34
Views
24,998
How to realize transformation functions : 1/s and 1/(1+s) into SCL inside S7-300
Replies
0
Views
944
Hi. I tryid to make a shiftregister with 31 entry's. the string array are declared in the Static area of the FB. When I run the code its not...
Replies
1
Views
1,280
Looking for some help with an array of strings in SCL. I have an array of 99 string[98] I also have an array of 99 INT My first 4 chars of the...
Replies
9
Views
2,448
Back
Top Bottom