Search code examples
twincatcodesysst

Need to optimize code (Reduce the size) CODESYS ST (TwinCat 2) to save memory


The code size (CODESYS ST, TwinCat 2) for the beckhoff BC9000 PLC (64 kB max) should be reduced to a minimum:

var

word_CO2: word;

(code skipped)

Need to reduce the size of this code:

MEMCPY(ADR(word_CO2),ADR(ReceiveBuffer[3]),1);

MEMCPY(ADR(word_CO2)+1,ADR(ReceiveBuffer[2]),1);


Solution

  • To reduce code size in CoDeSys 2.3 do the following:

    1. Go into 'Project' -> 'Options' -> 'Build'.
    2. Select 'Replace constants'.
    3. Select 'Exclude objects'... and start excluding grayout FBs and FUNs in the system libraries one-by-one.
    4. Perform 'Clean All' and 'Rebuild All' commands if you are not sure in what you are excluding. Also perform them regulary in between.

    If the above does not help your code needs review and optimization. Generally speaking try to avoid multiple IF...ELSIF statemenets, use MUX and SEL operators instead and (a little bit of) CASE logic. Switching to POINTER arithmetic and compacting data into ARRAYS also helps. Use bit access to BYTE, WORD and DWORD variables instead of BOOL variables.