static void BrowseAndReadFromNode([NotNull] string parentItemId) { // Obtain all node elements under parentItemId var browseParameters = new DABrowseParameters(); // no filtering whatsoever DANodeElementCollection nodeElementCollection = Client.BrowseNodes("", ServerClass, parentItemId, browseParameters); // Remark: that BrowseNodes(...) may also throw OpcException; a production code should contain handling for it, here // omitted for brevity. Boolean append = false; if (parentItemId.ToString() == "") { append = false; } else { append = true; } foreach (DANodeElement nodeElement in nodeElementCollection) { Debug.Assert(nodeElement != null); using (StreamWriter opcfile = new System.IO.StreamWriter(@"C:\Users\kiekensk\source\repos\ConsoleAppOPCClient\ConsoleAppOPCClient\dump\Cttmt2008 OPC values.txt", append)) { // If the node is a leaf, it might be possible to read from it if (nodeElement.IsLeaf) { // Determine what the display - either the value read, or exception message in case of failure. string display; try { object value = Client.ReadItemValue("", ServerClass, nodeElement); display = String.Format("{0}", value); } catch (OpcException exception) { display = String.Format("** {0} **", exception.GetBaseException().Message); } Console.WriteLine("{0} -> {1}", nodeElement.ItemId, display); opcfile.WriteLine("{0} -> {1}", nodeElement.ItemId, display); } // If the node is not a leaf, just display its itemId else { Console.WriteLine("{0}", nodeElement.ItemId); opcfile.WriteLine("{0}", nodeElement.ItemId); } } // If the node is a branch, browse recursively into it. if (nodeElement.IsBranch && (nodeElement.ItemId != "SimulateEvents") /* this branch is too big for the purpose of this example */) { BrowseAndReadFromNode(nodeElement); } } }
private static void BrowseFromNode( EasyDAClient client, ServerDescriptor serverDescriptor, DANodeDescriptor parentNodeDescriptor) { Debug.Assert(client != null); Debug.Assert(serverDescriptor != null); Debug.Assert(parentNodeDescriptor != null); Boolean append = false; if (parentNodeDescriptor.ToString() == "") { append = false; } else { append = true; } // Obtain all node elements under parentNodeDescriptor var browseParameters = new DABrowseParameters(); // no filtering whatsoever DANodeElementCollection nodeElementCollection = client.BrowseNodes(serverDescriptor, parentNodeDescriptor, browseParameters); // Remark: that BrowseNodes(...) may also throw OpcException; a production code should contain handling for // it, here omitted for brevity. foreach (DANodeElement nodeElement in nodeElementCollection) { Debug.Assert(nodeElement != null); Console.WriteLine(nodeElement); using (StreamWriter opcfile = new System.IO.StreamWriter(@"C:\Users\kiekensk\source\repos\ConsoleAppOPCClient\ConsoleAppOPCClient\dump\Cttmt2008 OPC dump.txt", append)) { opcfile.WriteLine(nodeElement); } // If the node is a branch, browse recursively into it. if (nodeElement.IsBranch) { _branchCount++; BrowseFromNode(client, serverDescriptor, nodeElement); } else { _leafCount++; } } }
static void BrowseAndReadFromNode([NotNull] string parentItemId) { // Obtain all node elements under parentItemId var nodeFilter = new DANodeFilter(); // no filtering whatsoever DANodeElementCollection nodeElementCollection = Client.BrowseNodes("", ServerClass, parentItemId, nodeFilter); // Remark: that BrowseNodes(...) may also throw OpcException; a production code should contain handling for it, here // omitted for brevity. foreach (DANodeElement nodeElement in nodeElementCollection) { Debug.Assert(nodeElement != null); // If the node is a leaf, it might be possible to read from it if (nodeElement.IsLeaf) { // Determine what the display - either the value read, or exception message in case of failure. string display; try { object value = Client.ReadItemValue("", ServerClass, nodeElement); display = String.Format("{0}", value); } catch (OpcException exception) { display = String.Format("** {0} **", exception.GetBaseException().Message); } Console.WriteLine("{0} -> {1}", nodeElement.ItemId, display); } // If the node is not a leaf, just display its itemId else { Console.WriteLine("{0}", nodeElement.ItemId); } // If the node is a branch, browse recursively into it. if (nodeElement.IsBranch && (nodeElement.ItemId != "SimulateEvents") /* this branch is too big for the purpose of this example */) { BrowseAndReadFromNode(nodeElement); } } }
public static void DataTypes() { var easyDAClient = new EasyDAClient(); // Define the list of data types we will be checking for. // Change as needed for your application. // This technique is only usable if there is a known list of // data types you are interested in. If you are interested in // all leaves, even those that are of data types not explicitly // listed, always include VarTypes.Empty as the first data type. // The leaves of "unlisted" data types will have VarTypes.Empty // associated with them. var dataTypes = new VarType[] { VarTypes.Empty, VarTypes.I2, VarTypes.R4 }; // For each leaf found, this dictionary wil hold its associated data type. var dataTypeDictionary = new Dictionary <DANodeElement, VarType>(); // For each data type, browse for leaves of this data type. foreach (VarType dataType in dataTypes) { var browseParameters = new DABrowseParameters(DABrowseFilter.Leaves, "", "", dataType); DANodeElementCollection nodeElements = easyDAClient.BrowseNodes("", "AutoJet.ACPFileServerDA.1", "Cttmt2008", browseParameters); // Store the leaf information into the dictionary, and // associate the current data type with it. foreach (var nodeElement in nodeElements) { dataTypeDictionary[nodeElement] = dataType; } } // Display each leaf found, and its associated data type. foreach (KeyValuePair <DANodeElement, VarType> pair in dataTypeDictionary) { DANodeElement nodeElement = pair.Key; VarType dataType = pair.Value; Console.WriteLine("{0}: {1}", nodeElement, dataType); } }
public static void DataType() { var easyDAClient = new EasyDAClient(); ServerDescriptor serverDescriptor = "AutoJet.ACPFileServerDA.1"; // Browse for all leaves under the "Simulation" branch DANodeElementCollection nodeElementCollection = easyDAClient.BrowseLeaves(serverDescriptor, "Simulation"); // Create list of node descriptors, one for each leaf obtained DANodeDescriptor[] nodeDescriptorArray = nodeElementCollection .Where(element => !element.IsHint) // filter out hint leafs that do not represent real OPC items (rare) .Select(element => new DANodeDescriptor(element)) .ToArray(); // Get the value of DataType property; it is a 16-bit signed integer ValueResult[] valueResultArray = easyDAClient.GetMultiplePropertyValues(serverDescriptor, nodeDescriptorArray, DAPropertyIds.DataType); for (int i = 0; i < valueResultArray.Length; i++) { DANodeDescriptor nodeDescriptor = nodeDescriptorArray[i]; // Check if there has been an error getting the property value ValueResult valueResult = valueResultArray[i]; if (valueResult.Exception != null) { Console.WriteLine("{0}: *** {1}", nodeDescriptor.NodeId, valueResult.Exception.Message); continue; } // Convert the data type to VarType var varType = (VarType)(short)valueResult.Value; // Display the obtained data type Console.WriteLine("{0}: {1}", nodeDescriptor.ItemId, varType); } }