Creating the interface for a C-block or C++-block (deprecated)

How to create the interface for your →blocks in →C or →C++:

  1. In the project explorer: Create a new ST-interface within the project.
    Result: The ST-editor is opened for this file.

  2. Enter the following ST-code at the top of this file:

    For C-blocks

    Sytnax
    {extern_c}
     
    ( * optional_begin *) USING Namespace_1;
    USING Namespace_2; (* optional_end *)
     
    // optional: enter the supported platforms
    {Supported_Platforms := platform-1, platform-2, ... platform-n}
    Example
    {extern_c}
    {Supported_Platforms := WindowsX86, Ce5Nmr20}

    For C++-blocks

    Sytnax
    {extern_cxx}
     
    ( * optional_begin *) USING Namespace_1;
    USING Namespace_2; (* optional_end *)
     
    // optional: enter the supported platforms
    {Supported_Platforms := platform-1, platform-2, ... platform-n}
    Example
    {extern_cxx}
    {Supported_Platforms := WindowsX86}

    Meaning of the syntax:

    • Due to {extern_c} or {extern_c}, logi.CAD 3 recognizes that you wish to create the applications for the following blocks in C or C++. This is valid for all blocks that you will create in the file. Create different files, if you want to declare blocks for C as well as for C++.

    • The USING namespace directive after {extern_c} or {extern_cxx} is optional. See "Namespaces in ST: usage" for details on this directive.

    • Due to {Supported_Platforms := ...}, logi.CAD 3 recognizes that the blocks and data types are valid for the defined platforms only. This is valid for all elements that you will declare in the file. If you do not enter {Supported_Platforms := ...}, the declared elements are valid for all platforms possible within logi.CAD 3. Those platforms are listed under "Configuring PLC within PLC-object". However, observe that the platform BuiltInPlc is not supported for {Supported_Platforms := ...}. Best practice is to use the content assist in order to enter the requested platforms.

  3. Below those lines, declare the requested blocks (i.e., →function blocks and/or →functions). Moreover, it is possible to declare →data types within the file.
    The declaration of those elements is done as usual within the ST-editor. Information about the necessary syntax is listed under: "Declaration of a function block in ST", "Declaration of a function in ST", "Declaration of a data type in ST"
    Observe that the declaration of a →program is not possible within this file – due to {extern_c} or {extern_cxx}.

  4. Moreover for each function block and/or function: Declare the interface for this block, e.g. →input variables and →output variables.
    This declaration is done as usual within the ST-editor. Information about the necessary syntax is listed under: "Declaration of variables in ST"
    Observe that the declaration of →in-out variables, →external variables, →temporary variables as well as of →methods and →assignments are not possible within this file – due to {extern_c} or {extern_cxx}.
    logi.cals recommends you to declare all types of variables within the interface – in particular, if the values of the variables should be kept for the block from one execution of the application to the next one. So you will avoid problems that might occur otherwise when updating the changed application by using the resource manager (also known as reloading the application).

    If you do declare variables just within a C-file and some belated changes of the block logic are needed, logi.cals recommends you to create a new block containing the changed logic – instead of actually changing the block. Subsequently, it will be possible to use the r esource manager for updating the changed application.
    logi.CAD 3 does not prevent the change of a block with variables only contained in the C-file. If you make such changes and use the resource manager to update the changed application, unexpected errors might occur. For example, the application might be executed wrongly or it might be terminated, the PLC might even be terminated. In such cases, best practice is to build the application anew and then to load it onto the PLC.

    Example for declaration of a C-function block and a C-function (both with an interface)
    {extern_c}
    {Supported_Platforms := WindowsX86, Ce5Nmr20}
     
    FUNCTION_BLOCK MyCFB // The function block 'MyCFB' has 2 inputs and 1 output.
    VAR_INPUT
    In1, In2 : INT;
    END_VAR
    VAR_OUTPUT
    Out : INT;
    END_VAR
    END_FUNCTION_BLOCK
     
    FUNCTION MyCFun : INT // The function 'MyCFun' has 2 inputs and 2 outputs.
    VAR_INPUT
    In1, In2 : INT;
    END_VAR
    VAR_OUTPUT
    Out : INT;
    END_VAR
    END_FUNCTION
  5. Save the ST-interface with {extern_c} or {extern_cxx}.
    Now logi.CAD 3 is creating the files with C-code and the header files for the C-code, this is done for each declared function block and function.

    File

    Position

    Note

    LCfu___name.c or LCfu___name.cppRelated to the example: LCfu___MyCFB.c and LCfu___MyCFun.c

    in the folder in which the ST-interface has been created
    Example: in the folder src of the project

    This is the file in which you will create your C-code later. name is the name of the function block or function as it has been entered within the ST-interface.
    If this file does already exist, it is not overwritten by logi.CAD 3 when the ST-interface with {extern_c} or {extern_cxx} is saved. Furthermore, this file is not deleted, if you clean the project.

    LCfu___name.h
    Related to the example: LCfu___MyCFB.h and LCfu___MyCFun.h

    in the folder src-gen of the project

    This is the header file for the C-code. Do not modify this file because it is overwritten by logi.CAD 3 when the ST-interface with {extern_c} or {extern_cxx} is saved. name is the name of the function block or function as it has been entered within the ST-interface.

    By default, the header files are not displayed within the project explorer. If you want to display these files within the project explorer: Click images/download/attachments/451280970/IconViewMenu-version-1-modificationdate-1598000630466-api-v2.png in toolbar of the project explorer, select Filters and Customization... and uncheck src-gen folder (under Filter).

    If you change the already created interface of a C-block after you have saved the ST-interface with {extern_c} or {extern_cxx} for the first time (e.g. you rename the block or you create new in-/outputs), you have to perform some steps. Details: see "Anpassungen bei nachtraeglicher Aenderung der Schnittstelle fuer C-Baustein oder C++-Baustein".