Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过C#应用程序使用TwinCAT.Ads编写结构数组_C#_Twincat_Twincat Ads_Twincat Ads .net - Fatal编程技术网

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

通过C#应用程序使用TwinCAT.Ads编写结构数组,c#,twincat,twincat-ads,twincat-ads-.net,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;

我使用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
c类#

我用下面的代码编写,句柄[0]是可变句柄,是长度为5的类数组

adsClient.WriteAny(handles[0], stations, new int[] { 5 });

我猜你错过了PLC中的对手。请确保您在PLC中声明了一组站点,如:

// I have it in a global variable list named: STG_Variables
stat_array_Var : array [0..5] of Station;
这个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);
创建一个
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
并如上所述声明
s的数组变量


它是有效的。我能够将结构写入PLC,并查看阵列中的
“ID:0”
“ID:1”
“ID:2”
等字符串

您可以在PLC中发布站点阵列的代码吗?另外,PLC中变量的声明和C#code.Pack=0中的句柄也是毫无意义的,请删除它。感谢Mong Zhu的回复。我在PLC中定义了那个数组变量,但忘了在文章中提到。而且
句柄[0]
指向正确的数组变量。正如我在文章中提到的,代码正确读取值,但在编写时抛出错误。我发现我在封送结构属性时缺少或做错了什么。请你尝试一下和我一样的结构定义好吗?我确实尝试了和你一样的结构定义,请看我的编辑。我在你的帖子中没有看到任何更新。你能帮我确认一下吗?@VP对不起,是一个小懒汉,他把所有的东西都保持原样,就像我在实际代码中一样,只是删除了最后一个参数
newint[]{5}
,它就工作了。我把最后一个参数误认为是数组的长度。该参数在写入固定字符串值时非常有用,并且必须在写入操作期间提供该参数。感谢蒙珠的帮助。
[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;
}
TYPE Station :
STRUCT
    ClusterID   : STRING[10];
    Tech_Type   : USINT;
    Status      : BOOL;
    Reject      : BOOL;
    Rej_Detail  : STRING[50];
    Rej_Catagory : USINT; 
END_STRUCT
END_TYPE