FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
opcsample/src/GodSharpOpcUaClientSample/Program.cs at main · godsharp/opcsample · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
godsharp
/
opcsample
Public
Notifications
You must be signed in to change notification settings
Fork
14
Star
49
Code
Issues
6
Pull requests
0
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
opcsample
/
src
/
GodSharpOpcUaClientSample
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
297 lines (246 loc) · 11.1 KB
Breadcrumbs
opcsample
/
src
/
GodSharpOpcUaClientSample
/
Program.cs
Copy path
File metadata and controls
297 lines (246 loc) · 11.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using
GodSharp
.
Opc
.
Ua
;
using
GodSharp
.
Opc
.
Ua
.
Client
;
using
GodSharp
.
Opc
.
Ua
.
Client
.
Extensions
;
using
Opc
.
Ua
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Threading
;
using
System
.
Threading
.
Tasks
;
namespace
GodSharpOpcUaClientSample
{
internal
class
Program
{
private
static
async
Task
Main
(
string
[
]
args
)
{
Console
.
WriteLine
(
"Hello GodSharp OpcUa Client Sample!"
)
;
var
url
=
"opc.tcp://127.0.0.1:53530/OPCUA/SimulationServer"
;
url
=
"opc.tcp://127.0.0.1:48020/"
;
url
=
"opc.tcp://127.0.0.1:49320/"
;
url
=
"opc.tcp://192.168.1.11:4840"
;
url
=
"opc.tcp://10.0.0.20:4840"
;
url
=
"opc.tcp://192.168.250.1:4840"
;
#region # - Discovery
var
_discovery
=
false
;
if
(
_discovery
)
{
OpcUaServerDiscovery
discovery
=
new
OpcUaServerDiscovery
(
)
;
Console
.
WriteLine
(
"discovery Discovery"
)
;
var
servers
=
discovery
.
Discovery
(
url
)
;
foreach
(
var
item
in
servers
)
{
foreach
(
var
durl
in
item
.
DiscoveryUrls
)
{
Console
.
WriteLine
(
$
"
{
durl
}
"
)
;
var
endpoints
=
discovery
.
GetEndpoints
(
durl
)
;
if
(
endpoints
==
null
)
continue
;
foreach
(
var
endpoint
in
endpoints
)
{
Console
.
WriteLine
(
$
"
\t
-
{
endpoint
.
EndpointUrl
}
/
{
endpoint
.
SecurityMode
}
/
{
endpoint
.
SecurityPolicyUri
}
"
)
;
}
}
}
Console
.
WriteLine
(
"discovery DiscoveryUrls"
)
;
var
discoverys
=
discovery
.
DiscoveryUrls
(
url
)
;
if
(
discoverys
!=
null
)
{
foreach
(
var
discoveryUrl
in
discoverys
)
{
Console
.
WriteLine
(
$
"
{
discoveryUrl
}
"
)
;
var
endpoints
=
discovery
.
GetEndpoints
(
discoveryUrl
)
;
if
(
endpoints
==
null
)
continue
;
foreach
(
var
endpoint
in
endpoints
)
{
Console
.
WriteLine
(
$
"
\t
-
{
endpoint
.
EndpointUrl
}
/
{
endpoint
.
SecurityMode
}
/
{
endpoint
.
SecurityPolicyUri
}
"
)
;
}
}
}
Console
.
WriteLine
(
"discovery finished"
)
;
Console
.
ReadLine
(
)
;
}
#endregion # - Discovery
#region 0 - Initial
var
builder
=
new
OpcUaClientBuilder
(
)
.
WithEndpoint
(
url
)
.
WithAnonymous
(
)
.
WithSecurity
(
MessageSecurityMode
.
None
,
SecurityPolicies
.
None
)
//.WithAccount("root","secret")
//.WithAccount("root","secret12345678")
//.WithCertificate(cert2)
//.WithSecurity(MessageSecurityMode.SignAndEncrypt,SecurityPolicies.Basic256Sha256)
.
WithClientId
(
"GodSharpOpcUaClientConsole"
)
;
var
slim
=
new
ManualResetEventSlim
(
false
)
;
var
connectToServerWhenBuild
=
false
;
var
client
=
await
builder
.
BuildAsync
(
connectToServerWhenBuild
)
;
client
.
OnSessionOpenFailureTryAgainHandle
+=
(
s
,
e
)
=>
{
Console
.
WriteLine
(
$
"[
{
DateTime
.
Now
:
HH:mm:ss.fff
}
] OnSessionOpenFailureTryAgainHandle:
{
s
}
:
{
e
.
Message
}
"
)
;
}
;
client
.
OnSessionConnectNotification
+=
(
s
,
t
)
=>
{
switch
(
t
)
{
case
SessionConnectionState
.
Connected
:
Console
.
WriteLine
(
$
"[
{
DateTime
.
Now
:
HH:mm:ss.fff
}
]
{
s
.
SessionName
}
:connected"
)
;
slim
.
Set
(
)
;
break
;
case
SessionConnectionState
.
Reconnecting
:
Console
.
WriteLine
(
$
"[
{
DateTime
.
Now
:
HH:mm:ss.fff
}
]
{
s
.
SessionName
}
:reconnecting"
)
;
break
;
case
SessionConnectionState
.
Disconnecting
:
Console
.
WriteLine
(
$
"[
{
DateTime
.
Now
:
HH:mm:ss.fff
}
]
{
s
.
SessionName
}
:disconnecting"
)
;
break
;
case
SessionConnectionState
.
Disconnected
:
Console
.
WriteLine
(
$
"[
{
DateTime
.
Now
:
HH:mm:ss.fff
}
]
{
s
.
SessionName
}
:disconnected"
)
;
break
;
default
:
throw
new
ArgumentOutOfRangeException
(
nameof
(
t
)
,
t
,
null
)
;
}
}
;
client
.
OnSessionKeepAlive
+=
(
s
,
e
)
=>
{
//Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}]{s.SessionName}:{e.CurrentState}");
}
;
client
.
OnSessionSubscriptionChanged
+=
subscription
=>
{
foreach
(
var
item
in
subscription
.
Notifications
)
{
//Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}]{item..SessionName}:{e.CurrentState}");
}
}
;
client
.
OnMonitoredItemNotification
+=
(
n
,
i
,
e
)
=>
{
foreach
(
var
value
in
i
.
DequeueValues
(
)
)
{
Console
.
WriteLine
(
"{0}->{1} : {2}, {3}, {4}"
,
n
,
i
.
DisplayName
,
value
.
Value
,
value
.
SourceTimestamp
,
value
.
StatusCode
)
;
}
}
;
client
.
OnSubscribePollingChanged
+=
(
e
)
=>
{
foreach
(
var
value
in
e
.
Values
)
{
Console
.
WriteLine
(
"{0}->{1} : {2}"
,
e
.
Name
,
value
.
Node
.
ToString
(
)
,
value
.
Value
)
;
}
}
;
#endregion 0 - Initial
#region 1 - StartAsync
Console
.
WriteLine
(
"1 - StartAsync ..."
)
;
try
{
var
connected
=
connectToServerWhenBuild
?
client
.
Connected
:
client
.
Open
(
)
;
Console
.
WriteLine
(
$
"connect
{
connected
}
"
)
;
}
catch
(
Exception
e
)
{
Console
.
WriteLine
(
e
.
Message
)
;
Console
.
WriteLine
(
"connect failed,waiting for connect"
)
;
slim
.
Wait
(
)
;
}
//Console.WriteLine("1.1 - ComplexTypeSystem Load ...");
//var complexTypeSystem = new ComplexTypeSystem(client.Session);
//await complexTypeSystem.Load().ConfigureAwait(false);
//Console.WriteLine("1.1 - ComplexTypeSystem Load");
#endregion 1 - StartAsync
#region 2 - Browse / BrowseTree
var
_browse
=
false
;
if
(
_browse
)
{
Console
.
WriteLine
(
"2 - Press any key to test Browse..."
)
;
Console
.
ReadLine
(
)
;
var
all
=
client
.
Browse
(
)
;
//var browse = client.Session.Browse(new NodeId("ns=4;s=Demo.Static"));
Console
.
WriteLine
(
" DisplayName, BrowseName, NodeClass"
)
;
foreach
(
var
obj
in
all
)
{
Console
.
WriteLine
(
" {0}, {1}, {2}"
,
obj
.
DisplayName
,
obj
.
BrowseName
,
obj
.
NodeClass
)
;
var
browse2
=
client
.
Browse
(
(
NodeId
)
obj
.
NodeId
)
;
foreach
(
var
refd
in
browse2
)
{
Console
.
WriteLine
(
" + {0}, {1}, {2}"
,
refd
.
DisplayName
,
refd
.
BrowseName
,
refd
.
NodeClass
)
;
}
}
var
tree
=
client
.
BrowseTree
(
"ns=5;i=1"
)
;
//var tree = client.BrowseTree(new NodeId("ns=4;s=Demo.Static"));
Browse
(
tree
)
;
static
void
Browse
(
IEnumerable
<
ReferenceBrowseDescription
>
refs
,
int
level
=
-
1
)
{
level
++
;
foreach
(
var
description
in
refs
)
{
Console
.
WriteLine
(
"{0}+{1}, {2},{3}"
,
new
string
(
'
\t
'
,
level
)
,
//Formatter.FormatAttributeValue(attribute.ValueId.AttributeId, attribute.Value)}
//description.Node.BrowseName,
description
.
GetFormatText
(
)
,
description
.
Node
.
NodeClass
,
description
.
Node
.
NodeId
)
;
if
(
description
.
Children
!=
null
)
{
Browse
(
description
.
Children
,
level
)
;
}
}
}
}
#endregion 2 - Browse / BrowseTree
#region 3 - GetAttributes / GetProperties
var
_attributes
=
false
;
if
(
_attributes
)
{
Console
.
WriteLine
(
"3 - Press any key to test GetAttributes / GetProperties ..."
)
;
Console
.
ReadLine
(
)
;
var
node
=
new
NodeId
(
"ns=5;i=1"
)
;
var
attributes
=
client
.
GetAttributes
(
node
)
;
foreach
(
var
attribute
in
attributes
)
{
Console
.
WriteLine
(
$
"
{
attribute
.
Name
}
:
{
attribute
.
ValueText
}
"
)
;
}
var
properties
=
client
.
GetProperties
(
node
)
;
if
(
properties
!=
null
)
{
foreach
(
var
attribute
in
properties
)
{
Console
.
WriteLine
(
$
"
{
attribute
.
Name
}
:
{
GodSharp
.
Opc
.
Ua
.
Client
.
OpcUaHelper
.
FormatAttributeValue
(
attribute
.
ValueId
.
AttributeId
,
attribute
.
Value
,
client
.
Session
)
}
"
)
;
}
}
}
#endregion 3 - GetAttributes / GetProperties
#region 4 - Subscribe / Unsubscribe
var
_subscribe
=
false
;
if
(
_subscribe
)
{
Console
.
WriteLine
(
"Press any key to Subscribe"
)
;
Console
.
ReadLine
(
)
;
var
sub_name
=
"andon"
;
var
subscribes
=
new
string
[
]
{
"ns=0;i=2258"
,
"ns=0;i=2259"
}
;
// 2258 Server.ServerStatus.CurrentTime
// 2259 Server.ServerStatus.State
client
.
Subscribe
(
sub_name
,
subscribes
)
;
Console
.
WriteLine
(
"Press any key to Unsubscribe"
)
;
Console
.
ReadLine
(
)
;
client
.
Unsubscribe
(
sub_name
)
;
}
#endregion 4 - Subscribe / Unsubscribe
#region 4.1 - Subscribe Event / Unsubscribe Event
var
_event
=
false
;
if
(
_event
)
{
Console
.
WriteLine
(
"Press any key to Subscribe Event"
)
;
Console
.
ReadLine
(
)
;
var
sub_name
=
"andon"
;
var
subscribes
=
new
string
[
]
{
"ns=0;i=2258"
,
"ns=0;i=2259"
}
;
// 2258 Server.ServerStatus.CurrentTime
// 2259 Server.ServerStatus.State
client
.
Subscribe
(
sub_name
,
subscribes
)
;
Console
.
WriteLine
(
"Press any key to Unsubscribe Event"
)
;
Console
.
ReadLine
(
)
;
client
.
Unsubscribe
(
sub_name
)
;
}
#endregion 4.1 - Subscribe Event / Unsubscribe Event
Console
.
WriteLine
(
"5 - Press any key to test read/write ..."
)
;
Console
.
WriteLine
(
"6 - Press any key to close session..."
)
;
Console
.
ReadLine
(
)
;
var
ret
=
client
.
Close
(
)
;
Console
.
WriteLine
(
$
"Exit
{
ret
}
"
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL