Skip to content

Latest commit

 

History

History
47 lines (37 loc) · 1.73 KB

File metadata and controls

47 lines (37 loc) · 1.73 KB

ANY In & Output

(FunWithANY_BIT.cs, FunWithANY_NUM.cs, FB1WithANY_BIT.cs, FB1WithANY_NUM.cs, FB2WithANY_BIT.cs, FB2WithANY_NUM.cs)

The FunWithANY examples show, how to use the IEC 61131-3 ANY parameter in a C# function.
First we have the party optional attribute DataType(). This attribute is only necessary if you have an IEC type as in/output, which does not have a unique mapping to a C# data type. For example, uint in C# is UDINT in IEC. But if you want an input to be of data type DWORD, which is uint in C#, the DataType("DWORD") attribute is needed. For ANY parameters, the DataType() attribute must always be used. For more information on data type mapping, read the Readme.txt, available in every Visual Studio C# PLCnext project.

[Function, DataType("ANY_NUM")]
public static class Fun_with_ANY_NUM
{

In Functions all ANY parameters must be passed by reference.

    [Execution]
    public unsafe static ushort __Process(
        [Input, DataType("ANY_NUM")] ref Any VALUE,
        [Input, DataType("ANY_NUM")] ref Any MIN,
        [Input, DataType("ANY_NUM")] ref Any MAX)
    {
    ...

Use the runtime type handle to identify the element type.

Eclr.TypeCode code = (Eclr.TypeCode)Eclr.TypeInfo.GetTypeCode(VALUE.pRuntimeTypeHandle);

Use the member nLength of VALUE to determine its size.

uint SIZE_OF_VALUE = VALUE.nLength;

FB1WithANY is an example of using ANY in a function block. There, you don't need to reference your fields. But ANY as an Output parameter is not supported. For this, the FB2WithANY shows how to use an InOut parameter instead.