使用 OPC UA .NET 从 Kepserver 读取 "Channel1.Device1.Tag1" 值
Using OPC UA .NET read "Channel1.Device1.Tag1" value from Kepserver
I am using NetCoreConsoleClient sample from here: https://github.com/OPCFoundation/UA-.NETStandardLibrary
我自定义了代码,能够浏览整个服务器并为服务器上的每个节点获取时间戳、描述、数据类型等值。
对于节点 "Channel1.Device1.Tag1",我能够获取所有 properties/vlaues(如数据类型、地址、描述),但我没有得到 属性 的实际值"Tag1"
在 Kepserver 上,我注意到 "Channel1.Device.Tag1" 没有任何名为 "Value" 的 属性 保存实际标签值。
当我使用 KepServer 提供的 "Quick Client" 时出现 "Tag1" 值。
我需要使用我自己的客户端以某种方式取出 "tag" 值。
修改后的一段代码:
`Console.WriteLine("4 - Browse the OPC UA server namespace.");
ReferenceDescriptionCollection references;
Byte[] continuationPoint;
references = session.FetchReferences(ObjectIds.ObjectsFolder);
session.Browse(
null,
null,
ObjectIds.ObjectsFolder,
//ObjectIds.RootFolder,
0u,
BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences,
true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method,
out continuationPoint,
out references);
Console.WriteLine(" DisplayName, BrowseName, NodeClass");
foreach (var rd in references)
{
Console.WriteLine(" {0}, {1}, {2}", rd.DisplayName, rd.BrowseName, rd.NodeClass);
ReferenceDescriptionCollection nextRefs;
byte[] nextCp;
session.Browse(
null,
null,
ExpandedNodeId.ToNodeId(rd.NodeId, session.NamespaceUris),
0u,
BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences,
true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method,
out nextCp,
out nextRefs);
foreach (var nextRd in nextRefs)
{
Console.WriteLine(" + {0}, {1}, {2}", nextRd.DisplayName, nextRd.BrowseName, nextRd.NodeClass);
ReferenceDescriptionCollection nextRefs1;
byte[] nextCp1;
session.Browse(
null,
null,
ExpandedNodeId.ToNodeId(nextRd.NodeId, session.NamespaceUris),
0u,
BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences,
true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method,
out nextCp1,
out nextRefs1);
foreach (var nextRd1 in nextRefs1)
{
Console.WriteLine(" + {0}, {1}, {2}", nextRd1.DisplayName, nextRd1.BrowseName, nextRd1.NodeClass);
ReferenceDescriptionCollection nextRefs2;
byte[] nextCp2;
var res = session.Browse(
null, null,
ExpandedNodeId.ToNodeId(nextRd1.NodeId, session.NamespaceUris), 0u, BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences, true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out nextCp2, out nextRefs2);
double maxAge = 0; //Pick 0 millisecond old value directly from device not from the cache.
DataValueCollection results;
DiagnosticInfoCollection diagInfos;
ReadValueIdCollection readValueIds;
foreach (var nextRd2 in nextRefs2)
{
try
{
if (!nextRd2.NodeId.IsNull)
{
var node = ExpandedNodeId.ToNodeId(nextRd2.NodeId, session.NamespaceUris);
if (node != null)
{
DataValue dv = session.ReadValue(node);
ReferenceDescriptionCollection nextRefs3;
byte[] nextCp3;
var res1 = session.Browse(null, null,
ExpandedNodeId.ToNodeId(nextRd2.NodeId, session.NamespaceUris), 0u, BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences, true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out nextCp3, out nextRefs3);
foreach (var nextRd3 in nextRefs3)
{
if (!nextRd3.NodeId.IsNull)
{
var node1 = ExpandedNodeId.ToNodeId(nextRd3.NodeId, session.NamespaceUris);
if (node1 != null)
{
DataValue dv2 = session.ReadValue(node1);
}
}
}
}
}
}
catch (ServiceResultException srex)
{
//eat exception yum yum yum!
}
catch (Exception ex)
{
//eat exception yum yum yum!
}`
通过示例代码的点击和试用,我找到了答案。
给出值的神奇语句是:
数据值 dv1 = session.ReadValue("ns=2;s=Channel1.Device1.Tag1");
foreach (var nextRd1 in nextRefs1)
{
Console.WriteLine(" + {0}, {1}, {2}", nextRd1.DisplayName, nextRd1.BrowseName, nextRd1.NodeClass);
try
{
var _node = ExpandedNodeId.ToNodeId(nextRd1.NodeId, session.NamespaceUris);
DataValue dv2 = session.ReadValue(_node);
}
此示例使用:
https://github.com/hylasoft-usa/h-opc#documentation
您可能只需要找出您需要检索的值的类型
public void ReadValueFromOPC(string host, string userName, string password)
{
var options = new UaClientOptions
{
UserIdentity = new Opc.Ua.UserIdentity(
userName,
password)
};
using (var client = new UaClient(new Uri("opc.tcp://" +
host), options))
{
client.Connect();
var node = client.FindNode("SomeTag.SomeChildTag");
// Find out what the type is before you try to get the value
Type type = client.GetDataType(node.Tag);
// If you find out it's a UInt32 then you use it.
var value = client.Read<UInt32>(node.Tag).Value;
}
}
我自定义了代码,能够浏览整个服务器并为服务器上的每个节点获取时间戳、描述、数据类型等值。
对于节点 "Channel1.Device1.Tag1",我能够获取所有 properties/vlaues(如数据类型、地址、描述),但我没有得到 属性 的实际值"Tag1"
在 Kepserver 上,我注意到 "Channel1.Device.Tag1" 没有任何名为 "Value" 的 属性 保存实际标签值。
当我使用 KepServer 提供的 "Quick Client" 时出现"Tag1" 值。
我需要使用我自己的客户端以某种方式取出 "tag" 值。
修改后的一段代码:
`Console.WriteLine("4 - Browse the OPC UA server namespace.");
ReferenceDescriptionCollection references;
Byte[] continuationPoint;
references = session.FetchReferences(ObjectIds.ObjectsFolder);
session.Browse(
null,
null,
ObjectIds.ObjectsFolder,
//ObjectIds.RootFolder,
0u,
BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences,
true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method,
out continuationPoint,
out references);
Console.WriteLine(" DisplayName, BrowseName, NodeClass");
foreach (var rd in references)
{
Console.WriteLine(" {0}, {1}, {2}", rd.DisplayName, rd.BrowseName, rd.NodeClass);
ReferenceDescriptionCollection nextRefs;
byte[] nextCp;
session.Browse(
null,
null,
ExpandedNodeId.ToNodeId(rd.NodeId, session.NamespaceUris),
0u,
BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences,
true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method,
out nextCp,
out nextRefs);
foreach (var nextRd in nextRefs)
{
Console.WriteLine(" + {0}, {1}, {2}", nextRd.DisplayName, nextRd.BrowseName, nextRd.NodeClass);
ReferenceDescriptionCollection nextRefs1;
byte[] nextCp1;
session.Browse(
null,
null,
ExpandedNodeId.ToNodeId(nextRd.NodeId, session.NamespaceUris),
0u,
BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences,
true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method,
out nextCp1,
out nextRefs1);
foreach (var nextRd1 in nextRefs1)
{
Console.WriteLine(" + {0}, {1}, {2}", nextRd1.DisplayName, nextRd1.BrowseName, nextRd1.NodeClass);
ReferenceDescriptionCollection nextRefs2;
byte[] nextCp2;
var res = session.Browse(
null, null,
ExpandedNodeId.ToNodeId(nextRd1.NodeId, session.NamespaceUris), 0u, BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences, true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out nextCp2, out nextRefs2);
double maxAge = 0; //Pick 0 millisecond old value directly from device not from the cache.
DataValueCollection results;
DiagnosticInfoCollection diagInfos;
ReadValueIdCollection readValueIds;
foreach (var nextRd2 in nextRefs2)
{
try
{
if (!nextRd2.NodeId.IsNull)
{
var node = ExpandedNodeId.ToNodeId(nextRd2.NodeId, session.NamespaceUris);
if (node != null)
{
DataValue dv = session.ReadValue(node);
ReferenceDescriptionCollection nextRefs3;
byte[] nextCp3;
var res1 = session.Browse(null, null,
ExpandedNodeId.ToNodeId(nextRd2.NodeId, session.NamespaceUris), 0u, BrowseDirection.Forward,
ReferenceTypeIds.HierarchicalReferences, true,
(uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out nextCp3, out nextRefs3);
foreach (var nextRd3 in nextRefs3)
{
if (!nextRd3.NodeId.IsNull)
{
var node1 = ExpandedNodeId.ToNodeId(nextRd3.NodeId, session.NamespaceUris);
if (node1 != null)
{
DataValue dv2 = session.ReadValue(node1);
}
}
}
}
}
}
catch (ServiceResultException srex)
{
//eat exception yum yum yum!
}
catch (Exception ex)
{
//eat exception yum yum yum!
}`
通过示例代码的点击和试用,我找到了答案。
给出值的神奇语句是:
数据值 dv1 = session.ReadValue("ns=2;s=Channel1.Device1.Tag1");
foreach (var nextRd1 in nextRefs1)
{
Console.WriteLine(" + {0}, {1}, {2}", nextRd1.DisplayName, nextRd1.BrowseName, nextRd1.NodeClass);
try
{
var _node = ExpandedNodeId.ToNodeId(nextRd1.NodeId, session.NamespaceUris);
DataValue dv2 = session.ReadValue(_node);
}
此示例使用: https://github.com/hylasoft-usa/h-opc#documentation
您可能只需要找出您需要检索的值的类型
public void ReadValueFromOPC(string host, string userName, string password)
{
var options = new UaClientOptions
{
UserIdentity = new Opc.Ua.UserIdentity(
userName,
password)
};
using (var client = new UaClient(new Uri("opc.tcp://" +
host), options))
{
client.Connect();
var node = client.FindNode("SomeTag.SomeChildTag");
// Find out what the type is before you try to get the value
Type type = client.GetDataType(node.Tag);
// If you find out it's a UInt32 then you use it.
var value = client.Read<UInt32>(node.Tag).Value;
}
}