I’m using TwinCAT.Ads (TwinCAT 3) for Beckhoff plc communication through c# application. Application is reading and writing few plc variables. I’m getting an error:
“Unable to marshal object. Parameter name: value”
while writing an array of struct variable. However application is reading it without any error. Any help will be appreciated. Below is my code sample.
Struct in 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
Class in 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;
}
I’m writing with below code where handles[0] is variable handle and stations is array of class with length of 5.
adsClient.WriteAny(handles[0], stations, new int[] { 5 });