PROGRAMPLC_PRGVARÂ Â parameters:ARRAY[1..4]OFINT;Â Â splitString:BOOL;Â Â fileString:STRING:='123$R$N492$R$N500$R$N6091$R$N';Â Â arINT1:INT;Â Â first:STRING;Â Â fileStringDel:STRING;Â Â index:INT:=1;END_VAR
IFsplitStringTHEN
  FORindex:=1TO4DO
    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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Try looking at StringUtils library. You can do something like this to have way more than 255 characters:
PROGRAMParameterExtractorVAR
  sLongString : STRING(1024) :='01234567890$R$N1234567890$R$N2345678901$R$N3456789012$R$N4567890123$R$N5678901234$R$N6789012345';
  sShortString : STRING;
  aParameters : ARRAY[0..MAXPARAMETERS] OFULINT;
  sSplitChars : STRING :='$R$N';
  iSplitLocation : INT;
  iCount : INT :=0;END_VARVARCONSTANT
  MAXPARAMETERS : INT :=9;END_VAR//Loopthroughstringuntilthereisnostringleftorarrayhashititslimits
  WHILEStrLenA(pstData:=ADR(sLongString))>0DO
    //Findlocationoffirstbreakpoint
    iSplitLocation :=StrFindA(pst1:=ADR(sLongString), pst2:=ADR(sSplitChars), uiSearchStart:=1)-1;
   Â
    //Ifthereisnobreakpointfoundthensetlocationtosizeofstring
    IFiSplitLocation=-1THEN
      iSplitLocation :=DINT_TO_INT(StrLenA(pstData:=ADR(sLongString)));
    END_IF
   Â
    //Copythepartofthestringthatisneededfortheparameter.
    StrMidA(
        pst:=ADR(sLongString),
        uiInputBufferSize:=SIZEOF(sLongString),
        iLength:=iSplitLocation,
        iPosition:=1,
        pstResult:=ADR(sShortString),
        uiResultBufferSize:=SIZEOF(sShortString));
    //Makesurestringisnotemptybeforeusingupanarrayspot.
    IFStrLenA(pstData:=ADR(sShortString))>0THEN
      aParameters[iCount] :=STRING_TO_ULINT(sShortString);
      iCount :=iCount+1;
    END_IF
    //Removecopiedcomponentsfromlongstring
    StrDeleteA(pby:=ADR(sLongString), iLength:=(iSplitLocation+2), iPosition:=1);
    //Checktoseeifmoreparameterscanfitinarray. Ifnotexitloop
    IFiCount>MAXPARAMETERSTHEN
      iCount :=0;
      EXIT;
    END_IF
  END_WHILE
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
split your string using the special chars ( Cr Lf ) into a string array.
Then cast each string item to the desired datatype (int, real …)
The following code worked for me:
Unfortunately the maximum editable string size is 255 characters so not many parameters can be saved into one string.
Try looking at StringUtils library. You can do something like this to have way more than 255 characters:
@Comingback4u
You code looks much better than mine.
So by using this library it's possible to manipulate strings longer than 255 characters?
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.