gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:18:11 24 4
gpt4 key购买 nike

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

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

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

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

谢谢

最佳答案

编辑:时代变了,今天更多地使用 Linq,异步编程接管了。我重写了代码并使用 .Net Core 和 Beckhoff.TwinCAT.Ads NuGet。此代码在端口 851 连接到 localhost PLC 并写入 100 bool 数组。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 个 bool 数组:

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 写入一个数组。

关于c# - 通过 ADS.Net 将数组从 C# 发送到 TwinCat 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44700092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com