简体   繁体   English

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

[英]How to get symbols from TwinCAT in windows 10?

I have to use Beckhoff I/O in real-time mode (not PLC project), because the final goal is to make a Soft-PLC, where the program runs on the cumputer and not on Beckhoff.我必须在实时模式下使用 Beckhoff I/O(不是 PLC 项目),因为最终目标是制作一个 Soft-PLC,其中程序在计算机上运行而不是在 Beckhoff 上运行。 So in Visual Studio I wrote ac# code which opens an existing TwinCAT project in background (with a task and some variables linked), activates the configuration and then gets the symbols from that task, so I can use them to control in realtime the variables (eg reading an input or writing an output).所以在 Visual Studio 中,我编写了 ac# 代码,它在后台打开一个现有的 TwinCAT 项目(带有一个任务和一些链接的变量),激活配置,然后从该任务中获取符号,这样我就可以使用它们来实时控制变量(例如读取输入或写入输出)。 The problem is I can't get the symbols!问题是我无法获得符号! (Im using the SymbolLoaderFactory.Create method). (我使用 SymbolLoaderFactory.Create 方法)。 I created a class "Controlling" in the same namespace which contains some methods like GoConfig(), Open(), Activate() etc..我在同一个命名空间中创建了一个类“Controlling”,其中包含一些方法,如 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());

All projects I created to test this had 2 variables linked to the task.我创建的所有测试项目都有 2 个链接到任务的变量。 Everything works fine until the SymbolsGetter method.一切正常,直到 SymbolsGetter 方法。 From the Controlling class:从控制类:

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;
            }
        }
    

The Thread.Sleep is because otherwise the client.Connect generated an error. Thread.Sleep 是因为否则 client.Connect 会产生错误。 It only works in debug if I add a break point on "return symbolLoader" and if I pass over it with the mouse puntator to see the value (of symbolLoader).如果我在“返回symbolLoader”上添加一个断点,并且如果我用鼠标puntator越过它以查看(symbolLoader的)值,它仅在调试中有效。 I tried to insert another sleep but nothing, it doesn't work.我试图插入另一个睡眠,但没有,它不起作用。 Im actually starting thinking it is black magic.我实际上开始认为这是黑魔法。 Another problem is that I can't get symbols from all projects (nether using the debug technique of before).另一个问题是我无法从所有项目中获取符号(使用之前的调试技术)。 And even when it works I can't use the name of the symbol to write a high/low signal to that variable.即使它有效,我也不能使用符号的名称将高/低信号写入该变量。 So nothing is working, I'm just able to create and open a project, a task etc. Just one more information: im using windows 10 (maybe TwinCAT works better on windows 7 or xp, but I have to make it work on windows 10).所以什么都不起作用,我只能创建和打开一个项目、一个任务等。还有一个信息:我使用的是 windows 10(也许 TwinCAT 在 windows 7 或 xp 上运行得更好,但我必须让它在 windows 上运行10)。 I have to do everything without touching or seeing the TwinCAT project window, it has to stay in background.我必须在不接触或看到 TwinCAT 项目窗口的情况下完成所有操作,它必须留在后台。 I'm not a programmer and I'm quite new into the Beckhoff/TwinCAT world.我不是程序员,我对 Beckhoff/TwinCAT 的世界还很陌生。 Please if anyone can help me I'd be incredibly thankful to him.请如果有人能帮助我,我会非常感谢他。

If you want to use the ADS library from beckhoff you cannot not have a Beckhoff PLC-Runtime in the background.如果您想使用 beckhoff 的 ADS 库,您不能在后台没有Beckhoff PLC-Runtime。

With the ADS-lib you access the symbols in the PLC Runtime which has an Ethercat Master with which it can communicate with the Beckhoff Ethercat IOs.使用 ADS-lib,您可以访问 PLC 运行系统中的符号,该系统具有一个 Ethercat 主站,可以与 Beckhoff Ethercat IO 进行通信。

If you do not want to use a Beckhoff Plc-Runtime (and it's not really clear to me why you wouldn't want to) you need another kind of Ethercat Master in order to communicate with the IOs.如果您不想使用 Beckhoff Plc-Runtime(我不太清楚您为什么不想使用),您需要另一种 Ethercat Master 来与 IO 进行通信。

I have to use Beckhoff I/O in real-time mode (not PLC project), because the final goal is to make a Soft-PLC, where the program runs on the cumputer and not on Beckhoff.我必须在实时模式下使用 Beckhoff I/O(不是 PLC 项目),因为最终目标是制作一个 Soft-PLC,其中程序在计算机上运行而不是在 Beckhoff 上运行。

This is confusing, because the Beckhoff runtime runs on computer besides windows.这令人困惑,因为 Beckhoff 运行时运行在 Windows 之外的计算机上。 If you want to control IOs without real time capability you can buy an Advantech PCI IO card for example and use their c# sdk.如果您想在没有实时功能的情况下控制 IO,您可以购买 Advantech PCI IO 卡并使用他们的 c# sdk。

You can't use c# to access Beckhoff IOs without a Beckhoff runtime in the background.如果后台没有 Beckhoff 运行时,您就无法使用 c# 访问 Beckhoff IO。 Twincat doesn't work better on windows 7 or xp in fact Twincat 3 is not even supported on windows xp and new machines only use windows 10 for security and support reasons. Twincat 在 windows 7 或 xp 上的工作效果并不好,事实上,windows xp 甚至不支持 Twincat 3,新机器出于安全和支持原因只使用 windows 10。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM