通过ADS.Net从C#发送数组到TwinCat 3通过ADS.Net
问题描述:
我想使用TwinCat 3控制阀门和Visual Studio C#来处理想要在喷泉上显示的图像的自动图形喷泉。通过ADS.Net从C#发送数组到TwinCat 3通过ADS.Net
图像处理程序的最终形式是二进制阵列图像(附件): Image Processing Result 1; Image Processing Result 2;
我想用图像处理的最终形式来控制机器上的阀门(阀门在1时打开,当阀门为0时阀门将关闭)。我对TwinCat 3非常新颖特别是与ADS库。
来自infosys beckhoff的示例对我没有什么帮助,有人可以帮助我吗?
谢谢
答
我做了在端口851连接到本地PLC和写入100个布尔变量命名为 “boolArray” 在MAIN TC3的阵列(3的TwinCAT)样本控制台程序:
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)
你尝试过什么到目前为止送呢?在infosys上的示例是有帮助的... –
实际上,我正在尝试将数组写入TwinCat 3.在infosys上,有一个示例从twincat 3读取数组,但没有示例如何从twincat 3写入数组。 –
我发现示例9对我非常有帮助,现在我试图弄清楚如何在我的项目中实现此示例。 –