I am library developer & new to CodeSys OOPs programming. I have a problem where it is required to define variable size arrays & varaible size structure(DUT) objects. As i will get the information of size at runtime only.
I want to create something like below.
I know the below code doesnt work, but i am just trying to explain what i want to do.
TYPE DUT :
STRUCT
abyReadBuffer : ARRAY[*] OF BYTE;// I dont know the size of this array. It could be maximum of 64bytes but i get this information at runtime only.
usiVar0 : USINT;
uiVar1 : UINT;
END_STRUCT
END_TYPE
FUNCTION or FUNCTIONBLOCK (in library)
VAR
structure : ARRAY[*] OF DUT;// I dont know the size of this array. I get this information at runtime only.
END_VAR
Also the problem with above approach is i dont get the SIZE information from user, but from a status word of another FB. So i dont want any input parameters which the user needs to pass regarding size. Also the size parameter is going to change in probably every cycle/instance.
Do i need to use Dynamic Memory Allocation for above?? If yes could anyone show me some example of it??
π
1
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
However i made a small mistake while posting the question.
1) VAR_IN_OUT is not permissible in DUT objects. I can somehow make it fix as it wont be greater than 64bytes.
2) Secondly, i dont want to use VAR_IN_OUT as it is mandatory for the user to pass a fixed length array as input parameter, which is not possible for the user as he doesnt have that information.
I hope now i make it more clear....
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Common approach may be to size your array to the maximum size (if size is not a problem, ie on small arrays), then to use a key value to detect array end (ie nullbyte 16#00).
This way you can fill your dut array with values and optionnaly indicate the end of dut array.
This approach is valid only with small arrays, as reserved memory may be an issue.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
For instance, your array max length is 10.
If you only fill it with 5 items, then reset lefts items (5..9) and set your length variable to 5.
Edit : This is how string are handled (a byte array with a max length as defined in the variable definition and then the actual length is set by the first null byte)
Last edit: dFx 2021-01-07
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I am library developer & new to CodeSys OOPs programming. I have a problem where it is required to define variable size arrays & varaible size structure(DUT) objects. As i will get the information of size at runtime only.
I want to create something like below.
I know the below code doesnt work, but i am just trying to explain what i want to do.
TYPE DUT :
STRUCT
abyReadBuffer : ARRAY[*] OF BYTE;// I dont know the size of this array. It could be maximum of 64bytes but i get this information at runtime only.
usiVar0 : USINT;
uiVar1 : UINT;
END_STRUCT
END_TYPE
FUNCTION or FUNCTIONBLOCK (in library)
VAR
structure : ARRAY[*] OF DUT;// I dont know the size of this array. I get this information at runtime only.
END_VAR
Also the problem with above approach is i dont get the SIZE information from user, but from a status word of another FB. So i dont want any input parameters which the user needs to pass regarding size. Also the size parameter is going to change in probably every cycle/instance.
Do i need to use Dynamic Memory Allocation for above?? If yes could anyone show me some example of it??
more posts ...
Size isn't needed, it's implicitly given by the ARRAY via the Lowerbound and Upperbound function call.
See help: https://help.codesys.com/webapp/_cds_datatype_array;product=codesys;version=3.5.16.0#array-of-variable-length
Last edit: hermsen 2020-12-24
Hello h-hermsen
Thank you for the reply.
However i made a small mistake while posting the question.
1) VAR_IN_OUT is not permissible in DUT objects. I can somehow make it fix as it wont be greater than 64bytes.
2) Secondly, i dont want to use VAR_IN_OUT as it is mandatory for the user to pass a fixed length array as input parameter, which is not possible for the user as he doesnt have that information.
I hope now i make it more clear....
You might need dynamic Function Blocks, Option B.) from:
https://help.codesys.com/webapp/fb_factory;product=LibDevSummary;version=3.5.15.0
The examples are in the standard CODESYS Library Template.
Common approach may be to size your array to the maximum size (if size is not a problem, ie on small arrays), then to use a key value to detect array end (ie nullbyte 16#00).
This way you can fill your dut array with values and optionnaly indicate the end of dut array.
This approach is valid only with small arrays, as reserved memory may be an issue.
Hello dFx,
Firstly, in the actual data i may get null values also. I need to find some other key value maybe.
I didnt understand you properly. How can i handle my array of DUT??
Could you please elaborate a little?
Then just use an external length variable.
For instance, your array max length is 10.
If you only fill it with 5 items, then reset lefts items (5..9) and set your length variable to 5.
Edit : This is how string are handled (a byte array with a max length as defined in the variable definition and then the actual length is set by the first null byte)
Last edit: dFx 2021-01-07