private ReferenceDescriptionCollection GetReferenceDescriptionCollection(NodeId sourceId) { TaskCompletionSource <ReferenceDescriptionCollection> task = new TaskCompletionSource <ReferenceDescriptionCollection>(); // find all of the components of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.Aggregates; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable | NodeClass.Method | NodeClass.ReferenceType | NodeClass.ObjectType | NodeClass.View | NodeClass.VariableType | NodeClass.DataType); nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; // find all nodes organized by the node. BrowseDescription nodeToBrowse2 = new BrowseDescription(); nodeToBrowse2.NodeId = sourceId; nodeToBrowse2.BrowseDirection = BrowseDirection.Forward; nodeToBrowse2.ReferenceTypeId = ReferenceTypeIds.Organizes; nodeToBrowse2.IncludeSubtypes = true; nodeToBrowse2.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable | NodeClass.Method | NodeClass.View | NodeClass.ReferenceType | NodeClass.ObjectType | NodeClass.VariableType | NodeClass.DataType); nodeToBrowse2.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); nodesToBrowse.Add(nodeToBrowse2); // fetch references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(m_OpcUaClient.Session, nodesToBrowse, false); return(references); }
/// <summary> /// 浏览一个节点的引用 /// </summary> /// <param name="tag">节点值</param> /// <returns>引用节点描述</returns> public ReferenceDescription[] BrowseNodeReference(string tag) { NodeId sourceId = new NodeId(tag); // 该节点可以读取到方法 BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.Aggregates; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable | NodeClass.Method); nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; // 该节点无论怎么样都读取不到方法 // find all nodes organized by the node. BrowseDescription nodeToBrowse2 = new BrowseDescription(); nodeToBrowse2.NodeId = sourceId; nodeToBrowse2.BrowseDirection = BrowseDirection.Forward; nodeToBrowse2.ReferenceTypeId = ReferenceTypeIds.Organizes; nodeToBrowse2.IncludeSubtypes = true; nodeToBrowse2.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable); nodeToBrowse2.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); nodesToBrowse.Add(nodeToBrowse2); // fetch references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodesToBrowse, false); return(references.ToArray()); }
/// <summary> /// Populates the branch in the tree view. /// </summary> /// <param name="sourceId">The NodeId of the Node to browse.</param> /// <param name="nodes">The node collect to populate.</param> private async Task PopulateBranch(NodeId sourceId, TreeNodeCollection nodes) { try { nodes.Clear(); // find all of the components of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.Aggregates; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable); nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; // find all nodes organized by the node. BrowseDescription nodeToBrowse2 = new BrowseDescription(); nodeToBrowse2.NodeId = sourceId; nodeToBrowse2.BrowseDirection = BrowseDirection.Forward; nodeToBrowse2.ReferenceTypeId = ReferenceTypeIds.Organizes; nodeToBrowse2.IncludeSubtypes = true; nodeToBrowse2.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable); nodeToBrowse2.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); nodesToBrowse.Add(nodeToBrowse2); Cursor.Current = Cursors.WaitCursor; // fetch references from the server. ReferenceDescriptionCollection references = await Task.Run(() => FormUtils.Browse(m_groupLog.Server.Connection.Session, nodesToBrowse, false)); Cursor.Current = Cursors.Default; // process results. for (int ii = 0; ii < references.Count; ii++) { ReferenceDescription target = references[ii]; // add node. TreeNode child = new TreeNode(Utils.Format("{0}", target)); child.Tag = target; child.Nodes.Add(new TreeNode()); nodes.Add(child); if (target.NodeClass == NodeClass.Variable) { child.ImageKey = "label_icon"; child.SelectedImageKey = "label_icon"; } } } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary>浏览一个节点的引用</summary> /// <param name="tag"></param> /// <returns></returns> public ReferenceDescription[] BrowseNodeReference(string tag) { NodeId nodeId = new NodeId(tag); BrowseDescription browseDescription1 = new BrowseDescription(); browseDescription1.NodeId = nodeId; browseDescription1.BrowseDirection = BrowseDirection.Forward; browseDescription1.ReferenceTypeId = ReferenceTypeIds.Aggregates; browseDescription1.IncludeSubtypes = true; browseDescription1.NodeClassMask = 7U; browseDescription1.ResultMask = 63U; BrowseDescription browseDescription2 = new BrowseDescription(); browseDescription2.NodeId = nodeId; browseDescription2.BrowseDirection = BrowseDirection.Forward; browseDescription2.ReferenceTypeId = ReferenceTypeIds.Organizes; browseDescription2.IncludeSubtypes = true; browseDescription2.NodeClassMask = 3U; browseDescription2.ResultMask = 63U; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(browseDescription1); nodesToBrowse.Add(browseDescription2); return(FormUtils.Browse(this.m_session, nodesToBrowse, false).ToArray()); }
/// <summary> /// 读取一个节点的指定属性 /// </summary> /// <param name="nodeId"></param> /// <param name="attribute"></param> /// <returns></returns> private DataValue ReadNoteDataValueAttributes(NodeId nodeId, uint attribute) { NodeId sourceId = nodeId; ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = sourceId; nodeToRead.AttributeId = attribute; nodesToRead.Add(nodeToRead); int startOfProperties = nodesToRead.Count; // find all of the pror of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.HasProperty; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = 0; nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); // fetch property references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(CommonMethods.opcUaClient.Session, nodesToBrowse, false); if (references == null) { return(null); } for (int ii = 0; ii < references.Count; ii++) { // ignore external references. if (references[ii].NodeId.IsAbsolute) { continue; } ReadValueId nodeToRead2 = new ReadValueId(); nodeToRead2.NodeId = (NodeId)references[ii].NodeId; nodeToRead2.AttributeId = Attributes.Value; nodesToRead.Add(nodeToRead2); } // read all values. DataValueCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; CommonMethods.opcUaClient.Session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); return(results[0]); }
/// <summary> /// Populates the branch in the tree view. /// </summary> /// <param name="sourceId">The NodeId of the Node to browse.</param> /// <param name="nodes">The node collect to populate.</param> private void PopulateBranch(NodeId sourceId, TreeNodeCollection nodes) { try { nodes.Clear(); // find all of the components of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.Aggregates; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable); nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; // find all nodes organized by the node. BrowseDescription nodeToBrowse2 = new BrowseDescription(); nodeToBrowse2.NodeId = sourceId; nodeToBrowse2.BrowseDirection = BrowseDirection.Forward; nodeToBrowse2.ReferenceTypeId = ReferenceTypeIds.Organizes; nodeToBrowse2.IncludeSubtypes = true; nodeToBrowse2.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable); nodeToBrowse2.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); nodesToBrowse.Add(nodeToBrowse2); // fetch references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodesToBrowse, false); // process results. for (int ii = 0; ii < references.Count; ii++) { ReferenceDescription target = references[ii]; // add node. TreeNode child = new TreeNode(Utils.Format("{0}", target)); child.Tag = target; child.Nodes.Add(new TreeNode()); nodes.Add(child); } // update the attributes display. DisplayAttributes(sourceId); } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary>读取一个节点的所有属性</summary> /// <param name="tag"></param> /// <returns></returns> //public OpcuaNodeAttribute[] ReadNoteAttributes(string tag) //{ // NodeId nodeId = new NodeId(tag); // ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); // for (uint index = 2; index <= 22U; ++index) // nodesToRead.Add(new ReadValueId() // { // NodeId = nodeId, // AttributeId = index // }); // int count = nodesToRead.Count; // BrowseDescription browseDescription = new BrowseDescription(); // browseDescription.NodeId = nodeId; // browseDescription.BrowseDirection = BrowseDirection.Forward; // browseDescription.ReferenceTypeId = ReferenceTypeIds.HasProperty; // browseDescription.IncludeSubtypes = true; // browseDescription.NodeClassMask = 0U; // browseDescription.ResultMask = 63U; // BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); // nodesToBrowse.Add(browseDescription); // ReferenceDescriptionCollection descriptionCollection = FormUtils.Browse(this.m_session, nodesToBrowse, false); // if (descriptionCollection == null) // return new OpcuaNodeAttribute[0]; // for (int index = 0; index < descriptionCollection.Count; ++index) // { // if (!descriptionCollection[index].NodeId.IsAbsolute) // nodesToRead.Add(new ReadValueId() // { // NodeId = (NodeId)descriptionCollection[index].NodeId, // AttributeId = 13U // }); // } // DataValueCollection results = (DataValueCollection)null; // DiagnosticInfoCollection diagnosticInfos = (DiagnosticInfoCollection)null; // this.m_session.Read((RequestHeader)null, 0.0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); // ClientBase.ValidateResponse((IList)results, (IList)nodesToRead); // ClientBase.ValidateDiagnosticInfos(diagnosticInfos, (IList)nodesToRead); // List<OpcNodeAttribute> opcNodeAttributeList = new List<OpcNodeAttribute>(); // for (int index = 0; index < results.Count; ++index) // { // OpcNodeAttribute opcNodeAttribute1 = new OpcNodeAttribute(); // BuiltInType builtInType; // if (index < count) // { // if (!(results[index].StatusCode == 2150957056U)) // { // opcNodeAttribute1.Name = Attributes.GetBrowseName(nodesToRead[index].AttributeId); // if (StatusCode.IsBad(results[index].StatusCode)) // { // opcNodeAttribute1.Type = Utils.Format("{0}", (object)Attributes.GetDataTypeId(nodesToRead[index].AttributeId)); // opcNodeAttribute1.Value = (object)Utils.Format("{0}", (object)results[index].StatusCode); // } // else // { // TypeInfo typeInfo = TypeInfo.Construct(results[index].Value); // OpcNodeAttribute opcNodeAttribute2 = opcNodeAttribute1; // builtInType = typeInfo.BuiltInType; // string str = builtInType.ToString(); // opcNodeAttribute2.Type = str; // if (typeInfo.ValueRank >= 0) // opcNodeAttribute1.Type += "[]"; // opcNodeAttribute1.Value = results[index].Value; // } // } // else // continue; // } // else if (!(results[index].StatusCode == 2150891520U)) // { // opcNodeAttribute1.Name = Utils.Format("{0}", (object)descriptionCollection[index - count]); // if (StatusCode.IsBad(results[index].StatusCode)) // { // opcNodeAttribute1.Type = string.Empty; // opcNodeAttribute1.Value = (object)Utils.Format("{0}", (object)results[index].StatusCode); // } // else // { // TypeInfo typeInfo = TypeInfo.Construct(results[index].Value); // OpcNodeAttribute opcNodeAttribute2 = opcNodeAttribute1; // builtInType = typeInfo.BuiltInType; // string str = builtInType.ToString(); // opcNodeAttribute2.Type = str; // if (typeInfo.ValueRank >= 0) // opcNodeAttribute1.Type += "[]"; // opcNodeAttribute1.Value = results[index].Value; // } // } // else // continue; // opcNodeAttributeList.Add(opcNodeAttribute1); // } // return opcNodeAttributeList.ToArray(); //} /// <summary>读取一个节点的所有属性</summary> /// <param name="tag"></param> /// <returns></returns> public DataValue[] ReadNoteDataValueAttributes(string tag) { NodeId nodeId = new NodeId(tag); ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); for (uint index = 1; index <= 22U; ++index) { nodesToRead.Add(new ReadValueId() { NodeId = nodeId, AttributeId = index }); } int count = nodesToRead.Count; BrowseDescription browseDescription = new BrowseDescription(); browseDescription.NodeId = nodeId; browseDescription.BrowseDirection = BrowseDirection.Forward; browseDescription.ReferenceTypeId = ReferenceTypeIds.HasProperty; browseDescription.IncludeSubtypes = true; browseDescription.NodeClassMask = 0U; browseDescription.ResultMask = 63U; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(browseDescription); ReferenceDescriptionCollection descriptionCollection = FormUtils.Browse(this.m_session, nodesToBrowse, false); if (descriptionCollection == null) { return(new DataValue[0]); } for (int index = 0; index < descriptionCollection.Count; ++index) { if (!descriptionCollection[index].NodeId.IsAbsolute) { nodesToRead.Add(new ReadValueId() { NodeId = (NodeId)descriptionCollection[index].NodeId, AttributeId = 13U }); } } DataValueCollection results = (DataValueCollection)null; DiagnosticInfoCollection diagnosticInfos = (DiagnosticInfoCollection)null; this.m_session.Read((RequestHeader)null, 0.0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); ClientBase.ValidateResponse((IList)results, (IList)nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, (IList)nodesToRead); return(results.ToArray()); }
/// <summary> /// Handles the BeforeExpand event of the BrowseTV control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param> private void BrowseTV_BeforeExpand(object sender, TreeViewCancelEventArgs e) { try { ReferenceDescription reference = (ReferenceDescription)e.Node.Tag; e.Node.Nodes.Clear(); // browse HasEventSource to display the sources but it won't be possible to select them. BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = Opc.Ua.ObjectTypeIds.BaseEventType; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = ReferenceTypeIds.HasSubtype; nodeToBrowse.IncludeSubtypes = false; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; if (reference != null) { nodeToBrowse.NodeId = (NodeId)reference.NodeId; } // add the childen to the control. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodeToBrowse, false); for (int ii = 0; ii < references.Count; ii++) { reference = references[ii]; // ignore out of server references. if (reference.NodeId.IsAbsolute) { continue; } TreeNode child = new TreeNode(reference.ToString()); child.Nodes.Add(new TreeNode()); child.Tag = reference; e.Node.Nodes.Add(child); } } catch (Exception exception) { MessageBox.Show(exception.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Displays the attributes and properties in the attributes view. /// </summary> /// <param name="sourceId">The NodeId of the Node to browse.</param> private void DisplayAttributes(NodeId sourceId) { try { AttributesLV.Items.Clear(); ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); // attempt to read all possible attributes. for (uint ii = Attributes.NodeClass; ii <= Attributes.UserExecutable; ii++) { ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = sourceId; nodeToRead.AttributeId = ii; nodesToRead.Add(nodeToRead); } int startOfProperties = nodesToRead.Count; // find all of the pror of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.HasProperty; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = 0; nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); // fetch property references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodesToBrowse, false); if (references == null) { return; } for (int ii = 0; ii < references.Count; ii++) { // ignore external references. if (references[ii].NodeId.IsAbsolute) { continue; } ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)references[ii].NodeId; nodeToRead.AttributeId = Attributes.Value; nodesToRead.Add(nodeToRead); } // read all values. DataValueCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); // process results. for (int ii = 0; ii < results.Count; ii++) { string name = null; string datatype = null; string value = null; // process attribute value. if (ii < startOfProperties) { // ignore attributes which are invalid for the node. if (results[ii].StatusCode == StatusCodes.BadAttributeIdInvalid) { continue; } // get the name of the attribute. name = Attributes.GetBrowseName(nodesToRead[ii].AttributeId); // display any unexpected error. if (StatusCode.IsBad(results[ii].StatusCode)) { datatype = Utils.Format("{0}", Attributes.GetDataTypeId(nodesToRead[ii].AttributeId)); value = Utils.Format("{0}", results[ii].StatusCode); } // display the value. else { TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value); datatype = typeInfo.BuiltInType.ToString(); if (typeInfo.ValueRank >= ValueRanks.OneOrMoreDimensions) { datatype += "[]"; } value = Utils.Format("{0}", results[ii].Value); } } // process property value. else { // ignore properties which are invalid for the node. if (results[ii].StatusCode == StatusCodes.BadNodeIdUnknown) { continue; } // get the name of the property. name = Utils.Format("{0}", references[ii - startOfProperties]); // display any unexpected error. if (StatusCode.IsBad(results[ii].StatusCode)) { datatype = String.Empty; value = Utils.Format("{0}", results[ii].StatusCode); } // display the value. else { TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value); datatype = typeInfo.BuiltInType.ToString(); if (typeInfo.ValueRank >= ValueRanks.OneOrMoreDimensions) { datatype += "[]"; } value = Utils.Format("{0}", results[ii].Value); } } // add the attribute name/value to the list view. ListViewItem item = new ListViewItem(name); item.SubItems.Add(datatype); item.SubItems.Add(value); AttributesLV.Items.Add(item); } // adjust width of all columns. for (int ii = 0; ii < AttributesLV.Columns.Count; ii++) { AttributesLV.Columns[ii].Width = -2; } } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary> /// 读取一个节点的所有属性 /// </summary> /// <param name="tag">节点值</param> /// <returns>所有的数据</returns> public DataValue[] ReadNoteDataValueAttributes(string tag) { NodeId sourceId = new NodeId(tag); ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); // attempt to read all possible attributes. // 尝试着去读取所有可能的特性 for (uint ii = Attributes.NodeId; ii <= Attributes.UserExecutable; ii++) { ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = sourceId; nodeToRead.AttributeId = ii; nodesToRead.Add(nodeToRead); } int startOfProperties = nodesToRead.Count; // find all of the pror of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.HasProperty; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = 0; nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); // fetch property references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodesToBrowse, false); if (references == null) { return(new DataValue[0]); } for (int ii = 0; ii < references.Count; ii++) { // ignore external references. if (references[ii].NodeId.IsAbsolute) { continue; } ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)references[ii].NodeId; nodeToRead.AttributeId = Attributes.Value; nodesToRead.Add(nodeToRead); } // read all values. DataValueCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); return(results.ToArray()); }
/// <summary> /// 读取一个节点的所有属性 /// </summary> /// <param name="tag">节点信息</param> /// <returns>节点的特性值</returns> public OpcNodeAttribute[] ReadNoteAttributes(string tag) { NodeId sourceId = new NodeId(tag); ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); // attempt to read all possible attributes. // 尝试着去读取所有可能的特性 for (uint ii = Attributes.NodeClass; ii <= Attributes.UserExecutable; ii++) { ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = sourceId; nodeToRead.AttributeId = ii; nodesToRead.Add(nodeToRead); } int startOfProperties = nodesToRead.Count; // find all of the pror of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.HasProperty; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = 0; nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); // fetch property references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodesToBrowse, false); if (references == null) { return(new OpcNodeAttribute[0]); } for (int ii = 0; ii < references.Count; ii++) { // ignore external references. if (references[ii].NodeId.IsAbsolute) { continue; } ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)references[ii].NodeId; nodeToRead.AttributeId = Attributes.Value; nodesToRead.Add(nodeToRead); } // read all values. DataValueCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); // process results. List <OpcNodeAttribute> nodeAttribute = new List <OpcNodeAttribute>(); for (int ii = 0; ii < results.Count; ii++) { OpcNodeAttribute item = new OpcNodeAttribute(); // process attribute value. if (ii < startOfProperties) { // ignore attributes which are invalid for the node. if (results[ii].StatusCode == StatusCodes.BadAttributeIdInvalid) { continue; } // get the name of the attribute. item.Name = Attributes.GetBrowseName(nodesToRead[ii].AttributeId); // display any unexpected error. if (StatusCode.IsBad(results[ii].StatusCode)) { item.Type = Utils.Format("{0}", Attributes.GetDataTypeId(nodesToRead[ii].AttributeId)); item.Value = Utils.Format("{0}", results[ii].StatusCode); } // display the value. else { TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value); item.Type = typeInfo.BuiltInType.ToString(); if (typeInfo.ValueRank >= ValueRanks.OneOrMoreDimensions) { item.Type += "[]"; } item.Value = results[ii].Value;//Utils.Format("{0}", results[ii].Value); } } // process property value. else { // ignore properties which are invalid for the node. if (results[ii].StatusCode == StatusCodes.BadNodeIdUnknown) { continue; } // get the name of the property. item.Name = Utils.Format("{0}", references[ii - startOfProperties]); // display any unexpected error. if (StatusCode.IsBad(results[ii].StatusCode)) { item.Type = String.Empty; item.Value = Utils.Format("{0}", results[ii].StatusCode); } // display the value. else { TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value); item.Type = typeInfo.BuiltInType.ToString(); if (typeInfo.ValueRank >= ValueRanks.OneOrMoreDimensions) { item.Type += "[]"; } item.Value = results[ii].Value; //Utils.Format("{0}", results[ii].Value); } } nodeAttribute.Add(item); } return(nodeAttribute.ToArray()); }