Hello,
I have found some similar issues in the forums but I cannot seem to find an exact answer to my question regarding this issue.
My problem is I want to define a C# array for example bool myArray[2] and then connect the seperate variables to actual IO in GDS Port List in PLCnext Engineer.
For example:
myArray[0] → Digital_Output1
myArray[1] → Digital_Output2
What I have done is copy the following code from the C# Examples in Github and then declare my array like this into my C# PROGRAM:
[Global, OutputPort, DataType(“IntArrayFB”)]
public IntArrayFB IN_ARRAY;
This is only a test “Int” Array before I try to do what I previously explained.
After that I add my library to the PLCnext Engineer but in the GDS Port I see the following:
image.pngThere appear to be 3 weird errors. Also I only see “IN_ARRAY” instead of two seperate variables “IN_ARRAY[0]” and “IN_ARRAY[1]” and when I press the “IN PORT” no plc variables appear to connect them into it. Have I not understood something correctly?
Thank you in advance once again for your time and effort you put into my issue.
C# Code:
// The IecArray must be defined as a struct with a fixed size.
// The array definition MUST have following Attributes:
// 1. Array (Actually only one-dimensional arrays are supported by the PCWorx Engineer)
// 2. ArrayDimension
// 3. DataType to define the data type of the array elements
[Array(1), ArrayDimension(0, ArrayProperties.LowerBound, ArrayProperties.UpperBound), DataType(“DINT”)]
[StructLayout(LayoutKind.Explicit, Size = ArrayProperties.ByteSize)]
public struct IntArrayFB
{
// Helper containing constants to have a
// clear and maintainable definition for boundaries and size
private struct ArrayProperties
{
public const int LowerBound = 0; // must not necessarily being zero, it also can be negative
public const int UpperBound = 1; // IEC61131 representation is : userArray : ARRAY[-10..9] OF DINT (* size == 20 *)
// the size must be changed to the correct size of your elements times the amount of elements
public const int ByteSize = (UpperBound - LowerBound + 1) * sizeof(int);
}
public const int ByteSize = ArrayProperties.ByteSize;
// Fields
// The field “Anchor” defines the beginning of the array.
[FieldOffset(0)]
// The Anchor’s data type is the child data type of the array
public int Anchor;
// The constants LB and UB define the upper and lower bound. Boundaries will be checked by using them.
public const int LB = ArrayProperties.LowerBound;
public const int UB = ArrayProperties.UpperBound;
public int this[int index]
{
get
{
if (index >= (LB - ArrayProperties.LowerBound) && index <= (UB - ArrayProperties.LowerBound))
{
unsafe
{
fixed (int* pValue = &Anchor)
{
int result = (pValue + index);
return result;
}
}
}
else
{
throw new IndexOutOfRangeException();
}
}
set
{
if (index >= (LB - ArrayProperties.LowerBound) && index <= (UB - ArrayProperties.LowerBound))
{
unsafe
{
fixed (int pValue = &Anchor)
{
*(pValue + index) = value;
}
}
}
else
{
throw new IndexOutOfRangeException();
}
}
}
}
[Global, OutputPort, DataType(“IntArrayFB”)]
public IntArrayFB IN_ARRAY;



