-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFBWithUserArray.cs
More file actions
162 lines (143 loc) · 5.65 KB
/
FBWithUserArray.cs
File metadata and controls
162 lines (143 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#region Copyright
//
// Copyright (c) Phoenix Contact GmbH & Co. KG. All rights reserved.
// Licensed under the MIT. See LICENSE file in the project root for full license information.
//
#endregion Copyright
using Iec61131.Engineering.Prototypes.Common;
using Iec61131.Engineering.Prototypes.Methods;
using Iec61131.Engineering.Prototypes.Types;
using Iec61131.Engineering.Prototypes.Variables;
using System;
using System.Runtime.InteropServices;
namespace ExampleLib
{
// 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 PLCnext 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 = -10; // must not necessarily being zero, it also can be negative
public const int UpperBound = 9; // 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();
}
}
}
}
[FunctionBlock]
public class FB_with_user_array
{
[Input]
public IntArrayFB IN_ARRAY;
[Output, DataType("DINT")]
public int MIN_VALUE;
[Output, DataType("DINT")]
public int MAX_VALUE;
[Output, DataType("DINT")]
public int AVERAGE;
[Output]
public bool IS_DATA_CHANGED;
[Initialization]
public void __Init()
{
}
public const int BackupArraySize = 20;
public int[] BackupData = new int[BackupArraySize];
// This function block example demonstrates two jobs:
// First job is to calculate the average value of the given arrays elements.
// Second job is to compare the current array content to its content of the previous FB call.
[Execution]
public unsafe void __Process()
{
// Implementation of first job
MIN_VALUE = int.MaxValue;
MAX_VALUE = 0;
int total = 0;
fixed (int* data = &IN_ARRAY.Anchor)
{
for (int i = 0; i <= (IntArrayFB.UB - IntArrayFB.LB); i++)
{
int currentValue = data[i];
total += currentValue;
MIN_VALUE = Math.Min(MIN_VALUE, currentValue);
MAX_VALUE = Math.Max(MAX_VALUE, currentValue);
}
}
AVERAGE = total / (IntArrayFB.UB - IntArrayFB.LB + 1);
// Implementation of second job
// For arrays of elementary types the index operator can be used effectively.
IS_DATA_CHANGED = false;
// if input array of function block fits locally created backup array
int elementSize = IntArrayFB.ByteSize / sizeof(int);
if (BackupArraySize >= elementSize)
{
for (int i = 0; i <= (IntArrayFB.UB - IntArrayFB.LB); i++)
{
if (BackupData[i] != IN_ARRAY[i])
{
IS_DATA_CHANGED = true;
}
BackupData[i] = IN_ARRAY[i];
}
}
else
{
throw new IndexOutOfRangeException(); // Array size mismatch
}
}
}
}