forked from PLCnext/CSharpExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFBWithUserStruct.cs
More file actions
97 lines (89 loc) · 2.52 KB
/
FBWithUserStruct.cs
File metadata and controls
97 lines (89 loc) · 2.52 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
#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 Iec61131.Engineering.Prototypes.Types;
using Iec61131.Engineering.Prototypes.Variables;
using Iec61131.Engineering.Prototypes.Methods;
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
public int x;
public int y;
}
[FunctionBlock]
public class FB_with_user_struct
{
[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 'Input' and InOut parameter by reference as an InOut parameter. This saves memory and CPU time for copying values for large Arrays and structs.
[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--;
}
}
}
}