CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Sun Oct 20, 2019 9:29 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Jul 23, 2019 12:44 pm 
Offline

Joined: Wed Aug 05, 2015 1:26 pm
Posts: 23
I read a text file with saved parameters which has the following structure:

123
492
500
6091
etc

The resulting string looks like the following:
'123$R$N492$R$N500$R$N6091'

How can I extract all the numbers without the special characters from the string and assing them to an INT array?


Top
   
PostPosted: Tue Jul 23, 2019 2:01 pm 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 159
split your string using the special chars ( Cr Lf ) into a string array.
Then cast each string item to the desired datatype (int, real …)


Top
   
PostPosted: Tue Jul 23, 2019 9:48 pm 
Offline

Joined: Wed Aug 05, 2015 1:26 pm
Posts: 23
The following code worked for me:

Code:
PROGRAM PLC_PRG
VAR
   parameters: ARRAY[1..4] OF INT;
   splitString : BOOL;
   fileString : STRING := '123$R$N492$R$N500$R$N6091$R$N';
   arINT1 : INT;
   first : STRING;
   fileStringDel : STRING;
   index : INT := 1;
END_VAR


Code:
IF splitString THEN
   FOR index:=1 TO 4 DO
      arINT1 := FIND(fileString,'$R$N');
      first := LEFT(fileString,arINT1-1);
      fileString := DELETE(fileString,arINT1+1,1);
      parameters[index]:=STRING_TO_INT(first);
   END_FOR
   splitString := FALSE;
   index := 0;   
END_IF


Unfortunately the maximum editable string size is 255 characters so not many parameters can be saved into one string.


Top
   
PostPosted: Wed Jul 24, 2019 1:33 am 
Offline
Frequent User
Frequent User

Joined: Tue Dec 03, 2013 11:52 pm
Posts: 120
Try looking at StringUtils library. You can do something like this to have way more than 255 characters:
Code:
PROGRAM ParameterExtractor
VAR
    sLongString : STRING(1024) := '01234567890$R$N1234567890$R$N2345678901$R$N3456789012$R$N4567890123$R$N5678901234$R$N6789012345';
    sShortString : STRING;
    aParameters : ARRAY[0..MAXPARAMETERS] OF ULINT;
    sSplitChars : STRING := '$R$N';
    iSplitLocation : INT;
    iCount : INT := 0;
END_VAR
VAR CONSTANT
    MAXPARAMETERS : INT := 9;
END_VAR

//Loop through string until there is no string left or array has hit its limits
    WHILE StrLenA(pstData:= ADR(sLongString)) > 0 DO
        //Find location of first break point
        iSplitLocation := StrFindA(pst1:= ADR(sLongString), pst2:= ADR(sSplitChars), uiSearchStart:= 1) - 1;
       
        //If there is no break point found then set location to size of string
        IF iSplitLocation = -1 THEN
            iSplitLocation := DINT_TO_INT(StrLenA(pstData:= ADR(sLongString)));
        END_IF
      
        //Copy the part of the string that is needed for the parameter.
        StrMidA(
                pst:= ADR(sLongString),
                uiInputBufferSize:= SIZEOF(sLongString),
                iLength:= iSplitLocation,
                iPosition:= 1,
                pstResult:= ADR(sShortString),
                uiResultBufferSize:= SIZEOF(sShortString));

        //Make sure string is not empty before using up an array spot.
        IF StrLenA(pstData:= ADR(sShortString)) > 0 THEN
            aParameters[iCount] := STRING_TO_ULINT(sShortString);
            iCount := iCount + 1;
        END_IF

        //Remove copied components from long string
        StrDeleteA(pby:= ADR(sLongString), iLength:= (iSplitLocation + 2), iPosition:= 1);

        //Check to see if more parameters can fit in array. If not exit loop
        IF iCount > MAXPARAMETERS THEN
            iCount := 0;
            EXIT;
        END_IF
    END_WHILE


Top
   
PostPosted: Thu Jul 25, 2019 7:14 pm 
Offline

Joined: Wed Aug 05, 2015 1:26 pm
Posts: 23
@Comingback4u

You code looks much better than mine.
So by using this library it's possible to manipulate strings longer than 255 characters?


Top
   
PostPosted: Fri Jul 26, 2019 1:03 am 
Offline
Frequent User
Frequent User

Joined: Tue Dec 03, 2013 11:52 pm
Posts: 120
Yes, In the above example I define the size of the string by declaring "sLongString : STRING(1024) := ..." so we are at 4x the standard size of STRING. With the StringUtil library you specify the size of the buffer using the SIZEOF(sLongString) so you aren't limited to the 255. You can go much higher than 1024 but this was just for example.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited