Determine number length in a file in Twincat 3

Viewed 140

I am reading a file on my computer that contains the following information:

cellcount=011 (INT)

currentdensity=1.112 (REAL)

REAL2=2.1145 (REAL)

INT1=41823 (INT)

REAL3=4.2023 (REAL)

INT=11 (INT)

Currently I am storing the ReadBuffer in a string(1000) because I thought that was the easiest way to manipulate the content. I want to be able to extract the numbers as you see and store them in variables. I want it to be dynamic so folks can enter any number (not reals in to ints, but otherwise).

so far I have looked at the string functions of twincat 3 and using MID() and FIND() I can make something work, but then I need to know the length of the numbers. Like this:

test.CellCount := STRING_TO_INT(MID(sTest,number_of_chars,FIND(sTest,'cellcount:')+10));

Any idea how to make this dynamic?

1 Answers

Square brackets after a string variable will allow you to extract the ASCII code of a particular character. Knowing that digits 0-9 are ASCII codes 48-57, you can iterate through the characters following your search string until no more digits are found. For example:

loc1 := FIND(sTest,'cellcount=') + 9;

FOR i:=loc1 TO (loc1+10) DO    // 10 = maximum length of number
  IF (sTest[i]>=48 AND sTest[i]<=57) OR sTest[i]=46 THEN
    loc2 := i;
  ELSE
    EXIT;
  END_IF
END_FOR

number_of_chars := loc2 - loc1 + 1;

ASCII code 46 is the decimal point, to allow parsing of floating point values.

Related