forked from PLCnext/CSharpExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FBWithUserArray.cs
142 lines (132 loc) · 4.51 KB
/
FBWithUserArray.cs
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
#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
using System;
using System.Iec61131Lib;
using Eclr;
using Eclr.Pcos;
using System.Runtime.InteropServices;
using Iec61131.Engineering.Prototypes.Types;
using Iec61131.Engineering.Prototypes.Variables;
using Iec61131.Engineering.Prototypes.Methods;
using Iec61131.Engineering.Prototypes.Common;
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 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.size)]
public struct IntArray
{
// Helper containing constants to have a
// clear and maintainable definition for boundaries and size
struct ArrayProperties
{
public const int lowerBound = 0;
public const int upperBound = 9;
// the size must be changed to the correct size of your elements times the amount of elements
public const int size = (upperBound - lowerBound + 1) * sizeof(int);
}
// 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 && index <= UB)
{
unsafe
{
fixed (int* pValue = &Anchor)
{
int result = *(pValue + index);
return result;
}
}
}
else
{
throw new IndexOutOfRangeException();
}
}
set
{
if (index >= LB && index <= UB)
{
unsafe
{
fixed (int* pValue = &Anchor)
{
*(pValue + index) = value;
}
}
}
else
{
throw new IndexOutOfRangeException();
}
}
}
}
[FunctionBlock]
public class FB_with_user_array
{
[Input]
public IntArray iIN;
[Output]
public int iMIN_VALUE;
[Output]
public int iMAX_VALUE;
[Output]
public int iAVERAGE;
[Output]
public bool xIS_DATA_CHANGED;
private IntArray Backup;
[Initialization]
public void __Init()
{
}
[Execution]
public unsafe void __Process()
{
iMIN_VALUE = int.MaxValue;
iMAX_VALUE = 0;
int total = 0;
// This kind of accessing the array works fine with all types of arrays but it is really cumbersome
// Nevertheless it can also be used for arrays of e.g. IecStrings.
fixed (int* data = &iIN.Anchor)
{
for (int i = 0; i < (IntArray.UB - IntArray.LB + 1); i++)
{
int currentValue = data[i];
total += currentValue;
iMIN_VALUE = Math.Min(iMIN_VALUE, currentValue);
iMAX_VALUE = Math.Max(iMAX_VALUE, currentValue);
}
}
// For arrays of elementary types the index operator can be used effectively.
xIS_DATA_CHANGED = false;
for (int i = IntArray.LB; i <= IntArray.UB; i++)
{
if(Backup[i] != iIN[i])
{
xIS_DATA_CHANGED = true;
}
}
Backup = iIN;
iAVERAGE = total / (IntArray.UB - IntArray.LB + 1);
}
}
}