1

This is how I have it now

TYPE MyType: STRUCT
    name: STRING[20];
    input: INT;
    output: INT;
    END_STRUCT
END_TYPE
PROGRAM PLC_PRG:
    VAR
        struct:  MyType;
        struct_NULL:  MyType;
        error: BOOL;
    END_VAR

    IF error THEN
        struct := struct_NULL;
    END_IF
END_PROGRAM

Is there another way to null the structure, without declearing and using struct_NULL

tomatoeshift
  • 465
  • 7
  • 23

2 Answers2

6

Just use SysMemSet (Codesys library SysMem), MemSet (Codesys library MemoryUtils) or MEMSET (TwinCAT 3 library Tc2_System) function to set all data to 0.

SysMemSet(
    pDest       := ADR(TestStruct), 
    udiValue    := 0, 
    udiCount    := SIZEOF(TestStruct)
);

You can write a simple helper function for it :)

FUNCTION F_Clear : BOOL
VAR_INPUT
    Target : ANY;
END_VAR
VAR
END_VAR


SysMemSet(
    pDest       := Target.pValue, 
    udiValue    := 0, 
    udiCount    := Target.diSize
);

Usage, works for all kinds of variables!

F_Clear(TestStruct);
Quirzo
  • 1,183
  • 8
  • 10
1

Yes, this would work, but can you guarantee that no one will write in the code something like this:

struct_NULL.input := 7;

So I would prefer to write an short function which set back all value to default:

FUNCTION F_setToDefault_MyStruct : MyStruct
VAR
    DefaultStruct   : MyStruct;
END_VAR

F_setToDefault_MyStruct := DefaultStruct;

So in the programm you can call:

//set struct to default values
TestStruct:=F_setToDefault_MyStruct();

I know, a lot more code, but no one can change the initial values in struct_NULL, and I think it´s much more easier to read and understand

Arndt
  • 98
  • 5