diff --git a/OPCUaClient/UaClient.cs b/OPCUaClient/UaClient.cs
index f5e5934..f2c1e99 100644
--- a/OPCUaClient/UaClient.cs
+++ b/OPCUaClient/UaClient.cs
@@ -99,6 +99,11 @@ public bool IsConnected
return this.Session.Connected;
}
}
+
+ ///
+ /// Namespace index (namespace index = n1 in ns=n1;s=n2).
+ ///
+ public ushort NamespaceIndex { get; set; } = 2;
#endregion
#region Public methods
@@ -256,6 +261,51 @@ public void Disconnect()
}
}
+ /// Set the namespace index using an identifier.
+ /// BrowseName of the OPC-UA object.
+ public void SetNamespaceIndexFromIdentifier(string identifier)
+ {
+ this.Session.Browse(
+ null,
+ null,
+ Opc.Ua.ObjectIds.ObjectsFolder,
+ 0u,
+ BrowseDirection.Forward,
+ ReferenceTypeIds.HierarchicalReferences,
+ true,
+ (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method,
+ out byte[] continuationPoint,
+ out ReferenceDescriptionCollection refdescs);
+
+ foreach (var item in refdescs)
+ {
+ if (item.DisplayName.Text.Equals(identifier))
+ {
+ this.NamespaceIndex = item.BrowseName.NamespaceIndex;
+ return;
+ }
+ }
+ throw new ArgumentException("Namespace index not found for identifier: ", nameof(identifier));
+ }
+
+ /// Set the namespace index using the namespace uri.
+ /// The namespace uri
+ public void SetNamespaceIndexFromUri(string uri)
+ {
+ //Get the namespace index of the specified namespace uri
+ int namespaceIndex = this.Session.NamespaceUris.GetIndex(uri);
+ //If the namespace uri doesn't exist, namespace index is -1
+ if (namespaceIndex >= 0)
+ {
+ this.NamespaceIndex = (ushort)namespaceIndex;
+ }
+ else
+ {
+ throw new ArgumentException("Namespace index not found for uri: ", nameof(uri));
+
+ }
+ }
+
///
/// Write a value on a tag
@@ -273,7 +323,7 @@ public void Write(String address, Object value)
WriteValueCollection writeValues = new WriteValueCollection();
var writeValue = new WriteValue
{
- NodeId = new NodeId(address, 2),
+ NodeId = new NodeId(address, this.NamespaceIndex),
AttributeId = Attributes.Value,
Value = new DataValue()
};
@@ -319,7 +369,7 @@ public Tag Read(String address)
{
new ReadValueId
{
- NodeId = new NodeId(address, 2),
+ NodeId = new NodeId(address, this.NamespaceIndex),
AttributeId = Attributes.Value
}
};
@@ -335,7 +385,7 @@ public Tag Read(String address)
///
- /// Write a lis of values
+ /// Write a list of values
///
///
///
@@ -347,7 +397,7 @@ public void Write(List tags)
writeValues.AddRange(tags.Select(tag => new WriteValue
{
- NodeId = new NodeId(tag.Address, 2),
+ NodeId = new NodeId(tag.Address, this.NamespaceIndex),
AttributeId = Attributes.Value,
Value = new DataValue()
{
@@ -383,7 +433,7 @@ public List Read(List address)
ReadValueIdCollection readValues = new ReadValueIdCollection();
readValues.AddRange(address.Select(a => new ReadValueId
{
- NodeId = new NodeId(a, 2),
+ NodeId = new NodeId(a, this.NamespaceIndex),
AttributeId = Attributes.Value
}));
@@ -419,7 +469,7 @@ public void Monitoring(String address, int miliseconds, MonitoredItemNotificatio
{
var subscription = this.Subscription(miliseconds);
MonitoredItem monitored = new MonitoredItem();
- monitored.StartNodeId = new NodeId(address, 2);
+ monitored.StartNodeId = new NodeId(address, this.NamespaceIndex);
monitored.AttributeId = Attributes.Value;
monitored.Notification += monitor;
subscription.AddItem(monitored);
@@ -482,7 +532,7 @@ public List Groups(String address, bool recursive = false)
browser.NodeClassMask = (int)NodeClass.Object | (int)NodeClass.Variable;
browser.ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences;
- ReferenceDescriptionCollection browseResults = browser.Browse(new NodeId(address, 2));
+ ReferenceDescriptionCollection browseResults = browser.Browse(new NodeId(address, this.NamespaceIndex));
foreach (var result in browseResults)
{
if (result.NodeClass == NodeClass.Object)
@@ -522,7 +572,7 @@ public List Tags(String address)
browser.NodeClassMask = (int)NodeClass.Object | (int)NodeClass.Variable;
browser.ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences;
- ReferenceDescriptionCollection browseResults = browser.Browse(new NodeId(address, 2));
+ ReferenceDescriptionCollection browseResults = browser.Browse(new NodeId(address, this.NamespaceIndex));
foreach (var result in browseResults)
{
if (result.NodeClass == NodeClass.Variable)
@@ -600,7 +650,7 @@ public Task> GroupsAsync(String address, bool recursive = false)
browser.NodeClassMask = (int)NodeClass.Object | (int)NodeClass.Variable;
browser.ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences;
- ReferenceDescriptionCollection browseResults = browser.Browse(new NodeId(address, 2));
+ ReferenceDescriptionCollection browseResults = browser.Browse(new NodeId(address, this.NamespaceIndex));
foreach (var result in browseResults)
{
if (result.NodeClass == NodeClass.Object)
@@ -643,7 +693,7 @@ public Task> TagsAsync(String address)
browser.NodeClassMask = (int)NodeClass.Object | (int)NodeClass.Variable;
browser.ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences;
- ReferenceDescriptionCollection browseResults = browser.Browse(new NodeId(address, 2));
+ ReferenceDescriptionCollection browseResults = browser.Browse(new NodeId(address, this.NamespaceIndex));
foreach (var result in browseResults)
{
if (result.NodeClass == NodeClass.Variable)
@@ -676,7 +726,7 @@ public async Task WriteAsync(String address, Object value)
WriteValueCollection writeValues = new WriteValueCollection();
var writeValue = new WriteValue
{
- NodeId = new NodeId(address, 2),
+ NodeId = new NodeId(address, this.NamespaceIndex),
AttributeId = Attributes.Value,
Value = new DataValue()
};
@@ -720,7 +770,7 @@ public async Task> WriteAsync(List tags)
writeValues.AddRange(tags.Select(tag => new WriteValue
{
- NodeId = new NodeId(tag.Address, 2),
+ NodeId = new NodeId(tag.Address, this.NamespaceIndex),
AttributeId = Attributes.Value,
Value = new DataValue()
{
@@ -760,7 +810,7 @@ public async Task ReadAsync(String address)
{
new ReadValueId
{
- NodeId = new NodeId(address, 2),
+ NodeId = new NodeId(address, this.NamespaceIndex),
AttributeId = Attributes.Value
}
};
@@ -790,7 +840,7 @@ public async Task> ReadAsync(List address)
ReadValueIdCollection readValues = new ReadValueIdCollection();
readValues.AddRange(address.Select(a => new ReadValueId
{
- NodeId = new NodeId(a, 2),
+ NodeId = new NodeId(a, this.NamespaceIndex),
AttributeId = Attributes.Value
}));
diff --git a/README.md b/README.md
index 57c553a..42fcb21 100644
--- a/README.md
+++ b/README.md
@@ -42,6 +42,21 @@ dotnet add package OPCUaClient
```
+### Change the namespace index (default namespace index is 2)
+
+```cs
+// Fixed namespace index
+client.NamespaceIndex = 3;
+
+// From URI
+client.SetNamespaceIndexFromUri("urn://N44/Ua/Device1");
+
+// From identifier
+client.SetNamespaceIndexFromIdentifier("ua-server-identifier");
+
+```
+
+
### Read a tag
```cs