c# - 如何从 Windows 10 中的 TwinCAT 获取符号?

标签 c# windows real-time symbols twincat

我必须在实时模式下使用 Beckhoff I/O(不是 PLC 项目),因为最终目标是制作一个软 PLC,程序在计算机上运行,​​而不是在 Beckhoff 上运行。所以在 Visual Studio 中,我编写了一个 c# 代码,它在后台打开一个现有的 TwinCAT 项目(链接了一个任务和一些变量),激活配置,然后从该任务中获取符号,这样我就可以使用它们来实时控制变量(例如读取输入或写入输出)。问题是我无法获取符号! (我正在使用 SymbolLoaderFactory.Create 方法)。我在同一个命名空间中创建了一个类“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());

我创建的所有测试项目都有 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 的)值,它仅在调试中有效。 我试图插入另一个 sleep 但没有,它不起作用。我实际上开始认为这是黑魔法。 另一个问题是我无法从所有项目中获取符号(也无法使用之前的调试技术)。即使它有效,我也不能使用符号的名称向该变量写入高/低信号。所以什么都不起作用,我只能创建并打开一个项目、一个任务等。 还有一个信息:我正在使用 windows 10(也许 TwinCAT 在 windows 7 或 xp 上工作得更好,但我必须让它在 windows 10 上工作)。 我必须在不接触或看不到 TwinCAT 项目窗口的情况下做所有事情,它必须留在后台。我不是程序员,我对 Beckhoff/TwinCAT 世界还很陌生。如果有人可以帮助我,我将非常感谢他。

最佳答案

如果您想使用来自 beckhoff 的 ADS 库,您不能没有后台有一个 Beckhoff PLC-Runtime。

使用 ADS-lib,您可以访问 PLC Runtime 中的符号,它有一个 Ethercat 主站,它可以与 Beckhoff Ethercat IO 进行通信。

如果您不想使用 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 运行时除了在 Windows 之外还运行在计算机上。 如果您想控制没有实时功能的 IO,您可以购买 Advantech PCI IO 卡并使用他们的 c# sdk。

如果后台没有 Beckhoff 运行时,您就无法使用 C# 访问 Beckhoff IO。 Twincat 在 Windows 7 或 xp 上的工作效果并不好,事实上,Windows xp 甚至不支持 Twincat 3,出于安全和支持原因,新机器仅使用 Windows 10。

关于c# - 如何从 Windows 10 中的 TwinCAT 获取符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64700731/

相关文章:

c# - 无法从 'double?' 转换为 'decimal'

c# - 无效的资源目录名称: “res animation” aapt.exe

windows - 如何为 Windows 或 Mac 或 Linux 系统发布 Electron 应用程序?

c++ - 使用 C++ 进行机器人实时编程

algorithm - 检测时间序列数据的变化

while-loop - 同时停止多个 RT 定时循环的最佳方法?

c# - C# 中的 Convert.ToString() 和 .ToString() 方法

c# - 测试超出执行超时时间

Windows 批处理文件 - 在变量上查找字符串,查找包含百分比字符和单引号的字符串

c# - 如何以独立于区域设置的方式定位本地组?