Accessing TwinCAT configuration

This chapter describes how to create and access a TwinCAT configuration project via Automation interface. The objective of this creation process is to get access to a TwinCAT System Manager configuration.

The following code snippets demonstrate how to get access to TwinCAT and create a System Manager configuration.

Sample (C#):

TcSysManager systemManager = new TcSysManager();
systemManager.NewConfiguration();
//...Creating configuration...
systemManager.SaveConfiguration("C:\Sample.tsm");

Sample (Powershell):

$systemManager = new-object -comobject TCatSysManager.TcSysManager
$systemManager.NewConfiguration()
//...Creating configuration...
$systemManager.SaveConfiguration("C:\Sample.tsm")

Sample (IronPython):

sysManType = System.Type.GetTypeFromProgID("TCatSysManager.TcSysManager")
systemManager = System.Activator.CreateInstance(sysManType)
systemManager.NewConfiguration()
systemManager.SaveConfiguration("C:\Sample.tsm")

Sample (C++):

#include "stdafx.h"
#include <atlbase.h>

#import "C:\TwinCAT\Io\TCatSysManager.tlb"

int _tmain(int argc, _TCHAR* argv[])
{
  HRESULT hr = CoInitialize(NULL);

  if (SUCCEEDED(hr))
  {
    TCatSysManagerLib::ITcSysManager3Ptr pTCatSysMan;
    HRESULT hr = pTCatSysMan.CreateInstance("TCatSysManager.TcSysManager.1");

    CComBSTR bstrFile(L"C:\\Untitled.tsm");

    hr = pTCatSysMan->OpenConfiguration(bstrFile.Detach());

    if(SUCCEEDED(hr))
    {
      hr = pTCatSysMan->ActivateConfiguration();

      if(SUCCEEDED(hr))
      {
        hr = pTCatSysMan->StartRestartTwinCAT();
      }
    }

    if (FAILED(hr))
    {
      CComPtr<IErrorInfo> pErrorInfo;
      hr = ::GetErrorInfo(0,&pErrorInfo);

      if (hr == S_OK)
      {
         CComBSTR pbstrError(L"");
         hr = pErrorInfo->GetDescription(&pbstrError);
         wprintf((LPWSTR)pbstrError);
      }
    }
  }

  CoUninitialize();
  return 0;
}