OpcUaExtension.cs
2.03 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
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Model.Entities;
using Opc.Ua;
using Opc.Ua.Client;
using OpcUaHelper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace HHECS.Executor
{
/// <summary>
/// PLC读写扩展类
/// </summary>
public static class OpcUaExtension
{
private static Session m_session;
/// <summary>
/// read several value nodes from server
/// </summary>
/// <param name="tags">所以的节点数组信息</param>
/// <returns>all values</returns>
public static List<T> ReadNodes<T>(params BusCollectionCommand[] collectionCommands)
{
var nodesToRead = collectionCommands.Select(t => new ReadValueId()
{
NodeId = new NodeId(t.CommandCode),
AttributeId = Attributes.Value
}).ToList();
List<T> result = new List<T>();
// 读取当前的值
var ss = m_session.Read(
null,
0,
TimestampsToReturn.Neither,
(ReadValueIdCollection)nodesToRead,
out DataValueCollection results,
out DiagnosticInfoCollection diagnosticInfos);
ClientBase.ValidateResponse(results, nodesToRead);
for (int i = 0; i < results.Count; i++)
{
collectionCommands[i].Value = (string)results[i].Value;
}
ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);
foreach (var item in results)
{
result.Add((T)item.Value);
}
return result;
}
/// <summary>
/// connect to server
/// </summary>
/// <param name="serverUrl">remote url</param>
public static bool ConnectServer(string serverUrl ,OpcUaClient opcUaClient)
{
opcUaClient.ConnectServer(serverUrl);
return opcUaClient.Connected;
}
}
}