我想使用 TwinCat 3 来控制阀门和 Visual Studio C# 来处理想要在喷泉上显示的图像来制作一个自动图形喷泉。
图像处理程序的最终形式是二进制数组图像(附): 图像处理结果1; 图像处理结果2;
我想使用图像处理的最终形式来控制机器上的阀门(阀门为 1 时打开,为 0 时阀门关闭)。我对 TwinCat 3 非常新,尤其是 ADS图书馆。
infosys beckhoff 的样本对我没有帮助,有人可以帮我吗?
谢谢你
编辑:时代变了,今天更多的 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();
}
}
}
可编程逻辑控制器代码:
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。
我使用System.Runtime.InteropServices.
MarshalAs
的是 TwinCAT 3.1.4022.0
数组声明:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] data;
然后我可以通过
TcAdsClient.WriteAny( ixGroup, ixOffset, data )