NumericRange — Reading and Writing Array/Matrix Subranges
Overview
OPC UA Part 4 §7.27 NumericRange denotes a sub-range of the array or matrix behind the Value property, used by Read / Write to touch only some elements instead of the whole range. Common notations:
| String | Meaning |
|---|---|
| "" | Entire Interval (All) |
| "5" | The 5th element |
| "3:7" | 3rd–7th (inclusive) |
| "1:3,2:4" | Two-dimensional matrix: First dimension [1..3], second dimension [2..4] |
| "0:5,10" | Two-dimensional: First dimension [0..5], second dimension only the 10th element |
API
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| NumericRange.All | Static | Read | The entire interval |
| NumericRange.Single(int) | Factory | — | Single Index |
| NumericRange.Span(start, end) | Factory | — | Range |
| NumericRange.Multi(params NumericRange[]) | Factory | — | Multidimensional |
| NumericRange.Parse(string) | Static | — | Parse a String |
| NumericRange.TryParse(s, out r) | Static | — | Security Analysis |
| range.ToString() | Methods | — | Serialize to a string OPC UA |
| range.Start / End | Properties | Read | Start and End Subscripts (-1 = Entire Interval) |
| range.SubRanges | Properties | Read | Multidimensional Subranges |
| range.IsAll | Properties | Read | Is this the entire interval? |
| ua.ReadRange(nid, range, attr) | Extensions | Read | Read a segment, return DataValue |
| ua.WriteRange(nid, range, value, attr) | Extensions | Write | Write a sub-segment, return StatusCode |
ReadRange / WriteRange is NumericRangeExtensions an extension method (that acts on DarraOpcUa ), with the following signature:
DataValue ReadRange(this DarraOpcUa ua, string nodeId, NumericRange range,
AttributeId attr = AttributeId.Value);
StatusCode WriteRange(this DarraOpcUa ua, string nodeId, NumericRange range,
Variant value, AttributeId attr = AttributeId.Value);
range.IsAll, read and write the entire interval at a time without commas IndexRange; this method works perfectly.Partial text currently unavailable: Currently, there are no read/write entry points that support IndexRange; passing a non-entire-segment range will result in an immediate exception NotImplementedException, along with a Warn event (status code BadNotSupported). There is no fallback mechanism to "pretend it succeeded." When modifying a portion of an array, first read the entire segment, make the changes, and then write the entire segment back.
Code Example
using System;
using DarraOpcUa_Client;
using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Connect();
// 1) Entire segment — Equivalent to a Read without a range
using (var dv = ua.ReadRange("ns=2;s=Arr", NumericRange.All))
{
double[] all = dv.Value.AsDoubleArray ?? Array.Empty<double>();
Console.WriteLine($"全部 {all.Length} 元素");
}
// 2) String Parsing
var r = NumericRange.Parse("3:7");
Console.WriteLine(r.ToString()); // "3:7"
if (NumericRange.TryParse("0:5,10", out var r2D))
Console.WriteLine($"维度数 = {r2D.SubRanges.Length}");
// 3) Scope Construction
var one = NumericRange.Single(3); // "3"
var span = NumericRange.Span(0, 99); // "0:99"
var mat = NumericRange.Multi(NumericRange.Span(0, 9),
NumericRange.Span(0, 9)); // "0:9,0:9"
Console.WriteLine($"{one} / {span} / {mat}");
// 4) Write the entire paragraph
using var v = new Variant();
v.Double = 42.5;
var rc = ua.WriteRange("ns=2;s=Arr", NumericRange.All, v);
if (rc != StatusCode.Good) Console.WriteLine($"写失败: {rc}");
// 5) Sub-segment — Currently unavailable; will throw an exception NotImplementedException
try
{
using var dvSub = ua.ReadRange("ns=2;s=Arr", NumericRange.Span(3, 7));
}
catch (NotImplementedException ex)
{
Console.WriteLine(ex.Message);
}
Error Handling
Invalid Path (does not return a StatusCode):
| Situation | Throw |
|---|---|
ua == null / nodeId == null / value == null | ArgumentNullException |
| Non-block range (Stack—unexported IndexRange entry point) | NotImplementedException |
| Read failed (status != 0) | OpcUaException (Bring StatusCode) |
WriteRange Do not throw business errors, but instead returns a status code to the caller:
| StatusCode | Meaning |
|---|---|
| Good | Success |
| BadTypeMismatch | The value being written does not match DataType the node |
| BadNotWritable | The "Value" node is read-only. |
The server may also return status codes such BadNodeIdUnknown as depending on its implementation.BadNotSupported This appears only in the "Subsection Unavailable" Warn event (OpcUaEventCategory.Write / Read) and is not the return value of WriteRange.
Best Practices
- Don't parse it every time —
NumericRange.Span(0, 99)thanNumericRange.Parse("0:99")directly - Read the entire interval first, then revise and rewrite the entire interval. — Sub-section entry is currently unavailable; for minor changes, follow the "read → edit → write back" process.
- Scope Using Factory Construction —
Single/Span/ Indexes and dimension codes have alreadyMultibeen handled; do not manually construct strings
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| NumericRange | NumericRange | NumericRange | NumericRange | NumericRange | DarraUa_NumericRange |
| ReadRange | read_range | readRange | ReadRange | read_range | DarraUa_Session_ReadRange |
| WriteRange | write_range | writeRange | WriteRange | write_range | DarraUa_Session_WriteRange |