Post by struccc on Inheritence of struct,
CODESYS Forge
talk
(Post)
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); // POINTER TO REAL As_Real := pReal^; In the 'get' accessors, evidently: pReal := ADR(_Data); // POINTER TO REAL pReal^ := 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_BLOCK FB_LogEntry VAR_INPUT MsgClass : UDINT; // Like DEBUG, WARN, ERR... MsgCode : UDINT; // Like Errors.ERR_FAILED MsgTS : DT; // The timestamp END_VAR VAR _Data : STRING(80); // Our data container... _Descr : __SYSTEM.AnyType; // A standard descriptor for our data, containing TYPE_CLASS, address and size END_VAR METHOD SET_Value : BOOL VAR_INPUT anyValue : ANY; END_VAR VAR I : DINT; diSize : DINT; pStr : POINTER TO STRING; END_VAR // Check what did we receive in anyValue. diSize := anyValue.diSize; // We use constant __SYSTEM.TYPE_CLASS to identify the received data type CASE anyValue.TypeClass OF // Maybe we don't want to store references, pointers... and who knows what else... __SYSTEM.TYPE_CLASS.TYPE_REFERENCE, __SYSTEM.TYPE_CLASS.TYPE_POINTER : SET_Value := FALSE; // For the planned types we will be just fine. TYPE_CLASS.TYPE_BOOL, TYPE_CLASS.TYPE_INT, TYPE_CLASS.TYPE_REAL : SET_Value := TRUE; // Optionally string can be handled separately, maybe we have received STRING(255), but practically it is shorter than 80 bytes... TYPE_CLASS.TYPE_STRING : pStr := anyValue.pValue; diSize := MIN(anyValue.diSize, LEN(pStr^) + 1); // Get the actual size, and rewrite the received structure member diSize := MIN(SIZEOF(_Data), diSize); // Can chop down the received string to our length... SET_Value := TRUE; // Maybe want to play a little bit more here, to narrow down or convert datatypes, etc... // Or just reject any other datatype ELSE SET_Value := FALSE; RETURN; END_CASE // Fail, if the received value is still larger than our container... IF diSize > SIZEOF(_Data) THEN SET_Value := FALSE; END_IF // Here we should be ok, just set up the _DataType structure, and copy store the data IF SET_Value THEN THIS^._Descr.TypeClass := anyValue.TypeClass; // The typeclass is already filtered THIS^._Descr.diSize := diSize; // Set the (adjusted) size THIS^._Descr.pValue := ADR(_Data); // This will not change, just to be sure {IF defined (pou:SysMem.SysMemCpy)} SysMem.SysMemCpy(_DataType.pValue, anyValue.pValue, TO_UDINT(anyValue.diSize)); {ELSE} // An ugly replacement MemCpy FOR I:=0 TO diSize - 1 DO _Descr.pValue[I] := anyValue.pValue[i]; END_FOR {END_IF} // Otherwise, in case of failure maybe better set an empty value (overwrite the former data descriptor) ELSE THIS^._Descr.TypeClass := TYPE_CLASS.TYPE_NONE; THIS^._Descr.pValue := ADR(_Data); THIS^._Descr.diSize := 0; END_IF METHOD GET_Value : BOOL VAR_INPUT anyValue : ANY; END_VAR VAR I : DINT; END_VAR // We just have to serve the data, using the __System.AnyType structure received // Roughly we can say: IF anyValue.TypeClass = _Descr.TypeClass AND anyValue.pValue <> 0 // This should not be possible, already taken care of by Codesys (?) THEN {IF defined (pou:SysMem.SysMemCpy)} SysMem.SysMemCpy(anyValue.pValue, _DataType.pValue, TO_UDINT(MIN(anyValue.diSize, _Descr.diSize))); {ELSE} // An ugly replacement MemCpy FOR I:=0 TO MIN(anyValue.diSize -1, _Descr.diSize - 1) DO anyValue.pValue[I] := _Descr.pValue[I]; END_FOR {END_IF} // Just to make sure, that our string is terminated... IF anyValue.TypeClass = TYPE_CLASS.TYPE_STRING THEN anyValue.pValue[anyValue.diSize -1] := 0; END_IF GET_Value := TRUE; RETURN; END_IF // ... But can play more CASE anyValue.TypeClass OF TYPE_CLASS.TYPE_WSTRING : ; // Could do conversion TYPE_CLASS.TYPE_XSTRING : ; // Wow, I have to figure this out TYPE_CLASS.TYPE_PARAMS : ; // BTW, what is this, how to use? TYPE_CLASS.TYPE_ANYNUM : ; // ... END_CASE 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.
Last updated: 2025-03-09
Post by pierre123 on User management - User Tab missing
CODESYS Forge
talk
(Post)
i had the same problem. Thanks you for the response.
Last updated: 2023-08-25
new feature: list/view plclogs from the plcs
CODESYS Forge
talk
(Thread)
new feature: list/view plclogs from the plcs
Last updated: 2023-08-30
Need help before losing control over myself with the BK1150 ;-)
CODESYS Forge
talk
(Thread)
Need help before losing control over myself with the BK1150 ;-)
Last updated: 2017-05-31
Compiling CoDeSys project using the command line interface
CODESYS Forge
talk
(Thread)
Compiling CoDeSys project using the command line interface
Last updated: 2021-01-27
How to control the Amp2 from HifiBerry ?
CODESYS Forge
talk
(Thread)
How to control the Amp2 from HifiBerry ?
Last updated: 2023-07-04
The required device description is not installed..
CODESYS Forge
talk
(Thread)
The required device description is not installed..
Last updated: 2023-10-03
Focus/Selection color frame around the element
CODESYS Forge
talk
(Thread)
Focus/Selection color frame around the element
Last updated: 2023-10-27
Post by mmoncada on Dinamically loading recipes
CODESYS Forge
talk
(Post)
I'm having trouble with the initial loading of recipes. I can create them and load one or the other, but when the program restarts the recipes are not listed. I'm calling: RecipeManCommands.ReloadRecipes and then RecipeManCommands.GetRecipeCount which returns 0 recipes. None of the methods returns error and if i call them again after RecipeManCommands.CreateRecipe RecipeManCommands.ReadAndSaveRecipe getRecipeCount returns the number of recipes created. When i restart the system, once again they are not listed even though the recipe files are there. Any ideas? Martin Btw, Codesys 3.5 SP 16 Path 9
Last updated: 2023-12-06
Using the Wago 750-658 CAN Gateway
CODESYS Forge
talk
(Thread)
Using the Wago 750-658 CAN Gateway
Last updated: 2024-07-23
No gateway with the given settings could be found
CODESYS Forge
talk
(Thread)
No gateway with the given settings could be found
Last updated: 2024-08-02
Get the .git folder path via scripting
CODESYS Forge
talk
(Thread)
Get the .git folder path via scripting
Last updated: 2024-08-08
How Could I Desactivated The Watchdog ?
CODESYS Forge
talk
(Thread)
How Could I Desactivated The Watchdog ?
Last updated: 2024-08-13
Post by remoyang on Package manager
CODESYS Forge
talk
(Post)
Also during the installation, it has the following error as attached
Last updated: 2024-08-16
Disable Communications to 'Modbus_Server_COM_Port' from the logic
CODESYS Forge
talk
(Thread)
Disable Communications to 'Modbus_Server_COM_Port' from the logic
Last updated: 2024-08-30
Is the Realtek RTL8119I-CG ethernet controler on supported list?
CODESYS Forge
talk
(Thread)
Is the Realtek RTL8119I-CG ethernet controler on supported list?
Last updated: 2024-09-04
Is the Realtek RTL8119I-CG ethernet controler on supported list?
CODESYS Forge
talk
(Thread)
Is the Realtek RTL8119I-CG ethernet controler on supported list?
Last updated: 2024-09-04
Is the Realtek RTL8119I-CG ethernet controler on supported list?
CODESYS Forge
talk
(Thread)
Is the Realtek RTL8119I-CG ethernet controler on supported list?
Last updated: 2024-09-04
Post by jinlee on Camera Feed (CCTV)
CODESYS Forge
talk
(Post)
Hi, Thanks for the information. However, I can't download the associated project.
Last updated: 2024-09-12
Clarifications regarding the appropriate runtime system to use
CODESYS Forge
talk
(Thread)
Clarifications regarding the appropriate runtime system to use
Last updated: 2024-09-26
Error trying to activate the demo Git licence
CODESYS Forge
talk
(Thread)
Error trying to activate the demo Git licence
Last updated: 2024-10-11
Post by pernockham on Persistence Manager: Config File not found?
CODESYS Forge
talk
(Post)
Trying to utilize the persistence manager, but system do not write to file and do not create the config file. System is a Linux SL, version 4.13.0.0 Problems with persistence/config started when I had other problems to go online, I deleted the config file and the persistent files, but the system will not re-create them. I have tried to remove/re-install the "PersistenceManager"-module, create the folder-structure myself. And also reinstall the linux-runtime but its clearly not working. Any help appreciated.
Last updated: 2024-10-31
The application does not exist on "device"
CODESYS Forge
talk
(Thread)
The application does not exist on "device"
Last updated: 2024-12-17
Removing a symbol from the IEC Symbols Editor grid
CODESYS Forge
talk
(Thread)
Removing a symbol from the IEC Symbols Editor grid
Last updated: 2025-02-13
Post by pistola on Pause Tab Order
CODESYS Forge
talk
(Post)
I'm programming an HMI, and on a settings page I have external buttons allow the operator to tab between various elements to select which one they want to adjust. I then use "Input Configuration" > "OnMouseClick" to select which variable to adjust. The problem is, when adjusting the variable the pointer will move to the other elements within the tab order. Is there a way to pause or prevent the pointer to move to the other elements when a variable is TRUE?
Last updated: 2025-03-17
To search for an exact phrase, put it in quotes. Example: "getting started docs"
To exclude a word or phrase, put a dash in front of it. Example: docs -help
To search on specific fields, use these field names instead of a general text search. You can group with AND or OR.