Codesys Error - Function block must be instantiated to be accessed

367 views Asked by At

In my Codesys 3.5.18 project, I had function blocks MyKeyboard and MyNumberPad declared from a library I had previously created (version 1.1.0) and added to the Library Manager.

PROGRAM Dummy
 VAR
  'some other declarations    
     MyKeyboard : ETN_KNP.ControlKeyboard;  
     MyNumberPad : ETN_KNP.ControlNumberPad;
  'some other declartions 
 END_VAR

I made some changes to this library, and gave it version 1.1.1. I updated the placeholder version to 1.1.1 in my Library Manager. Now, I am getting an error that the function blocks are not instantiated. They clearly are - the code that did that in the variable declaration part of main project is unchanged, the only change is the new library version.

enter image description here

compile error

I tried restarting my laptop and Codesys, hoping the issue is some confusion in the Library Manager / Repository with the new version. Why might this be happening and how can I fix it?

1

There are 1 answers

0
Guiorgy On

As function blocks in codesys are essentially what classes are in other languages, you must call the 'constructor' to initialize an object. In codesys this initialization is done by calling the FB_Init (Function Block Initialize) method.

Codesys implicitly calls the FB_Init method one first run with the arguments defined during the declaration, for example:

VAR
    fb: MY_FB("arg1", "arg2"); // fb.FB_Init(bInitRetains, bInCopyCode, "arg1", "arg2"); will be called.
    // bInitRetains and bInCopyCode are internal variables used by codesys, don't worry about them.
END_VAR

If the FB_Init is not explicitly called, then codesys automatically calls it without passing any arguments, in other words the below two declarations are semantically the same to codesys:

VAR
    fb1: MY_FB;
    fb2: MY_FB();
END_VAR

There's one exception though, you can disable the implicit call to FB_Init during declaration by using the noinit pragma, in which case you need to explicitly call FB_Init yourself once during application initialization:

VAR
    {attribute 'noinit'}
    fb: MY_FB;

    first_run: BOOL := TRUE;
END_VAR

IF first_run THEN
    first_run := FALSE;
    fb.FB_Init(FALSE, FALSE, "arg1", "arg2")
END_IF

Finally, you must ensure that, just like with any function call, the correct number and type of arguments are passed to the FB_Init function:

VAR
    fb: MY_FB("arg1", "arg2"); // error if MY_FB expects 1 or 3 arguments instead
END_VAR