forked from PLCnext/CSharpExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FBWithUserStruct.cs
101 lines (89 loc) · 2.57 KB
/
FBWithUserStruct.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
#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.Methods;
using Iec61131.Engineering.Prototypes.Types;
using Iec61131.Engineering.Prototypes.Variables;
using Iec61131.Engineering.Prototypes.Common;
namespace ExampleLib
{
// The attribute "Structure" is necessary to make the struct visible in the PCWorx Engineer
[Structure]
public struct Position
{
// the fields must be public as well as the struct itself
[DataType("DINT")]
public int x;
[DataType("DINT")]
public int y;
}
// Pass 'Input' and 'Output' parameter by value.
[FunctionBlock]
public class FB_with_user_struct1
{
[Input]
public Position NEW_POSITION;
[Output]
public Position CURRENT_POSITION;
[Initialization]
public void __Init()
{
}
[Execution]
public void __Process()
{
if (CURRENT_POSITION.x < NEW_POSITION.x)
{
CURRENT_POSITION.x++;
}
else if (CURRENT_POSITION.x > NEW_POSITION.x)
{
CURRENT_POSITION.x--;
}
if (CURRENT_POSITION.y < NEW_POSITION.y)
{
CURRENT_POSITION.y++;
}
else if (CURRENT_POSITION.y > NEW_POSITION.y)
{
CURRENT_POSITION.y--;
}
}
}
// Pass parameters by reference as an 'InOut' parameter. This saves memory and CPU time for copying values for large arrays and structures.
[FunctionBlock]
public class FB_with_user_struct2
{
[InOut]
unsafe public Position* NEW_POSITION;
[InOut]
unsafe public Position* CURRENT_POSITION;
[Initialization]
public void __Init()
{
}
[Execution]
unsafe public void __Process()
{
if ((*CURRENT_POSITION).x < (*NEW_POSITION).x)
{
(*CURRENT_POSITION).x++;
}
else if ((*CURRENT_POSITION).x > (*NEW_POSITION).x)
{
(*CURRENT_POSITION).x--;
}
if ((*CURRENT_POSITION).y < (*NEW_POSITION).y)
{
(*CURRENT_POSITION).y++;
}
else if ((*CURRENT_POSITION).y > (*NEW_POSITION).y)
{
(*CURRENT_POSITION).y--;
}
}
}
}