Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions OPCUaClient/UaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public bool IsConnected
return this.Session.Connected;
}
}

/// <summary>
/// Namespace index (namespace index = n1 in ns=n1;s=n2).
/// </summary>
public ushort NamespaceIndex { get; set; } = 2;
#endregion

#region Public methods
Expand Down Expand Up @@ -256,6 +261,51 @@ public void Disconnect()
}
}

/// <summary>Set the namespace index using an identifier.</summary>
/// <param name="identifier">BrowseName of the OPC-UA object.</param>
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));
}

/// <summary>Set the namespace index using the namespace uri.</summary>
/// <param name="uri">The namespace uri </param>
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));

}
}


/// <summary>
/// Write a value on a tag
Expand All @@ -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()
};
Expand Down Expand Up @@ -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
}
};
Expand All @@ -335,7 +385,7 @@ public Tag Read(String address)


/// <summary>
/// Write a lis of values
/// Write a list of values
/// </summary>
/// <param name="tags"> <see cref="Tag"/></param>
/// <exception cref="WriteException"></exception>
Expand All @@ -347,7 +397,7 @@ public void Write(List<Tag> 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()
{
Expand Down Expand Up @@ -383,7 +433,7 @@ public List<Tag> Read(List<String> 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
}));

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -482,7 +532,7 @@ public List<Group> 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)
Expand Down Expand Up @@ -522,7 +572,7 @@ public List<Tag> 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)
Expand Down Expand Up @@ -600,7 +650,7 @@ public Task<List<Group>> 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)
Expand Down Expand Up @@ -643,7 +693,7 @@ public Task<List<Tag>> 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)
Expand Down Expand Up @@ -676,7 +726,7 @@ public async Task<Tag> 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()
};
Expand Down Expand Up @@ -720,7 +770,7 @@ public async Task<List<Tag>> WriteAsync(List<Tag> 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()
{
Expand Down Expand Up @@ -760,7 +810,7 @@ public async Task<Tag> ReadAsync(String address)
{
new ReadValueId
{
NodeId = new NodeId(address, 2),
NodeId = new NodeId(address, this.NamespaceIndex),
AttributeId = Attributes.Value
}
};
Expand Down Expand Up @@ -790,7 +840,7 @@ public async Task<List<Tag>> ReadAsync(List<String> 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
}));

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down