You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TwinCAT3 Automation Interface如何读取VS2013中TwinCAT输出消息?

如何读取TwinCAT System/PlcTask类型的输出消息?

我来帮你解决这个问题:在TwinCAT 3.1.4024.4环境下用Automation Interface做项目构建和激活时,你会发现dte.ToolWindows.ErrorList.ErrorItems只能拿到前10条构建类消息,像TwinCAT System(10000)、PlcTask(350)这类系统/任务级的消息完全捕获不到。这是因为这些消息不属于VS Error List的范畴,它们要么存在于TwinCAT自身的日志系统,要么输出到VS Output窗口的专属面板里。

下面给你两种靠谱的解决方法:


方法1:用TwinCAT Automation Interface的日志接口(推荐)

TwinCAT官方提供了ITcSysLog接口,可以直接获取所有系统和任务日志,包括PLC测试的结果,这是最稳定的方案。

示例代码

// 确保引用了TwinCAT的COM组件:TcXaeShell、TcXaeManager等
using TwinCAT.SystemManager;

// 初始化SystemManager
ITcSystemManager sysMan = new TcSystemManager();
sysMan.Startup(@"你的解决方案路径", true);

// 执行激活和重启操作
sysMan.ActivateConfiguration();
sysMan.StartRestartTwinCAT();

// 获取并过滤TwinCAT日志
ITcSysLog sysLog = sysMan.SysLog;
// 只包含我们需要的日志源:TwinCAT System(10000)和PlcTask(350)
sysLog.Filter.SetFilter(10000, TcSysLogFilterMode.Include);
sysLog.Filter.SetFilter(350, TcSysLogFilterMode.Include);

// 遍历所有符合条件的日志条目
foreach (ITcSysLogEntry entry in sysLog.Entries)
{
    string logLine = $"{entry.Time:yyyy-MM-dd HH:mm:ss.fff} | '{entry.Source}' ({entry.SourceId}): {entry.Message}";
    Console.WriteLine(logLine);
    
    // 如果是错误类型,可以单独处理
    if (entry.Type == TcSysLogEntryType.Error)
    {
        Console.WriteLine($"⚠️ 错误级别日志: {logLine}");
    }
}

方法2:读取VS Output窗口的TwinCAT面板

如果你需要同时获取VS构建输出和TwinCAT的消息,可以直接读取VS Output窗口中对应的面板内容。

示例代码

using EnvDTE;
using System;

// 获取VS的Output窗口
Window outputWindow = dte.Windows.Item(Constants.vsWindowKindOutput);
OutputWindow ow = (OutputWindow)outputWindow.Object;

// 找到TwinCAT对应的输出面板(名称可能是"TwinCAT"或"TcXaeShell",视VS版本略有不同)
OutputWindowPane twinCatPane = null;
foreach (OutputWindowPane pane in ow.OutputWindowPanes)
{
    if (pane.Name.Equals("TwinCAT", StringComparison.OrdinalIgnoreCase))
    {
        twinCatPane = pane;
        break;
    }
}

// 读取并处理面板内容
if (twinCatPane != null)
{
    twinCatPane.Activate();
    // 获取面板全部文本
    string fullOutput = twinCatPane.TextDocument.StartPoint.CreateEditPoint().GetText(twinCatPane.TextDocument.EndPoint);
    Console.WriteLine("=== TwinCAT Output 内容 ===");
    Console.WriteLine(fullOutput);

    // 按行分割,过滤出目标消息
    string[] lines = fullOutput.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    foreach (string line in lines)
    {
        if (line.Contains("'TwinCAT System' (10000)") || line.Contains("'PlcTask' (350)"))
        {
            Console.WriteLine("\n📌 捕获到目标消息: " + line);
        }
    }
}

为什么原始代码不行?

你原来用ErrorItems获取的是VS的错误列表,这个列表只收录项目构建、代码分析这类和VS编译相关的有限条目,而TwinCAT的系统重启日志、PLC测试结果属于TwinCAT自身的运行时消息,不会被VS归类到Error List里,所以自然拿不到。


补充:你提供的原始信息

VS2013控制台输出片段

消息1:应用程序已更新 0 0
消息2:构建完成 -- 0错误,0警告:准备下载!0 0
...
消息11:2019年11月29日 09:52:58 477毫秒 | 'TwinCAT System' (10000): 已由AmsNetId: 10.73.8.239.1.1 端口32867请求重启TwinCAT系统。
...
错误23:2019年11月29日 09:53:02 108毫秒 | 'PlcTask' (350): 测试失败 'PRG_TEST.fbSum_Test@TwoPlusTwoEqualsFour',预期值:4,实际值:3,消息:计算不正确

Jenkins控制台输出片段

[仅能读取前10条消息]
ActivateConfiguration Errors count: 10
vsBuildErrorLevelLow: Description: 应用程序已更新
vsBuildErrorLevelLow: Description: 构建完成 -- 0错误,0警告:准备下载!
...
Exit code is 1

原始C#代码

//5.4.1 ITcPlcProject - Automation Interface文档
ITcSmTreeItem plcProjectRootItem = sysMan.LookupTreeItem("TIPC^UnitTestJenkins");
ITcPlcProject iecProjectRoot = (ITcPlcProject)plcProjectRootItem;
iecProjectRoot.BootProjectAutostart = true;
iecProjectRoot.GenerateBootProject(true);
sysMan.ActivateConfiguration();
sysMan.StartRestartTwinCAT();
ErrorItems ActivateConfigurationErrors = dte.ToolWindows.ErrorList.ErrorItems;
Console.WriteLine("ActivateConfiguration Errors count: " + ActivateConfigurationErrors.Count);
for (int i = 1; i <= ActivateConfigurationErrors.Count; i++)
{
    ErrorItem item = ActivateConfigurationErrors.Item(i);
    Console.WriteLine(item.ErrorLevel + ": " + "Description: " + item.Description);
}

内容的提问来源于stack exchange,提问作者Kumaraswamy Gaviyappa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:12:33