Post by john-robinson on Limiting Memory Access of an Array to Within its Bounds
CODESYS Forge
talk
(Post)
Recently we had an issue regarding some simple code to calculate a rolling average. The code indexes from zero to 199 to properly store the current input into a circular buffer which then allows us to calculate a rolling average: VAR input_5s : REAL; outs_arr : ARRAY[0..199] OF REAL; i : USINT := 0; END_VAR ___ //this code runs every five seconds, calculating a rolling average outs_arr[i] := input_5s; i := i + 1; output := OSCAT_BASIC.ARRAY_AVG(ADR(outs_arr), SIZEOF(outs_arr)); IF i >= SIZEOF(outs_arr) THEN i := 0; END_IF There is a simple bug in this code where the index will be set to 0 when it has surpassed the length of the array in bytes (800 in this case) rather than larger than the number of reals in the array (200). The solution here is simple, replacing i >= SIZEOF(outs_arr) with i >= SIZEOF(outs_arr)/SIZEOF(outs_arr[0]). In this example when the index increased to 201 and the line outs_arr[201] := input_5s was called, codesys arbitrarily wrote to the address in memory that is where outs_arr[201] would be if the array was that long. I would like to find a way to wrap the codesys array inside of a wrapper class that checks if an input is within the bounds of an array before writing to that value. I know how I would implement that for a specific array, I could create a method or class that takes an input of an array of variable length, ie. ARRAY[*] OF REAL, but I don't know how to make this for any data type. I am wondering if anyone has ever done anything similar to this, or has any better suggestions to ensure that none of the programmers on this application accidentally create code that can arbitrarily write to other locations in memory.
Last updated: 2024-03-05
Post by wehling-h on PI Input value
CODESYS Forge
talk
(Post)
Hi geoweil, have you tried to save changed values to remanent area or using a VAR RETAIN? After restart you have to initialize Counter with last remanent value. Depending on changes per cycle and the possibilitiy of your control unit to write remanent storage. Otherwise you need a buffering system to ensure save backup of last PI_Counter value. May you could read this Thread to get further help: https://forge.codesys.com/forge/talk/Engineering/thread/31e1c8349e/#bf48
Last updated: 2024-01-12
Network Variable List in Codesys 3.5 on CAN
CODESYS Forge
talk
(Thread)
Network Variable List in Codesys 3.5 on CAN
Last updated: 2023-08-05
Importing variable list from Excel into Codesys
CODESYS Forge
talk
(Thread)
Importing variable list from Excel into Codesys
Last updated: 2018-03-21
Visualization "F" Key not Reseting Variable
CODESYS Forge
talk
(Thread)
Visualization "F" Key not Reseting Variable
Last updated: 2018-01-18
Visualisierungs-Element bei Variable=FALSE unsichtbar schalten
CODESYS Forge
talk
(Thread)
Visualisierungs-Element bei Variable=FALSE unsichtbar schalten
Last updated: 2019-06-24
DT Variable in der CoDeSys Visualisierung anzeigen lassen
CODESYS Forge
talk
(Thread)
DT Variable in der CoDeSys Visualisierung anzeigen lassen
Last updated: 2016-04-28
Alarm Management - latched variable represented as Text list
CODESYS Forge
talk
(Thread)
Alarm Management - latched variable represented as Text list
Last updated: 2023-11-07
Variable indicating axis pending gearing or camming
CODESYS Forge
talk
(Thread)
Variable indicating axis pending gearing or camming
Last updated: 2023-11-28
Saving variable values on power cycle (RevPi Connect)
CODESYS Forge
talk
(Thread)
Saving variable values on power cycle (RevPi Connect)
Last updated: 2023-12-04
Adding a variable from text list into ST code
CODESYS Forge
talk
(Thread)
Adding a variable from text list into ST code
Last updated: 2023-12-07
Adding a variable from text list into ST code
CODESYS Forge
talk
(Thread)
Adding a variable from text list into ST code
Last updated: 2023-12-07
Visualisierung wechseln ΓΌber SPS Variable Codesys 3.5
CODESYS Forge
talk
(Thread)
Visualisierung wechseln ΓΌber SPS Variable Codesys 3.5
Last updated: 2024-08-27
Using Latch Variable in Alarm as input from text list
CODESYS Forge
talk
(Thread)
Using Latch Variable in Alarm as input from text list
Last updated: 2025-03-06
WebVisu Numpad dialog - Dialogtitle not accepting string variable
CODESYS Forge
talk
(Thread)
WebVisu Numpad dialog - Dialogtitle not accepting string variable
Last updated: 2025-05-29
Read Global Variable from an IoT Device
CODESYS Forge
talk
(Thread)
Read Global Variable from an IoT Device
Last updated: 2025-07-22
Current Visu name without Current Visu Variable
CODESYS Forge
talk
(Thread)
Current Visu name without Current Visu Variable
Last updated: 2025-08-13
BOOL variable in OPC UA and modbus simultaneously
CODESYS Forge
talk
(Thread)
BOOL variable in OPC UA and modbus simultaneously
Last updated: 2025-08-18
Importing variable list from Excel into Codesys
CODESYS Forge
talk
(Thread)
Importing variable list from Excel into Codesys
Last updated: 2017-04-18
kopieren einer INt Variable in einer Funktion
CODESYS Forge
talk
(Thread)
kopieren einer INt Variable in einer Funktion
Last updated: 2010-08-27
How to create Network Variable by Script
CODESYS Forge
talk
(Thread)
How to create Network Variable by Script
Last updated: 2017-12-08
Defining two variable in one rectangle box in Visualization
CODESYS Forge
talk
(Thread)
Defining two variable in one rectangle box in Visualization
Last updated: 2017-04-22
Execute a external program when variable change true/false
CODESYS Forge
talk
(Thread)
Execute a external program when variable change true/false
Last updated: 2015-06-17
bestimmen, ob eine Variable Input, Output oder Merker ist
CODESYS Forge
talk
(Thread)
bestimmen, ob eine Variable Input, Output oder Merker ist
Last updated: 2009-01-15
Network Variable Setting ON Raspberry PI [Problem]
CODESYS Forge
talk
(Thread)
Network Variable Setting ON Raspberry PI [Problem]
Last updated: 2019-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.