我在Windows SP2上使用SP2 I/O 2.11。使用.NET库"TwinCAT.ADS“,我已经成功地读取和写入了输入/输出(从EtherCAT总线上的设备映射对象)。当然,这只在TwinCAT处于Freerun或实时模式时才有效。
我现在的问题是:我可以手动设置TwinCAT模式。,但如何设置TwinCat模式(Freerun,Realtime,.)使用Win32程序?,我在TwinCAT.ADS中找不到合适的函数。存在这样的函数吗?
发布于 2014-05-08 07:21:51
我找到了一个解决方案:名称空间TCatSysManagerLib (来自文件C:\TwinCAT\Io\AddIns\TCatSysManagerLib.dll)提供了一个名为ITcSysManager的类,它使您能够从TwinCAT访问系统管理器函数。
发布于 2015-11-05 15:56:43
我只对TwinCAT3有一些经验,但是库是一样的。
在TwinCAT3中,不同的模式有、RUN、和配置。要在两种模式之间切换,您将需要TCatSysManagerLib,这是正确的。但是没有(特别是对于TwinCAT3)切换回配置模式的方法(yet...but贝克霍夫正在研究它)。
如果您想这样做,您将需要通过AmsPort.SystemService,即10000。
下面是一个如何从RUN模式切换到CONFIG模式的示例:
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();
}在这里,从配置到运行模式(使用TCatSysManagerLib)的相反方式是:->仅适用于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();
}或者(我没有试过):
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();
}发布于 2018-08-30 13:41:48
我实现了一个基于@ChrisKo的答案的解决方案。下面的WinForms代码操作的前提是,存在一个名为Form1的表单,它有三个名为_runButton、_configButton和_exitButton的按钮。该项目应该有一个参考TwinCAT.Ads.dll。
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using TwinCAT.Ads;
namespace TwinCatStateREader
{
public partial class Form1 : Form
{
private TcAdsClient _tcClient;
private TcAdsClient _tcSystemClient;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
_tcClient = new TcAdsClient();
_tcClient.Connect(851);
_tcSystemClient = new TcAdsClient();
_tcSystemClient.Connect((int)AmsPort.SystemService);
if (_tcSystemClient.ReadState().AdsState != AdsState.Run)
{
_configButton.Enabled = false;
_runButton.Enabled = true;
}
else
{
_runButton.Enabled = false;
_configButton.Enabled = true;
}
}
catch (AdsErrorException ex)
{
MessageBox.Show(ex.Message);
}
}
private void _exitButton_Click(object sender, EventArgs e)
{
Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
_tcClient.Disconnect();
_tcClient.Dispose();
_tcSystemClient.Disconnect();
_tcSystemClient.Dispose();
}
catch (AdsErrorException ex)
{
MessageBox.Show(ex.Message);
}
}
private void _configButton_Click(object sender, EventArgs e)
{
_configButton.Enabled = false;
_tcSystemClient.WriteControl(new StateInfo(AdsState.Reconfig, _tcSystemClient.ReadState().DeviceState));
if (WaitForState(AdsState.Config, 3000))
{
_runButton.Enabled = true;
}
else
{
_configButton.Enabled = true;
}
}
private void _runButton_Click(object sender, EventArgs e)
{
_runButton.Enabled = false;
_tcSystemClient.WriteControl(new StateInfo(AdsState.Reset, _tcSystemClient.ReadState().DeviceState));
if (WaitForState(AdsState.Run, 3000))
{
_configButton.Enabled = true;
}
else
{
_runButton.Enabled = true;
}
}
private bool WaitForState(AdsState state, int timeOutInMilliSeconds)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
while (stopwatch.ElapsedMilliseconds <= timeOutInMilliSeconds)
{
try
{
if (_tcSystemClient.ReadState().AdsState == state)
{
return true;
}
}
catch (AdsErrorException)
{
// This can happen while ADS changes state and we try to read it
}
finally
{
Thread.Sleep(20);
}
}
stopwatch.Stop();
return false;
}
}
}请注意,此解决方案使用两个连接到不同端口的ads客户端(851 / 10000)
https://stackoverflow.com/questions/23513402
复制相似问题