通过 ADS.Net 将数组从 C# 发送到 TwinCat 3

Cal*_*eph 3 .net c# plc visual-studio twincat-ads

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

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

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

infosys beckhoff 的样本对我没有帮助,有人可以帮我吗?

谢谢你

Ily*_*Dan 5

编辑:时代变了,今天更多的 Linq 被使用,异步编程接管了。我已经重写了代码,并在 Beckhoff.TwinCAT.Ads NuGet 中使用了 .Net Core。此代码在端口 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

PLC代码:

PROGRAM MAIN
VAR
    boolArray           : ARRAY [0..99] OF BOOL;
END_VAR
Run Code Online (Sandbox Code Playgroud)

原始答案:我制作了一个示例控制台程序,它在端口 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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码很好地代表了向 TC3 写入一个数组。