如何在Windows 10中从TwinCAT获取符号?

问题描述

我必须在实时模式下使用Beckhoff I / O(不是PLC项目),因为最终目标是制作一个Soft-PLC,该程序在计算机而不是Beckhoff上运行。因此,在Visual Studio中,我编写了ac#代码,该代码后台打开一个现有的TwinCAT项目(带有链接的任务和一些变量),激活配置,然后从该任务获取符号,因此我可以使用它们实时控制变量(例如,读取输入或写入输出)。问题是我无法获得符号! (我使用SymbolLoaderFactory.Create方法)。我在同一命名空间中创建了一个“控制”类,其中包含一些方法,例如GoConfig(),Open(),Activate()等。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EnvDTE;
using System.IO;
using TCatSysManagerLib;
using System.Management;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Collections;
using System.Xml;
using TwinCAT.Ads;
using TwinCAT.Ads.ValueAccess;
using TwinCAT.Ads.TypeSystem;
using TwinCAT.TypeSystem;


namespace TCControl
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            
            string solName = "solution1"; 
            string solPath = $@"B:\...solutionPath...\{solName}";

            string[] terminal = new string[1];
            terminal[0] = "EK1100";

            string ipAddress = "10.157.4.69.1.1";
            int port = 301;

            Controlling co = new Controlling();
            MessageFilter.Register();
            

            //go to configuration mode
            co.GoConfig(ipAddress);

            
            //open the solution
            string[,] treeItemsandTypes = co.Open(solPath,solName,ipAddress,terminal);
            co.Activate(solPath,solName);
            
            //obtain symbols
            ISymbolLoader symbolLoader = co.SymbolsGetter(ipAddress,port);

            ISymbol[] symbols = new ISymbol[symbolLoader.Symbols.Count];
            
            int i = 0;
            foreach (ISymbol symbol in symbolLoader.Symbols)
            {
                symbols[i] = symbol;
                i++;
            }
            
            Console.WriteLine("\n" + symbols[0].ToString());
            Console.WriteLine("\n" + symbols[1].ToString());

我为此测试创建的所有项目都有2个与任务链接的变量。 一切正常,直到使用SymbolsGetter方法。在控制类中:

public ISymbolLoader SymbolsGetter(string indirizzo,int port)
        {
            using (TcAdsClient client = new TcAdsClient())
            {
                System.Threading.Thread.Sleep(2000);
                client.Connect(indirizzo,port);

                //Creates the Symbol Objects as hierarchical tree
                SymbolLoaderSettings settings = new SymbolLoaderSettings(SymbolsLoadMode.Flat,ValueAccessMode.IndexGroupOffsetPreferred);

                ISymbolLoader symbolLoader = SymbolLoaderFactory.Create(client,settings);

                //System.Threading.Thread.Sleep(2000);
                return symbolLoader;
            }
        }
    

Thread.Sleep是因为否则client.Connect生成错误。 仅当在“ return symbolLoader”上添加断点并且使用鼠标撑开器将其越过以查看(symbolLoader的)值时,它才在调试中起作用。 我试图插入另一个睡眠,但是什么也没有,它不起作用。我实际上开始认为这是黑魔法。 另一个问题是我无法从所有项目中获得符号(也不使用以前的调试技术)。即使它工作了,我也无法使用符号的名称向该变量写入高/低信号。所以什么都没用,我只能创建和打开项目,任务等。 仅提供一个信息:我使用Windows 10(也许TwinCAT在Windows 7或XP上工作更好,但我必须使其在Windows 10上工作)。 我必须在不触摸或看不到TwinCAT项目窗口的情况下做所有事情,它必须留在后台。我不是程序员,并且对Beckhoff / TwinCAT世界还很陌生。请任何人帮助我,我将非常感谢他。

解决方法

如果要使用Beckhoff的ADS库,请您不能在后台运行Beckhoff PLC运行系统。

使用ADS-lib可以访问PLC运行时中的符号,该运行时具有Ethercat主站,可以与Beckhoff Ethercat IO进行通信。

如果您不想使用Beckhoff Plc-Runtime(并且我现在还不清楚为什么不愿意),则需要另一种Ethercat Master才能与IO进行通信。

我必须在实时模式下使用Beckhoff I / O(不是PLC项目),因为最终目标是制作一个Soft-PLC,该程序在计算机而不是Beckhoff上运行。

这令人困惑,因为Beckhoff运行时在Windows之外的计算机上运行。 如果要控制没有实时功能的IO,可以购买例如研华PCI IO卡并使用其c#sdk。

在后台没有Beckhoff运行时的情况下,不能使用c#访问Beckhoff IO。 Twincat在Windows 7或XP上无法更好地工作,实际上Windows XP甚至不支持Twincat 3,并且出于安全和支持的原因,新机器仅使用Windows 10。