如何从 TwinCat 读取包含结构数组的结构到 C#

How to read a struct containing array of structs from TwinCat to C#

使用 TwinCAT 3 ADS.Net 从 PLC 读取,我试图读取一个包含结构数组的结构,但是 ReadAny 命令崩溃并出现 "Unable to marshal type" 异常。

尽管直接读取结构数组工作正常。

public object ReadAny(long indexGroup, long indexOffset, Type type, int[] args);

ReadAny 方法的 header 注释说: “如果要读取的object的Type是数组类型,则每个维度的元素个数必须在参数args中指定。”

但是对于包含结构数组的结构,args 应该是什么? (没有 'args' 它也会失败。)

我目前使用 .NET 4.7,VS 2013。

有选择吗?

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public class WholeData
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
    public Station[] StationArray;
    // Potentially more fields...
}

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public class Station
{
    [MarshalAs(UnmanagedType.I1)]
    public bool isPass;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)]
    public string name;
    // More fields...
}

// -- Main --
int[] args = { 5 };

// Works fine:
Station[] stationArray = (Station[])m_AdsClient.ReadAny(indexGroup, indexOffset, typeof(Station[]), args);

// Fail:
WholeData wholeData = (WholeData)m_AdsClient.ReadAny(indexGroup, indexOffset, typeof(WholeData), args);
// - OR -
WholeData wholeData = (WholeData)m_AdsClient.ReadAny(m_VarHandle, typeof(WholeData), args);

我通过以下代码测试成功:

c#代码:

    class Program
    {
        public static TcAdsClient client;
        static void Main(string[] args)
        {


            // Create the ADS Client
            using (client = new TcAdsClient())
            {
                // Establish Connection
                client.Connect(new AmsAddress("10.1.2.95.1.1", 851));
                int handle = client.CreateVariableHandle("PRG_AIS.stAds");

                AdsClass ads = (AdsClass)client.ReadAny(handle, typeof(AdsClass));
                ads.boolArr[0] = 1;
                client.WriteAny(handle, ads);
                Console.ReadLine();

            }
        }
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    class AdsClass
    {

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public byte[] boolArr = new byte[10];
    }

ST代码:

TYPE AdsStruct :
STRUCT
    bTestArray : ARRAY[0..9] OF BOOL;
END_STRUCT
END_TYPE

AdsStruct 在 PRG_AIS 中被定义为 stAds。

或者如果您有一个结构数组,请按以下方式修改代码:

c#代码:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
class AdsClass
{

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public InnerStruct[] strArr = new InnerStruct[10];
}

struct InnerStruct
{
    public byte bBoolTest;
    public int nIntTest;
}

ST代码:

TYPE AdsStruct  :
STRUCT
    stTestArray : ARRAY[0..9] OF InnerStruct;
END_STRUCT
END_TYPE

TYPE InnerStruct :
STRUCT
    bBoolTest : BOOL;
    nIntTest  : DINT;
END_STRUCT
END_TYPE