【问题标题】:Send an array from C# to TwinCat 3 via ADS.Net通过 ADS.Net 将数组从 C# 发送到 TwinCat 3
【发布时间】:2017-06-22 12:57:37
【问题描述】:

我想制作一个自动图形喷泉,使用 TwinCat 3 来控制阀门和 Visual Studio C# 来处理想要在喷泉上显示的图像。

图像处理程序的最终形式是二进制数组图像(附): Image Processing Result 1; Image Processing Result 2;

我想用图像处理的最终形式来控制机器上的阀门(阀门为1时打开,为0时阀门关闭)。我对TwinCat 3非常陌生,尤其是使用 ADS 库。

infosys beckhoff 的示例对我没有帮助,有人可以帮助我吗?

谢谢

【问题讨论】:

  • 到目前为止您尝试过什么? infosys 上的示例很有帮助...
  • 实际上,我正在尝试将数组写入 TwinCat 3。在 infosys 上,有一个从 twincat 3 读取数组的示例,但没有一个如何从 twincat 3 写入数组的示例。跨度>
  • 我发现示例9对我很有帮助,现在我正在尝试弄清楚如何在我的项目中实现这个示例。

标签: c# .net visual-studio plc twincat-ads


【解决方案1】:

编辑: 时代变了,今天更多的 Linq 被使用,异步编程接管了。 我已经重写了代码并将 .Net Core 与 Beckhoff.TwinCAT.Ads NuGet 结合使用。 此代码在端口 851 处连接到本地 PLC,并写入 100 布尔数组。 PLC 中的数组位置为“MAIN.boolArray”。

using System;
using System.Linq;
using System.Threading.Tasks;
using TwinCAT.Ads;

namespace ArrayWrite
{
    class Program
    {
        static  void Main(string[] args)
        {
            WriteBoolArray(100).Wait();
        }

        private static async Task WriteBoolArray(int arrayLength)
        {
            byte[] boolArray = new byte[arrayLength];
            // Fill array with 010101010...
            boolArray = Enumerable.Range(0, arrayLength).Select(val => (val % 2 == 0) ? (byte)0 : (byte)1).ToArray();

            // Connect to PLC
            AdsClient client = new AdsClient();
            AmsAddress address = new AmsAddress("127.0.0.1.1.1", 851);
            client.Connect(address);

            // Get the handle for the array
            uint variableHandle;
            ResultHandle handleResult = await client.CreateVariableHandleAsync("MAIN.boolArray", default);
            if (handleResult.Succeeded)
            {
                variableHandle = handleResult.Handle;
            }
            else
            {
                Console.WriteLine($"Could not get handle. Error code: {handleResult.ErrorCode}. Press any key to exit");
                Console.ReadKey();
                return;
            }

            // Write the array
            ResultWrite writeResult = await client.WriteAnyAsync(variableHandle, boolArray, new int[] { arrayLength }, default);
            if (writeResult.Succeeded)
            {
                Console.WriteLine($"Write successful. Press any key to exit");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine($"Could not write variable. Error code: {writeResult.ErrorCode}. Press any key to exit");
                Console.ReadKey();
                return;
            }
            // In real code the exit should clean after the code like this:
            await client.DeleteVariableHandleAsync(variableHandle, default);
            client.Disconnect();
        }
    }
}

PLC 代码:

PROGRAM MAIN
VAR
    boolArray           : ARRAY [0..99] OF BOOL;
END_VAR

原答案: 我制作了一个示例控制台程序,该程序在端口 851 处连接到本地 PLC,并在 TC3(TwinCAT 3)的 MAIN 中写入名为“boolArray”的 100 个布尔数组:

using System;
using TwinCAT.Ads;
using System.Threading;

namespace WriteArrayToPLC
{
    
    class Program
    {
        static void Main(string[] args)
        {
            TcAdsClient adsClient = new TcAdsClient();
            byte[] boolArray = new byte[100];
            // Fill array with 010101010...
            for (int i = 0; i < 100; i++)
            {
                boolArray[i] = (i % 2 != 0) ? (byte)1 : (byte)0;
            }
            // Connect to PLC
            try
            {

                if (adsClient != null)
                {
                    Console.WriteLine("Connecting to PC");
                    adsClient.Connect(851);
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
                adsClient = null;
            }

            if (adsClient != null)
            {
                try
                {
                    // Get the handle for the array
                    int handle_array = adsClient.CreateVariableHandle("MAIN.boolArray");
                    // Write the array to PLC
                    Console.WriteLine("Writing the array at handle: " + handle_array.ToString());
                    adsClient.WriteAny(handle_array, boolArray);
                }
                catch (Exception err)
                {
                    Console.WriteLine(err.Message);
                }
                // The end
                Console.WriteLine("Done");
                Thread.Sleep(3000);
            }
        }
    }
}

这段代码很好地代表了将数组写入 TC3。

【讨论】:

    【解决方案2】:

    我使用System.Runtime.InteropServices.MarshalAs 和 TwinCAT 3.1.4022.0

    数组声明:

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public byte[] data;
    

    然后我可以通过发送它

    TcAdsClient.WriteAny( ixGroup, ixOffset, data )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-25
      • 2018-06-25
      • 2022-10-04
      • 1970-01-01
      • 2016-09-30
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多