Im looking for a way to define predefined version of the same structures through "extends"/inheritance. What I want to do is best shown with an example:
The system will not allow me to "re-define" "val_type" however. Instead I must do the work-around of defining four different types with all fields individually defined.
The benefit if I could extend the base item would be that Im (more) sure the structures are identical. In usage I can call in a specific type rather use the base type and assign which type. Usage by pointer/ref to log_data_base would also be easier..
is not eq to (the string_value will not be assigned): string_item_2 : log_data_string := (string_value := 'test');
Because effectively for the compiler this is eq. to a "double" assignment where the compiler sees only the first assignment (my guess). string_item_2 : log_data_base := (val_type := STRING_) := (string_value := 'test');
Im not sure this should be treated as a bug, but it would surely be nice to be able to use this construction (compiler is 3.5.20.40).
Anyone see any other alternative for solution (other than 4 complete STRUCTS with all fields individually defined)?
Last edit: pernockham 2025-03-07
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Interesting, thanks! Didnt know about ANY before, and will sure find a use-case for it. Im not sure it will help me here though, it seems to me that an ANY will not accept a string assignment?
I actually think that the "ALIAS" should work, that was a hard found error. I had a fair bit of scratching my head looking for misdirected pointers before I understood what was (not) happening. I will use the work-around of individual structs instead.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Strangely reminds me to my struggles... Want to do something "Elegant", reusable, universal, practical... In CODESYS??? π
First of all, before you get too deep into this:
If you could find a way, to make a "universal" log entry object, containing the variable length data itself, you wouldn't be able to store them in an array, or access them like an array, or pass them by value as a type. (please correct me, if I'm wrong, incorrect, or not precise).
Because... Basically you can't declare a type with variable memory footprint. This is a very deeply embedded characteristic of CODESYS, and all IEC 61131-3 systems, and it has many reasons behind. And yes, it is a very common trap / mistake, to forget about.
So, with a log entry - I guess - it's pretty much the purpose: store data and metadata together, and then handle it in a uniform way. There are ways to handle this, really depends on what is the purpose. For example:
1. Entries with fixed length (Maybe it is not as evil as it looks for the first time. Depends on the situation, but definitely the fastest and easiest code)
You can have your base object, with an internal, fixed length string or byte array variable. I would go with a string, and call it _Data.; And then you can make properties, like
As_Bool, As_Int, As_Real...
In the 'set' accessors, you can do like:
pReal:=ADR(_Data);//POINTERTOREALAs_Real:=pReal^;
In the 'get' accessors, evidently:
pReal:=ADR(_Data);//POINTERTOREALpReal^:=AS_Real;
Or, can use ANY type, if you are not obsessed with variable / property like access:
2. Fixed length, but nicer
First, some disadvantage to any values:
- You can only assign values with write access. No literals, constants, etc...
- Can only be used as input variable of function or function_block
- Therefore, stg you could reach:
LogEntry.Initialize (stVariable|rVariable|iVariable|xVariable);
Just a quick example (it's funny to play with ANY):
Be careful it was not tested. I'm sure can be done better, please feel free to comment
FUNCTION_BLOCKFB_LogEntryVAR_INPUTMsgClass:UDINT;// Like DEBUG, WARN, ERR...MsgCode:UDINT;// Like Errors.ERR_FAILEDMsgTS:DT;// The timestampEND_VARVAR_Data:STRING(80);// Our data container..._Descr:__SYSTEM.AnyType;// A standard descriptor for our data, containing TYPE_CLASS, address and sizeEND_VAR
Be careful it was not tested. I'm sure can be done better, please feel free to comment
3. If you really want to do entries with variable size
In a standard environment, it would be similar to the previous, except you dont have the container variable _Data, just use a pointer, practically _Descr.pValue
At Initialize (SET_Value), you have to allocate the memory, would be easy with SysMem.SysMemAlloc - nowadays with SysMem.SysMemAllocData -, and you make sure to release it after use with SysMem.SysMemFreeData...
SysMemAlloc was already hidden. The problem with this, that sooner or later your application will totally fragment the dynamic memory, and fail...
So should look for some form of dynMaybe MemUtils.MemoryManager (I am not sure what is the status and the future of it).
Using a UNION instead of a STRUCT alone should give you that flexibility and only occupy memory once.
And if you want to be really elegant, you could use an FB, which only holds PUBLIC VAR.
Because a POU can use "EXTENDS"/inheritance. In CODESYS V3, an FB has a lot in common with C++ classes.
Using ANY or ALIAS is rarely a good idea. It neither won't be pretty, nor have good readability
and requires you to create a lot of error-prone things around it.
Last edit: Hismoon 2025-03-14
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Im looking for a way to define predefined version of the same structures through "extends"/inheritance. What I want to do is best shown with an example:
etc. for LOG_DATA_REAL and ..STRING.
The system will not allow me to "re-define" "val_type" however. Instead I must do the work-around of defining four different types with all fields individually defined.
The benefit if I could extend the base item would be that Im (more) sure the structures are identical. In usage I can call in a specific type rather use the base type and assign which type. Usage by pointer/ref to log_data_base would also be easier..
Is this possible to achieve in some way that I have missed?
Easier that I thought, by ALIAS
Apparently my construction will not work as any of the sub-types are not be able to accept any (further) value-initiation.
Ie:
string_item : log_data_base := (string_value := 'test', val_type := STRING_);
is not eq to (the string_value will not be assigned):
string_item_2 : log_data_string := (string_value := 'test');
Because effectively for the compiler this is eq. to a "double" assignment where the compiler sees only the first assignment (my guess).
string_item_2 : log_data_base := (val_type := STRING_) := (string_value := 'test');
Im not sure this should be treated as a bug, but it would surely be nice to be able to use this construction (compiler is 3.5.20.40).
Anyone see any other alternative for solution (other than 4 complete STRUCTS with all fields individually defined)?
Last edit: pernockham 2025-03-07
Maybe the ANY type can help?
https://content.helpme-codesys.com/en/CODESYS%20Development%20System/_cds_datatype_any.html
Interesting, thanks! Didnt know about ANY before, and will sure find a use-case for it. Im not sure it will help me here though, it seems to me that an ANY will not accept a string assignment?
I actually think that the "ALIAS" should work, that was a hard found error. I had a fair bit of scratching my head looking for misdirected pointers before I understood what was (not) happening. I will use the work-around of individual structs instead.
Strangely reminds me to my struggles... Want to do something "Elegant", reusable, universal, practical... In CODESYS??? π
First of all, before you get too deep into this:
If you could find a way, to make a "universal" log entry object, containing the variable length data itself, you wouldn't be able to store them in an array, or access them like an array, or pass them by value as a type. (please correct me, if I'm wrong, incorrect, or not precise).
Because... Basically you can't declare a type with variable memory footprint. This is a very deeply embedded characteristic of CODESYS, and all IEC 61131-3 systems, and it has many reasons behind. And yes, it is a very common trap / mistake, to forget about.
So, with a log entry - I guess - it's pretty much the purpose: store data and metadata together, and then handle it in a uniform way. There are ways to handle this, really depends on what is the purpose. For example:
1. Entries with fixed length
(Maybe it is not as evil as it looks for the first time. Depends on the situation, but definitely the fastest and easiest code)
You can have your base object, with an internal, fixed length string or byte array variable. I would go with a string, and call it _Data.; And then you can make properties, like
As_Bool, As_Int, As_Real...
In the 'set' accessors, you can do like:
In the 'get' accessors, evidently:
Or, can use ANY type, if you are not obsessed with variable / property like access:
2. Fixed length, but nicer
First, some disadvantage to any values:
- You can only assign values with write access. No literals, constants, etc...
- Can only be used as input variable of function or function_block
- Therefore, stg you could reach:
LogEntry.Initialize (stVariable|rVariable|iVariable|xVariable);
Just a quick example (it's funny to play with ANY):
Be careful it was not tested. I'm sure can be done better, please feel free to comment
Be careful it was not tested. I'm sure can be done better, please feel free to comment
3. If you really want to do entries with variable size
In a standard environment, it would be similar to the previous, except you dont have the container variable _Data, just use a pointer, practically _Descr.pValue
At Initialize (SET_Value), you have to allocate the memory, would be easy with SysMem.SysMemAlloc - nowadays with SysMem.SysMemAllocData -, and you make sure to release it after use with SysMem.SysMemFreeData...
SysMemAlloc was already hidden. The problem with this, that sooner or later your application will totally fragment the dynamic memory, and fail...
So should look for some form of dynMaybe MemUtils.MemoryManager (I am not sure what is the status and the future of it).
4. You will end up by a LogEntry Factory
...
5. You could still have a look at this
IEC Snippets
BTW, Standard Codesys Logger is not a bad choice either. If you are really interested, I share some more code / library.
Using a UNION instead of a STRUCT alone should give you that flexibility and only occupy memory once.

And if you want to be really elegant, you could use an FB, which only holds PUBLIC VAR.
Because a POU can use "EXTENDS"/inheritance. In CODESYS V3, an FB has a lot in common with C++ classes.
Using ANY or ALIAS is rarely a good idea. It neither won't be pretty, nor have good readability
and requires you to create a lot of error-prone things around it.
Last edit: Hismoon 2025-03-14
--- deleted. Was a bug.
Last edit: Hismoon 2025-03-14
--- deleted. Was a bug.
Last edit: Hismoon 2025-03-14
--- deleted. Was a bug.
Last edit: Hismoon 2025-03-14
--- deleted. Was a bug.
Last edit: Hismoon 2025-03-14
--- deleted. Was a bug.
Last edit: Hismoon 2025-03-14