Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

ads - Change Size of Array in PLC

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

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

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...