Creating an FB which provides its functionality globally in the first PLC
1. Create a PLC and prepare a new function block (FB) (here: FB_Calculation). Derive the function block from the TcBaseModuleRegistered class, so that an instance of this function block is not only available in the same PLC, but can also be reached from a second. Note: as an alternative you can also modify an FB in an existing PLC.
2. The function block must offer its functionality by means of methods. These are defined in a global interface, whose type is system-wide and known regardless of programming language. To create a global interface, open the Context menu in the “Interface” tab of System Properties and choose the option “New”.
The TMC Editor opens, which provides you with support in creating a global interface.
3. Specify the name (here: I_Calculation) and append the desired methods. The interface is automatically derived from ITcUnknown, in order to fulfill the TwinCAT TcCOM module concept.
4. Specify the name of the methods analogously (here: Addition() and Subtraction()) and select HRESULT as return data type. This return type is mandatory if this type of TcCOM communication should be implemented.
5. Specify the method parameters last and then close the TMC Editor.
6. Now implement the I_Calculation interface in the FB_Calculation function block and append the c++_compatible attribute.
7. Choose the “Implement interfaces...” option in the Context menu of the function block in order to obtain the methods belonging to this interface.
8. Delete the two methods TcAddRef()and TcRelease() because the existing implementation of the base class should be used.
9. Create the FB_reinit() method for the FB_Calculation function block and call the basic implementation. This ensures that the FB_reinit() method of the base class will run during the online change. This is imperative.
10. Implement the TcQueryInterface() method of the Interface ITcUnknown. Via this method it is possible for other TwinCAT components to obtain an interface pointer to an instance of this function block and thus actuate method calls. The call for TcQueryInterface is successful if the function block or its base class provides the interface queried by means of iid (Interface ID). For this case the handed over interface pointer is allocated the address to the function block type-changed and the reference counter is incremented by means of TcAddRef().
11. Fill the two methods Addition() and Subtraction() with the corresponding code to produce the functionality: nRes := nIn1 + nIn2 and nRes := nIn1 - nIn2
12. Add one or more instances of this function block in the MAIN program block or in a global variable list.
The implementation in the first PLC is complete.
After compiling the PLC, the object ID of the TcCOM object which represents the instance of FB_Calculation is available as an outlet in the in the process image.