VS 2015 - Beckhoff AI脚本 - 错误:'System.Runtime.InteropServices.COMException' [英] VS 2015 -- Beckhoff AI script -- ERROR : 'System.Runtime.InteropServices.COMException'

查看:120
本文介绍了VS 2015 - Beckhoff AI脚本 - 错误:'System.Runtime.InteropServices.COMException'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我是EnvDTE的新手,可以自动设置项目配置。

I'm very new to EnvDTE to automatically setup project configurations.

我需要在Beckhoff PLC上自动配置程序。为此,Beckhoff有以下指南:

I need to make a application with Beckhoff PLC where the program is automatically configured. For this Beckhoff has the following guide:

https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_automationinterface/54043196246263435.html&id=

当我尝试使用c#代码时,visual studio启动了。启动后,我收到错误: 

When I try the c# code, visual studio starts up. After the startup I get the error: 

System.Dynamic.dll中出现未处理的"System.Runtime.InteropServices.COMException"异常例外。
附加信息:找不到指定的模板。请检查完整路径是否正确。

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Dynamic.dll
Additional information: The template specified cannot be found. Please check that the full path is correct.

在以下代码行中: 

At the following line of code : 

string template = @"C:\TwinCAT\3.1\Components\Base\PrjTemplate\TwinCAT Project.tsproj";                                         //path to project template
dynamic project = solution.AddFromTemplate(template, @"C:\Temp\VS_TC2\SolutionFolder\BAI", "projTemp");

模板正站在正确的位置。所以这一切都应该没问题。

The template is standing on the correct location. So all of this should be ok I think.

完整代码下方:

using System;
using System.IO;
using TCatSysManagerLib;



namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("********Auto load config Twincat********");
            Console.WriteLine("Start");


            // START VISUAL STUDIO 2015
            Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.14.0");                                                                                                       // PROGID Visual studio 2017 is 15.0
            Console.WriteLine("presenting the type:" + t);
            EnvDTE.DTE dte = (EnvDTE.DTE)Activator.CreateInstance(t);
            dte.SuppressUI = false;
            dte.MainWindow.Visible = true;
            // ADD PROJECT DIRECTORY
            if (Directory.Exists(@"C:\Temp\VS_TC2\SolutionFolder"))
                Directory.Delete(@"C:\Temp\VS_TC2\SolutionFolder", true);
            Directory.CreateDirectory(@"C:\Temp\VS_TC2\SolutionFolder");
            Directory.CreateDirectory(@"C:\Temp\VS_TC2\SolutionFolder\BAI");
            dynamic solution = dte.Solution;
            solution.Create(@"C:\Temp\VS_TC2\SolutionFolder", "MySolution1");
            solution.SaveAs(@"C:\Temp\VS_TC2\SolutionFolder\BAI\BAI.sln");
            string template = @"C:\TwinCAT\3.1\Components\Base\PrjTemplate\TwinCAT Project.tsproj";                                         //path to project template
            dynamic project = solution.AddFromTemplate(template, @"C:\Temp\VS_TC2\SolutionFolder\BAI", "projTemp");
            ITcSysManager sysManager = project.Object;
            sysManager.ActivateConfiguration();
            sysManager.StartRestartTwinCAT();
            project.Save();
            solution.SaveAs(@"C:\Temp\VS_TC2\SolutionFolder\MySolution1\MySolution1.sln");
            Console.ReadLine();
        }
    }
}

任何人都可以引导我进入正确的方向?

Can anybody guide me into the correct direction?

提前致谢,

亲切的问候,

Toon

推荐答案

这是创建解决方案的一种非常奇怪的方式。看起来tsproj只是一个普通的项目文件,所以你不应该真的需要这么做。但是我假设您安装的这个TwinCAT产品已经与VS集成了吗?

That is a really odd way to create a solution. It appears that the tsproj is just a regular project file so you shouldn't really need to do all this. But I'm assuming that this TwinCAT product you installed integrates with VS already right? 

让我们开始简单,打开VS,然后创建一个新项目。在可用模板列表中查找此TwinCAT项目模板。如果找到,则选择它,为项目命名并创建。你应该完成。无需运行此脚本。

Let's start simple, open VS and then create a new project. Look for this TwinCAT project template in the list of available templates. If it is found then select it, give your project a name and create. You should be done. No need to run this script.

如果没有自动创建项目的选项,则改为创建一个空白解决方案。然后将项目目录从TwinCAT(见下文)复制到解决方案文件夹。最后将现有项目添加到解决方案中。然后看看它是否构建并运行。

If there is no option to create the project automatically then create a blank solution instead. Then copy the project directory from TwinCAT (see below) to the solution folder. Finally add the existing project to the solution. Then see if it builds and runs.

如果仍然无效,那么这个hacky解决方案应该只需要一次。如果您按照上面的方法2进行操作,那么您已经完成了脚本的前半部分。只需加载解决方案文件(而不是创建
,保存它然后添加项目模板,下半部分应该可行。

If that still doesn't work then this hacky solution should only really be needed once. If you followed approach 2 above you've already done the first half of the script. The second half should be doable just by loading the solution file (instead of creating it, saving it and then adding the project template.

关于你看到的问题 - " 找不到指定的模板。请检查完整路径是否正确。"

On to the issue you're seeing - "The template specified cannot be found. Please check that the full path is correct."

这意味着您指定的模板文件找不到(`template`)。你安装了这个TwinCAT 3.1产品吗?路径是否正确?只有你能回答这些问题。 

That means the template file you specified (`template`) cannot be found. Did you install this TwinCAT 3.1 product? Is the path correct? Only you can answer these questions. 

接下来的事情很奇怪事实上,模板不是项目文件。你的路径指向`tsproj`文件,但模板是带有`vsdir`扩展名的zip文件。模板内部是VS用来加载它们的元数据文件$ b UI中的$ b和项目文件以及任何其他支持文件。要从模板添加项目,您应该使用`vsdir`文件的路径。 

The next thing that is odd about this is the fact that a template is not a project file. You're path is pointing to a `tsproj` file but templates are zip files with extensions like `vsdir`. Inside the template are metadata files that VS uses to load them in the UI and the project file and any additional support files. To add a project from a template you should be using the path to the `vsdir` file. 


这篇关于VS 2015 - Beckhoff AI脚本 - 错误:'System.Runtime.InteropServices.COMException'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆