Opening an existing PLC project
This chapter explains how to open and further process an existing PLC project using the TwinCAT Automation Interface. Topics:
- Opening an existing PLC project
- Saving a PLC project as a library
- Using online functions (Login, StartPlc, StopPlc)
- Setting boot project options
- Saving a project and/or solution as an archive
Opening an existing PLC project
To open an existing PLC project via the Automation Interface, navigate to the PLC node and select CreateChild() with the path to the .plcproj file.
There are three possible values for SubType:
0: Copy project to the solution directory1: Move project to the solution directory2: Use the original project location (in this case, pass""as the project name parameter)
These values correspond to the Yes, No, and Cancel buttons in the corresponding dialog box in TwinCAT XAE. Alternatively, a PLC project archive (.tpzip file) can be specified as a path.
C#:
ITcSmTreeItem plc = systemManager.LookupTreeItem("TIPC");
ITcSmTreeItem newProject = plc.CreateChild("NameOfProject", 1, "", pathToProjectOrTpzipFile);PowerShell:
$plc = $systemManager.LookupTreeItem("TIPC")
$newProject = $plc.CreateChild("NameOfProject", 1, "", pathToProjectOrTpzipFile)Saving a PLC project as a library
To save a PLC project as a PLC library, use the ITcPlcIECProject::SaveAsLibrary() method.
C#:
iecProject.SaveAsLibrary(pathToLibraryFile, false);
PowerShell:
$plcProject.SaveAsLibrary(pathToLibraryFile, $false)
The second parameter determines whether the library should be automatically installed in the default repository after saving.
Using online functions (Login, StartPlc, StopPlc, ResetCold, ResetOrigin)
The Automation Interface also supports PLC online functions, such as logging into a PLC runtime and starting, stopping, or resetting the PLC program. These functions are accessible via ProduceXml() and ConsumeXml() on an ITcPlcIECProject node.
XML structure:
<TreeItem>
<IECProjectDef>
<OnlineSettings>
<Commands>
<LoginCmd>false</LoginCmd>
<LogoutCmd>false</LogoutCmd>
<StartCmd>false</StartCmd>
<StopCmd>false</StopCmd>
</Commands>
</OnlineSettings>
</IECProjectDef>
</TreeItem>
C#:
string xml = "<TreeItem><IECProjectDef><OnlineSettings><Commands><LoginCmd>true</LoginCmd></Commands></OnlineSettings></IECProjectDef></TreeItem>";
ITcSmTreeItem plcProject = systemManager.LookupTreeItem("TIPC^NameOfProject^NameOfProject Project");
plcProject.ConsumeXml(xml);
Description of the XML nodes:
XML | Description |
|---|---|
|
|
|
|
|
|
|
|
![]() | To use commands such as |
Setting boot project options
This code sample sets boot project options for a PLC project using the ITcPlcProject interface.
C#:
ITcSmTreeItem plcProjectRoot = systemManager.LookupTreeItem("TIPC^PlcGenerated");
ITcPlcProject plcProjectRootIec = (ITcPlcProject) plcProjectRoot;
plcProjectRootIec.BootProjectAutostart = true;
plcProjectRootIec.GenerateBootProject(true);PowerShell:
$plcProject = $systemManager.LookupTreeItem("TIPC^PlcGenerated")
$plcProject.BootProjectAutostart = $true
$plcProject.GenerateBootProject($true)Saving a project and/or solution as an archive
To save the entire TwinCAT solution as a ZIP-compatible archive (*.tszip), use the ITcSysManager9 interface.
C#:
ITcSysManager9 newSysMan = (ITcSysManager9)systemManager;
newSysMan.SaveAsArchive(@"C:\test.tszip");PowerShell:
$systemManager.SaveAsArchive("C:\test.tszip")To reload a saved TSZIP file, use AddFromTemplate().
C#:
dte.Solution.AddFromTemplate("C:\test.tszip", @"C:\tmp", "CreatedFromTemplate");PowerShell:
$dte.Solution.AddFromTemplate("C:\test.tszip", "C:\tmp", "CreatedFromTemplate")To save a single PLC project as an archive (*.tpzip), use ITcSmTreeItem::ExportChild().
C#:
ITcSmTreeItem plc = sysManager.LookupTreeItem("TIPC");
plc.ExportChild("PlcProject", @"C:\PlcTemplate.tpzip");PowerShell:
$plc = $systemManager.LookupTreeItem("TIPC")
$plc.ExportChild("PlcProject", "C:\PlcTemplate.tpzip")CreateChild() is used to load a saved TPZIP file.
C#:
plcConfig.CreateChild("PlcFromTemplate", 0, null, @"C:\PlcTemplate.tpzip");PowerShell:
$plc.CreateChild("plcFromTemplate", 0, $null, "C:\PlcTemplate.tpzip")