I only have some experience with TwinCAT3, but the library is the same.
In TwinCAT3 we differ between Modes RUN and CONFIG. To switch between both modes you will need the TCatSysManagerLib, thats true. But there is (especially for TwinCAT3) no method for switching back to CONFIG mode (yet...but Beckhoff is working on it).
In case that you want to do this, you will need to go via the AmsPort.SystemService which is 10000.
Here an example how to switch from RUN to CONFIG mode:
public void setConfigMode()
{
TcAdsClient client = new TcAdsClient();
StateInfo mode = new StateInfo();
client.Connect((int)AmsPort.SystemService);
mode.AdsState = AdsState.Reconfig;
client.WriteControl(mode);
client.Dispose();
}
And here the opposite way from CONFIG to RUN mode (Using TCatSysManagerLib): --> only for TwinCAT3
public void setRunMode()
{
// _solution_path, _solution_name must be replaced by your solution path and solution name
Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)System.Activator.CreateInstance(t);
dte2.MainWindow.Visible = false; //keep TwinCAT window closed
Solution4 solution4 = (Solution4)dte2.Solution;
EnvDTE.Project project = null;
solution4.Open(Path.Combine(_solution_path, _solution_name + ".sln").ToString());
project = (EnvDTE.Project)solution4.Projects.Item(1);
ITcSysManager5 sysmanager5 = (ITcSysManager5)project.Object;
sysmanager5.StartRestartTwinCAT();
dte2.Quit();
}
or (I have not tried):
public void setRunMode()
{
TcAdsClient client = new TcAdsClient();
StateInfo mode = new StateInfo();
client.Connect((int)AmsPort.SystemService);
mode.AdsState = client.ReadState().AdsState;
mode.AdsState = AdsState.Reset;
client.WriteControl(mode);
client.Dispose();
}