Creating internal variables and functions

Dear Community Members,

I am developing a library, is there a way to declare internal functions and function blocks (internally used by the library blocks but are not available for the end user)? Also, from the set of variables used within the function blocks, can I hide all the internal variables, so that the user can’t see them?
Also, Can I categorize my function blocks within the said library as mentioned below?
image

Thanks and regards
Ashamdeep Singh Saini

Hi Asham,

If you’re concerned about users of your libraries accessing the source code, something you can do is export the library as a binary. When someone imports your library into their project, they will not see the source code. However, they will still see

  • The declared functions/function blocks within the .fun file
  • Any constants declared within the .var file in the library folder
  • Any datatypes declared within the .typ file in the library folder

Binary libraries will appear green in the Logical View as B&R libraries do. If you plan to do this, make sure you document your library well and version control your source code! Should you need to make changes later, there is no way to get the original source code from a binary library. And if your library requires functions from any other library in order to work, make sure to list those on the Dependencies page of the library’s Properties.

…is there a way to declare internal functions and function blocks (internally used by the library blocks but are not available for the end user)

Any functions/function blocks declared in the .fun file are useable by the user as this file defines the library’s available functions. If you write a library using C, you can define helper functions directly within the library’s C files without using the .fun file. However, I don’t think this is possible for Structured Text. If you want to use a function block within a function internally, you can declare that function block as an Internal variable within your function like this:

image

If you’re using custom functions internally and you don’t want to declare them in the same library, you can create a second (or third, or fourth…) library and list it as a dependency to your main library. This library can then hold your helper functions. They’d still be visible to the user, but they’d be in a separate file.

Also, from the set of variables used within the function blocks, can I hide all the internal variables, so that the user can’t see them?

Internal variables declared within the .fun file are always visible to the user. If your library is written in C, you can declare variables within the function/function block/library file and use them without putting them in the .fun file. This essentially hides them if the user cannot access the original source file. Again however, I don’t believe this is possible in Structured Text.

Also, Can I categorize my function blocks within the said library as mentioned below

This seems to work. At the end of the day as long as the compiler can find all of the declarations and implementations it will be able to compile the code. Let us know if you get any errors when you try this. If you export the library as a binary, the end user won’t see the structure anyway.

3 Likes

Some tips from my experience.

Also, from the set of variables used within the function blocks, can I hide all the internal variables, so that the user can’t see them?

Best practice is to define a data type for the internal variables and only have 1 “Internal” component in your FB interface, like so:

This allows you to insert the FB instance variable into a task watch, expand the “Internal” structure and look at all internal variables for trouble shooting.

4 Likes

Dear Marcus,

Thanks for the reply.

My purpose is to hide all the unnecessary information(local variables, supporting function blocks) from the user, which can make trouble shooting difficult for users.

I tried using C for developing library but I am unable to communicate with the input/output variables when declared as user defined Datatypes.

2 Likes

Dear Martin,

Thanks for the suggestion.

Dear Asham,

libraries with C sources work like this:

  1. Create a “LibName”.fun with the function block interface definition, e.g.:
    image

  2. Automation Studio will create a “LibName”.h when the project is built, with the data type definition, e.g.:
    image

  3. When a task is calling an instance of your function block the address of the variable with that data structure is passes into the implementation. You can access inputs/outputs/variables linke this:
    image

2 Likes

Dear Martin,

Thanks for your quick reply and guidance.
What if my input/output variable is a data type or enumeration.
E.g.



if (inst → Ipar.Press_PV ==0){
}
throws an error.

2 Likes

I have never used VAR_IN_OUT myself.

Please have a look into “LibName”.h and check what data type is assigned to it, it might be a pointer
inst → Ipar→Press_PV

or an address
((AHU_DT *)(inst → Ipar))->Press_PV

3 Likes

Thanks for the suggestion :smiley: