如何使用OPC UA.NET标准库将自定义节点添加到OPC UA服务器
我是一个使用tehe.NET标准建立OPC UA服务器的noob 当我想将新的自定义节点添加到OPC服务器时,它无法正确生成节点,下面是我的代码如何使用OPC UA.NET标准库将自定义节点添加到OPC UA服务器,.net,opc-ua,.net,Opc Ua,我是一个使用tehe.NET标准建立OPC UA服务器的noob 当我想将新的自定义节点添加到OPC服务器时,它无法正确生成节点,下面是我的代码 public override void CreateAddressSpace(IDictionary<NodeId, IList<IReference>> externalReferences) { lock (Lock) { LoadPredefinedNo
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);
}
}
public覆盖void CreateAddressSpace(IDictionary外部引用)
{
锁(锁)
{
LoadPredefinedNodes(系统上下文、外部引用);
//查找加载模型时创建的非类型化Boiler1节点。
BaseObjectState被动节点=(BaseObjectState)FindPredefinedNode(新节点ID(Objects.Boiler1,NamespaceIndexes[0]),typeof(BaseObjectState));
BaseObjectState被动节点2=(BaseObjectState)FindPredefinedNode(新节点ID(SelfDefined.Objects.DefinedModel,NamespaceIndexes[0]),typeof(BaseObjectState));
//将非类型化节点转换为可在服务器内操作的类型化节点。
m_boiler1=新的BoilerState(null);
m_样板1.创建(SystemContext,被动节点);
m_test1=新的TestVariableState(null);
m_test1.Create(SystemContext,passiveNode2);
//将非类型化的预定义节点替换为其强类型版本。
AddPredefinedNode(SystemContext,m_boiler1);
AddPredefinedNode(SystemContext,m_test1);
//创建锅炉节点。
m_boiler2=新的BoilerState(null);
m_test2=新的TestVariableState(null);
//从类型模型初始化它并分配唯一的节点ID。
m_样板2.Create(
系统上下文,
无效的
新的限定名称(“锅炉2”,名称空间索引[1]),
无效的
正确的);
m_test2.Create(
系统上下文,
无效的
新的限定名称(“值#2”,名称空间索引[1]),
无效的
真的
);
//将根目录链接到对象文件夹。
IList引用=null;
if(!externalReferences.TryGetValue(Opc.Ua.ObjectIds.ObjectsFolder,out references))
{
externalReferences[Opc.Ua.ObjectId.ObjectsFolder]=引用=新列表();
}
references.Add(新的NodeStateReference(Opc.Ua.referencetypeid.Organizes,false,m_test2.NodeId));
//将其及其所有子节点存储在预定义的节点字典中,以便于查找。
AddPredefinedNode(SystemContext,m_boiler2);
AddPredefinedNode(SystemContext,m_test2);
//开始更改节点值的模拟。
m_simulationTimer=新定时器(剂量模拟,零,1000,1000);
}
}
m_test1和m_test2由我定义,锅炉是BoilerServer中的原始节点。所有节点都是由OPCFoundation的ModelCompiler生成的:您编写了使用Opc.Ua.ModelCompiler的代码,因此我假设您已经生成了自己的模型并将自己的类添加到解决方案中。您是否在BoilerNodeManager的构造函数中指定了新名称空间
string[]namespaceUrls=新字符串[4];
namespaceUrls[0]=Namespaces.Boiler;
namespaceUrls[1]=Namespaces.Boiler+“/Instance”;
名称空间URL[2]=;
名称空间URL[3]=+“/Instance”;
设置名称空间(名称空间URL);
必须使用您自己的命名空间扩展此集合,然后您必须更新命名空间索引:
BaseObjectState被动节点2=(BaseObjectState)FindPredefinedNode(新节点ID(SelfDefined.Objects.DefinedModel,NamespaceIndexes[2]),typeof(BaseObjectState));
//..
m_test2.Create(
系统上下文,
无效的
新的限定名称(“值#2”,名称空间索引[3]),
无效的
正确的);
请解释您所说的“它无法正确生成节点”是什么意思,您预期会发生什么,以及实际会发生什么?