通过 C# 应用程序使用 TwinCAT.Ads 编写结构数组

V P*_*V P 4 c# twincat twincat-ads twincat-ads-.net

我正在使用 TwinCAT.Ads (TwinCAT 3) 通过 c# 应用程序进行 Beckhoff plc 通信。应用程序正在读取和写入少量 plc 变量。我收到一个错误:

“无法封送对象。参数名称:值”

在编写结构变量数组时。但是,应用程序正在读取它而没有任何错误。任何帮助将不胜感激。下面是我的代码示例。

Plc中的结构

TYPE Station :
    STRUCT
        ClusterID   : STRING[10];
        Tech_Type   : USINT;
        Status      : BOOL;
        Reject      : BOOL;
        Rej_Detail  : STRING[50];
        Rej_Catagory : USINT; 
    END_STRUCT
END_TYPE
Run Code Online (Sandbox Code Playgroud)

C#中的类

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public class Station
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
    public string ClusterID;
    public byte Tech_Type;
    [MarshalAs(UnmanagedType.I1)]
    public bool Status;
    [MarshalAs(UnmanagedType.I1)]
    public bool Reject;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 51)]
    public string Rej_Detail;
    public byte Rej_Catagory;
}
Run Code Online (Sandbox Code Playgroud)

我正在用下面的代码编写,其中handles[0]是变量句柄,stations是长度为 5 的类数组。

adsClient.WriteAny(handles[0], stations, new int[] { 5 });
Run Code Online (Sandbox Code Playgroud)

Mon*_*Zhu 5

我猜你错过了 PLC 中的对应物。请确保在您的 PLC 中,您已经声明了一系列类似以下内容的站:

// I have it in a global variable list named: STG_Variables
stat_array_Var : array [0..5] of Station;
Run Code Online (Sandbox Code Playgroud)

这个 C# 代码对我有用:

TcAdsClient AdsComClient = new TcAdsClient();
AdsComClient.Connect(NetID_TwinCat, 851);

int handle_array = AdsComClient.CreateVariableHandle("STG_Variables.stat_array_Var");

// get some test stations:
Station station = new Station();
Station station2 = new Station();
Station station3 = new Station();
Station station4 = new Station();
Station station5 = new Station();

Station[] station_plural = new Station[] { station, station2, station3, station4, station5 };

// write some stuff to recognize that write test worked
for (int i = 0; i < station_plural.Length; i++)
{
    station_plural[i].ClusterID = "ID: " + i.ToString();
}

// just use the normal WriteAny method without the new int[] { 5 } parameter!
// send it down to the plc
AdsComClient.WriteAny(handle_array, plural);
Run Code Online (Sandbox Code Playgroud)

我不知道你的手柄handles[0]指向哪里。编写一个数组不Station应该以 plc 中的单个结构结束。试试我的版本,请评论它是否适合你。

编辑:我在 C# 中使用了这个类定义:

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public class Station
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
    public string ClusterID;
    public byte Tech_Type;
    [MarshalAs(UnmanagedType.I1)]
    public bool Status;
    [MarshalAs(UnmanagedType.I1)]
    public bool Reject;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 51)]
    public string Rej_Detail;
    public byte Rej_Catagory;
}
Run Code Online (Sandbox Code Playgroud)

并创建了一个DUT并在 PLC 中使用了这个结构定义:

TYPE Station :
STRUCT
    ClusterID   : STRING[10];
    Tech_Type   : USINT;
    Status      : BOOL;
    Reject      : BOOL;
    Rej_Detail  : STRING[50];
    Rej_Catagory : USINT; 
END_STRUCT
END_TYPE
Run Code Online (Sandbox Code Playgroud)

Station如上所述声明了s的数组变量。

它有效。我能写的结构到PLC,看到了"ID: 0""ID: 1""ID: 2"等数组中的字符串