-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFB2WithANY_NUM.cs
More file actions
270 lines (227 loc) · 9.34 KB
/
FB2WithANY_NUM.cs
File metadata and controls
270 lines (227 loc) · 9.34 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#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.Iec61131Lib;
namespace ExampleLib
{
/// <summary>
/// The example FB limits a value into a defined range
/// and returns the result as Any
/// </summary>
[FunctionBlock]
public class FB2_with_ANY_NUM
{
[Input, DataType("ANY_NUM")]
public Any VALUE;
[Input, DataType("ANY_NUM")]
public Any MIN;
[Input, DataType("ANY_NUM")]
public Any MAX;
// Caution: Output parameters are not supported as ANY. Use InOut parameters instead.
[InOut, DataType("ANY_NUM")]
public Any RESULT;
[Output, DataType("UDINT")]
public uint SIZE_OF_VALUE;
[Initialization]
public void __Init()
{
}
[Execution]
public unsafe void __Process()
{
// Important due to unsafe programming:
// Check whether all parameters have the same data type!!!
if (VALUE.pRuntimeTypeHandle != RESULT.pRuntimeTypeHandle || VALUE.pRuntimeTypeHandle != MIN.pRuntimeTypeHandle || VALUE.pRuntimeTypeHandle != MAX.pRuntimeTypeHandle)
{
return;
}
// get the size of the connected value type
SIZE_OF_VALUE = VALUE.nLength;
// Get the element type constants associate to the runtime type handle.
// The values are defined in the standard ECMA-335 "Common Language Infrastructure (CLI)",
// Partition II, chapter II.23.1.16 "Element types used in signatures")
Eclr.TypeCode code = (Eclr.TypeCode)Eclr.TypeInfo.GetTypeCode(VALUE.pRuntimeTypeHandle);
// type dependent action
switch (code)
{
case Eclr.TypeCode.SByte:
{
sbyte tempValue = *(sbyte*)VALUE.pValue;
sbyte tempMin = *(sbyte*)MIN.pValue;
sbyte tempMax = *(sbyte*)MAX.pValue;
sbyte* pResult = (sbyte*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.Byte:
{
byte tempValue = *(byte*)VALUE.pValue;
byte tempMin = *(byte*)MIN.pValue;
byte tempMax = *(byte*)MAX.pValue;
byte* pResult = (byte*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.Int16:
{
short tempValue = *(short*)VALUE.pValue;
short tempMin = *(short*)MIN.pValue;
short tempMax = *(short*)MAX.pValue;
short* pResult = (short*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.UInt16:
{
ushort tempValue = *(ushort*)VALUE.pValue;
ushort tempMin = *(ushort*)MIN.pValue;
ushort tempMax = *(ushort*)MAX.pValue;
ushort* pResult = (ushort*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.Int32:
{
int tempValue = *(int*)VALUE.pValue;
int tempMin = *(int*)MIN.pValue;
int tempMax = *(int*)MAX.pValue;
int* pResult = (int*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.UInt32:
{
uint tempValue = *(uint*)VALUE.pValue;
uint tempMin = *(uint*)MIN.pValue;
uint tempMax = *(uint*)MAX.pValue;
uint* pResult = (uint*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.Int64:
{
long tempValue = *(long*)VALUE.pValue;
long tempMin = *(long*)MIN.pValue;
long tempMax = *(long*)MAX.pValue;
long* pResult = (long*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.UInt64:
{
ulong tempValue = *(ulong*)VALUE.pValue;
ulong tempMin = *(ulong*)MIN.pValue;
ulong tempMax = *(ulong*)MAX.pValue;
ulong* pResult = (ulong*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.Single:
{
float tempValue = *(float*)VALUE.pValue;
float tempMin = *(float*)MIN.pValue;
float tempMax = *(float*)MAX.pValue;
float* pResult = (float*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
case Eclr.TypeCode.Double:
{
double tempValue = *(double*)VALUE.pValue;
double tempMin = *(double*)MIN.pValue;
double tempMax = *(double*)MAX.pValue;
double* pResult = (double*)RESULT.pValue;
if (tempValue < tempMin)
{
tempValue = tempMin;
}
if (tempValue > tempMax)
{
tempValue = tempMax;
}
*pResult = tempValue;
break;
}
}
}
}
}