我是菜鸟,使用.NET Standard enter link description here建立OPC UA服务器当我想将新的自定义节点添加到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和m_test2由我定义,并且锅炉是BoilerServer中的原始节点。并且所有节点都是由OPCFoundation的ModelCompiler生成的:enter link description here
您写道您使用了Opc.Ua.ModelCompiler,因此我假设您已经生成了自己的模型并将自己的类添加到解决方案中。您是否在BoilerNodeManager的构造函数中指定了新的命名空间?
string[] namespaceUrls = new string[4];
namespaceUrls[0] = Namespaces.Boiler;
namespaceUrls[1] = Namespaces.Boiler + "/Instance";
namespaceUrls[2] = <EnterYourNamespace>;
namespaceUrls[3] = <EnterYourNamespace> + "/Instance";
SetNamespaces(namespaceUrls);
此集合必须使用您自己的名称空间扩展,并且必须更新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);