Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

is it possible to change the size of an array in an TwinCAT-PLC using ADS, in this case pyads?

VAR CONSTANT
    min_a   : INT := 1;
    max_a   : INT := 234;
END_VAR
VAR
    array_1: ARRAY[min_a..max_a] OF INT;
END_VAR

And then i wanted to change the value of the constants with ads, which works, but it never changes the size of the array in the plc.

Can somebody help me?

It's the first time that i work with an plc and that i write code in a structured text...

question from:https://stackoverflow.com/questions/65934043/change-size-of-array-in-plc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
259 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can allocate arrays of specific type and size with the __NEW(type, size) method and then free the memory with __DELETE(pointer) method as in the code below:

METHOD myCode
    VAR_INPUT
        myArray : POINTER TO INT;
    END_VAR

    myArray := __NEW(INT, 10); // Create array of type INT with size of 10 
    __DELETE(myArray); //Free the memory
    myArray := __NEW(INT, 20); // Allocate new memory now with the size of 20
    __DELETE(myArray); //Free the memory

END_METHOD
  • Be careful with this because you need to free the memory with the __DELETE(pointer) method!
  • Note that you can't change the size of array if you declare them statically like in your answer.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...