[英]How to add my self-defined node to OPC UA Server using the OPC UA .NET StandardLibrary
I am a noob to establish an OPC UA Server using tehe .NET Standard enter link description here when I want to add the new self-defined node to the OPC Server, it cannot generate the node correctly,below is my code我是一个菜鸟,使用 .NET Standard 建立一个 OPC UA Server 当我想将新的自定义节点添加到 OPC Server 时,在此处输入链接描述,它无法正确生成节点,以下是我的代码
public override void CreateAddressSpace(IDictionary<NodeId, IList<IReference>> externalReferences)
{
lock (Lock)
{
LoadPredefinedNodes(SystemContext, externalReferences);
// find the untyped Boiler1 node that was created when the model was loaded.
BaseObjectState passiveNode = (BaseObjectState)FindPredefinedNode(new NodeId(Objects.Boiler1, NamespaceIndexes[0]), typeof(BaseObjectState));
BaseObjectState passiveNode2 = (BaseObjectState)FindPredefinedNode(new NodeId(SelfDefined.Objects.DefinedModel, NamespaceIndexes[0]), typeof(BaseObjectState));
// convert the untyped node to a typed node that can be manipulated within the server.
m_boiler1 = new BoilerState(null);
m_boiler1.Create(SystemContext, passiveNode);
m_test1 = new TestVariableState(null);
m_test1.Create(SystemContext, passiveNode2);
// replaces the untyped predefined nodes with their strongly typed versions.
AddPredefinedNode(SystemContext, m_boiler1);
AddPredefinedNode(SystemContext, m_test1);
// create a boiler node.
m_boiler2 = new BoilerState(null);
m_test2 = new TestVariableState(null);
// initialize it from the type model and assign unique node ids.
m_boiler2.Create(
SystemContext,
null,
new QualifiedName("Boiler #2", NamespaceIndexes[1]),
null,
true);
m_test2.Create(
SystemContext,
null,
new QualifiedName("Value #2", NamespaceIndexes[1]),
null,
true
);
// link root to objects folder.
IList<IReference> references = null;
if (!externalReferences.TryGetValue(Opc.Ua.ObjectIds.ObjectsFolder, out references))
{
externalReferences[Opc.Ua.ObjectIds.ObjectsFolder] = references = new List<IReference>();
}
references.Add(new NodeStateReference(Opc.Ua.ReferenceTypeIds.Organizes, false, m_test2.NodeId));
// store it and all of its children in the pre-defined nodes dictionary for easy look up.
AddPredefinedNode(SystemContext, m_boiler2);
AddPredefinedNode(SystemContext, m_test2);
// start a simulation that changes the values of the nodes.
m_simulationTimer = new Timer(DoSimulation, null, 1000, 1000);
}
}
m_test1 and m_test2 is defined by me and the boiler is the original node in the BoilerServer. m_test1 和 m_test2 由我定义,boiler 是 BoilerServer 中的原始节点。 And all the node is generated by the ModelCompiler of the OPCFoundation: enter link description here并且所有节点都是由OPCFoundation的ModelCompiler生成的:在此处输入链接描述
You wrote that you used the Opc.Ua.ModelCompiler, so I assume that you have generated your own model and added the own classes to the solution.您写道您使用了 Opc.Ua.ModelCompiler,所以我假设您已经生成了自己的模型并将自己的类添加到解决方案中。 Did you specified the new namespaces in constructor of BoilerNodeManager?您是否在 BoilerNodeManager 的构造函数中指定了新的命名空间?
string[] namespaceUrls = new string[4];
namespaceUrls[0] = Namespaces.Boiler;
namespaceUrls[1] = Namespaces.Boiler + "/Instance";
namespaceUrls[2] = <EnterYourNamespace>;
namespaceUrls[3] = <EnterYourNamespace> + "/Instance";
SetNamespaces(namespaceUrls);
This collection must be extended with your own namespaces and than you have to update the NamespaceIndexes:这个集合必须用你自己的命名空间扩展,而不是你必须更新 NamespaceIndexes:
BaseObjectState passiveNode2 = (BaseObjectState)FindPredefinedNode(new NodeId(SelfDefined.Objects.DefinedModel, NamespaceIndexes[2]), typeof(BaseObjectState));
//..
m_test2.Create(
SystemContext,
null,
new QualifiedName("Value #2", NamespaceIndexes[3]),
null,
true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.