Add MonitoredItem
- First, create a subscription. See Create a Subscription.
- For the DataChanged event field, please see DataChanged Event.
- To change the sampling parameters, see Modify.
sub.Add(...)
public uint Add(string nodeIdString, AttributeId attribute = AttributeId.Value, double samplingIntervalMs = -1.0);
| Parameters | Default | Note |
|---|---|---|
| nodeIdString | — | Monitored NodeId |
| attribute | Value (13) | Monitored Attributes |
| samplingIntervalMs | -1 (Subscribe) | Server sampling interval. 0 = As soon as possible |
Returns: uint MonitoredItem handle (local handle, used for subsequent Modify / Remove operations)
Exception: OpcUaException (BadNodeIdUnknown etc.)
uint h = sub.Add("ns=2;s=Temperature");
uint h2 = sub.Add("ns=2;s=Pressure", AttributeId.Value, samplingIntervalMs: 100);
sub.AddMany(nodeIds, attribute)
Add RPC multiple items at once MI; if you use a loop to add them, each item is added one at a time MI RPC:
public IReadOnlyList<(uint handle, StatusCode status)> AddMany(
IReadOnlyList<string> nodeIds,
AttributeId attribute = AttributeId.Value);
var nodes = Enumerable.Range(1, 100).Select(i => $"ns=2;s=T{i}").ToList();
var results = sub.AddMany(nodes);
for (int i = 0; i < nodes.Count; i++)
{
if (results[i].status == StatusCode.Good)
Console.WriteLine($" {nodes[i]} -> handle={results[i].handle}");
else
Console.WriteLine($" {nodes[i]} FAILED: {results[i].status}");
}
Performance: AddMany One request plus N requests MI; the loop Add executes MI one request for each. The total execution time varies depending on the server and network conditions; please test it in your own environment.
DataChanged event args
Subscription callback parameters DataChangeEventArgs:
| Field | Type | Note |
|---|---|---|
| MonitoredItemHandle | uint | Triggered MI Handle |
| NodeId | string | It's about time MI NodeId |
| ValueString | string | String representation of the value (provided during the callback; thread-safe) |
| DataTypeName | string | Names of built-in data type enumerations (e.g., Double) |
| Status | StatusCode | DataValue Status |
| SourceTimestamp | DateTime? | Data Source Timestamp |
| ServerTimestamp | DateTime? | Server Timestamp |
ValueString / DataTypeName: Data has already been retrieved by the time the callback is triggered, ensuring safe cross-thread usage. Within the callback,Unable to obtain the original DataValue, If you need the original value, re-read ua.Read(e.NodeId) it in the callback (be sure DataValue to Dispose the returned value).
Remove
sub.Remove(h2); // h2 It is the return value of Add
sub.RemoveByNodeId("ns=2;s=Temperature"); // Press NodeId to remove all matching items
Monitoring Different Attributes
attribute Take AttributeId the enumeration; commonly used Value(13); other properties can also be monitored:
sub.Add("ns=2;s=T1", AttributeId.Value); // Value Changes
sub.Add("ns=2;s=T1", AttributeId.DataType); // Changes in Data Type Nodes (Rare)
Value Whether non-attribute properties can be monitored is determined by the server; if the server refuses, an exception is thrown Add OpcUaException.
DataChangeFilter (Dead Zone)
There is a heavy load with a filter, butThe filter will not be sent to the server at this time. — See DataChangeFilter:
public uint Add(string nodeId, AttributeId attr, double samplingIntervalMs,
MonitoringMode mode, uint queueSize, bool discardOldest,
DataChangeFilter filter);
If you do not pass a filter (or use the basic overload described above), all changes are pushed. Even if you pass a filter, the data will not be sent to the server; instead, it will be reported in full, and you will need to filter it yourself in the callback.