Reading and setting the declaration and implementation of a POU
Each POU with a declaration and implementation area exposes these via the interfaces ITcPlcDeclaration and ITcPlcImplementation. You can read and set both areas using the properties DeclarationText and ImplementationText. ImplementationXml provides the implementation in PLCopen XML format.
Requirements
- TwinCAT XAE is installed and started.
- A DTE connection to the target instance is active.
-
ITcSysManageris initialized in an STA thread. - The target POU is included in the PLC project.
Setting the declaration and implementation
The POU's Tree Item is converted via cast to ITcPlcDeclaration and ITcPlcImplementation. The respective content is set using the properties DeclarationText and ImplementationText.
// Cast directly to declaration and implementation interfaces
ITcSmTreeItem method = sysManager.LookupTreeItem(@"TIPC^PLC1^PLC1 Project^POUs^MyFB^MyMethod");
ITcPlcDeclaration decl = method as ITcPlcDeclaration;
decl.DeclarationText = "VAR_INPUT\n nValue : INT;\nEND_VAR";
ITcPlcImplementation impl = method as ITcPlcImplementation;
impl.ImplementationText = "MyMethod := nValue > 0;";
Reading the declaration and implementation
These same properties can also be read. The following sample creates a function block and then reads its declaration, implementation, and implementation XML.
$plcPousItem = $systemManager.LookupTreeItem("TIPC^PlcGenerated^PlcGenerated Project^POUs")
$newFb = $plcPousItem.CreateChild("FB_TEST", 604, "", 6)
$decl = $newFb.DeclarationText
$impl = $newFb.ImplementationText
$implXml = $newFb.ImplementationXml
POU types with declaration and implementation areas
Not every type of POU has both areas. The following table shows which types have a declaration area and which have an implementation area.
|
Tree Item |
Type |
Declaration area |
Implementation area |
|---|---|---|---|
|
Program |
POU |
Yes |
Yes |
|
Function |
POU |
Yes |
Yes |
|
Function block |
POU |
Yes |
Yes |
|
Action |
POU |
No |
Yes |
|
Method |
POU |
Yes |
Yes |
|
Property (Get/Set) |
POU |
Yes |
Yes |
|
Transition |
POU |
No |
Yes |
|
Enum |
DUT |
Yes |
No |
|
Struct |
DUT |
Yes |
No |
|
Union |
DUT |
Yes |
No |
|
Alias |
DUT |
Yes |
No |
|
Interface |
Interface |
Yes |
No |
|
Property (Get/Set) |
Interface |
Yes |
Yes |
|
Global variables |
GVL |
Yes |
No |
![]() | The process of creating POUs and other PLC objects is described in Creating PLC objects. |
