ARRAY finding specific value and its pocition /TwinCAT

2019-03-21
2019-11-26
  • Vijolica555 - 2019-03-21

    Hello,

    I have ARRAY [0..iReceivedData] OF BYTE where iReceivedData (UINT) is around 769 - depends on answer that is send by sensor. So i want to break up this array to more arrays. Is that possible? I want to break it where the value 32 is (that means space, so its space between the data parts). That is why i need to found value 32 in array and need to know its position. Anyone knows how to find that? so i will have aprox 30 arrays at the end and each represents specific information.

    Hope someone has an idea.
    Thank you.

     
  • dFx

    dFx - 2019-03-22

    to find "32", just use a for loop :

    NbMaxSubArrays := UPPER_BOUND(ptArray,0);
    // Clear pointer and length array
    FOR index:=0 TO NbMaxSubArrays DO
       ptArray[index]:= 0;
       LengthArray[index]:=0;
    END_FOR
    // Fill pointer array
    StartPosition := 0;
    EndPosition := 0;
    CurrentSubArray :=0;
    FOR index:=0 TO iReceivedData DO
       // Only when we find the separator
       IF ByteArrayBuffer[index] = 32 THEN
          EndPosition := index;
          ptArray[CurrentSubArray ]:= ADR(ByteArrayBuffer[StartPosition]); // Pointer to the first byte
          LengthArray[CurrentSubArray ]:=EndPosition - StartPosition + 1; // Length of the array (count of elements)
          CurrentSubArray := CurrentSubArray  + 1; // Use next record in sub array we are saving those records in
          StartPosition := index + 1; // Update start position
          // protect overflowing pointer buffer
          IF CurrentSubArray > NbMaxSubArrays THEN 
             EXIT;
          END_IF
       END_IF
    END_FOR
    

    Unless you have to copy your data to save it from a change, I would just define pointers to every sub array. This will save memory and computations.

     
  • josepmariarams - 2019-03-22

    You can substitute 32 by 0 in all array. Afteer tath you can:

    Str1 :pointer to string;

    If pos[index]=0 then str1:=adr(arr[index+1]) end_if

    Sent from my Moto G (5S) Plus using Tapatalk

     
  • Vijolica555 - 2019-03-22

    dFx hat geschrieben:
    to find "32", just use a for loop :

    NbMaxSubArrays := UPPER_BOUND(ptArray,0);
    // Clear pointer and length array
    FOR index:=0 TO NbMaxSubArrays DO
       ptArray[index]:= 0;
       LengthArray[index]:=0;
    END_FOR
    // Fill pointer array
    StartPosition := 0;
    EndPosition := 0;
    CurrentSubArray :=0;
    FOR index:=0 TO iReceivedData DO
       // Only when we find the separator
       IF ByteArrayBuffer[index] = 32 THEN
          EndPosition := index;
          ptArray[CurrentSubArray ]:= ADR(ByteArrayBuffer[StartPosition]); // Pointer to the first byte
          LengthArray[CurrentSubArray ]:=EndPosition - StartPosition + 1; // Length of the array (count of elements)
          CurrentSubArray := CurrentSubArray  + 1; // Use next record in sub array we are saving those records in
          StartPosition := index + 1; // Update start position
          // protect overflowing pointer buffer
          IF CurrentSubArray > NbMaxSubArrays THEN 
             EXIT;
          END_IF
       END_IF
    END_FOR
    

    Unless you have to copy your data to save it from a change, I would just define pointers to every sub array. This will save memory and computations.

    Hello, Thank you i will try that.
    I asumme that ptArrapt is pointer po my new array and ByteArrayBuffer is my data array?

     
  • Vijolica555 - 2019-03-22

    Josep M. Rams hat geschrieben:
    You can substitute 32 by 0 in all array. Afteer tath you can:
    Str1 :pointer to string;
    If pos[index]=0 then str1:=adr(arr[index+1]) end_if
    Sent from my Moto G (5S) Plus using Tapatalk

    Hello,

    Thank you.
    But i can not change 32 with 0 because i have also Nulls in my array that are important for me. but i can say pos[index] = 32, or? What is pos?

     
  • josepmariarams - 2019-03-22

    Pos would be your input array. I was thinking that your input array be an group of strings separated by number 32...

    Sorry

    Sent from my Moto G (5S) Plus using Tapatalk

     
  • dFx

    dFx - 2019-03-26

    Vijolica555 hat geschrieben:
    I asumme that ptArrapt is pointer po my new array and ByteArrayBuffer is my data array?

    You're assuming right.

     
  • JohnSorrell - 2019-11-26

    Can anyone help define the VARS for this program?
    I am a noob

     

Log in to post a comment.