forked from PLCnext/CSharpExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFBWithString.cs
More file actions
135 lines (124 loc) · 4.19 KB
/
FBWithString.cs
File metadata and controls
135 lines (124 loc) · 4.19 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
#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 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
{
// Define a string data type with a maximum string length of 200 characters
// Size is string length + 4 byte header + 1 byte terminating zero + padding for two byte alignment
// Mark the declaration with a String attribute and define the length
[String(200)]
[StructLayout(LayoutKind.Explicit, Size = 206)]
public struct TString200
{
// Fields
[FieldOffset(0)]
public IecStringEx s;
// Methods
//ctor is needed to set the maximum size and called in the initialization
public void ctor()
{
this.s.maximumLength = 200;
}
public void rctor()
{
this.s.maximumLength = 200;
}
}
[FunctionBlock]
public class FB_with_string
{
[Input]
public IecString80 VALUE;
[Input]
public short MESSAGE_ID;
[Output]
public IecString80 RESULT1; // Standard string
[Output]
public TString200 RESULT2; // User defined string
[Initialization]
public void __Init()
{
// Call ctor of strings in order to set the maximum size
VALUE.ctor();
RESULT1.ctor();
RESULT2.ctor();
}
[Execution]
public void __Process()
{
// Assign one IecString to another
IecStringEx.Copy(ref VALUE.s, ref RESULT1.s);
// assign .Net String to an IecString
switch (MESSAGE_ID)
{
case 0:
RESULT2.s.Init("Nothing selected!");
break;
case 1:
RESULT2.s.Init("The quick brown fox jumps over the lazy dog.");
break;
case 2:
RESULT2.s.Init("The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.");
break;
default:
RESULT2.s.Init("Undefined message ID!");
break;
}
}
}
// Input and InOut parameter can be passed by reference. This saves memory and CPU time for copying values for large Arrays and structs.
[FunctionBlock]
public class FB_with_string2
{
[InOut]
public unsafe IecStringEx* VALUE;
[Input]
public short MESSAGE_ID;
[InOut]
public unsafe IecStringEx* RESULT1;
[InOut]
public unsafe IecStringEx* RESULT2;
[Initialization]
public void __Init()
{
//Calling the ctor is not allowed when the string is passed by reference
//at initialization time the pointer has no valid reference
}
[Execution]
public void __Process()
{
unsafe
{
// Assign one IecString to another
IecStringEx.Copy(ref *VALUE, ref *RESULT1);
// assign .Net String to an IecString
switch (MESSAGE_ID)
{
case 0:
RESULT2->Init("Nothing selected!");
break;
case 1:
RESULT2->Init("The quick brown fox jumps over the lazy dog.");
break;
case 2:
RESULT2->Init("The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.");
break;
default:
RESULT2->Init("Undefined message ID!");
break;
}
}
}
}
}