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:
| String | Meaning |
|---|---|
| "" | The entire interval |
| "5" | The 5th element |
| "3:7" | 3–7 |
| "1:3,2:4" | Two-dimensional: First dimension [1..3], Second dimension [2..4] |
API
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| NumericRange.all() | Static Methods | — | Entire Sentinel Segment (start=end=-1) |
| NumericRange.single(index) | Factory | — | Single Index (i:i) |
NumericRange.span(start, end) | Factory | — | Interval [start..end] |
| NumericRange.multi(*dims) | Factory | — | Multidimensional |
| NumericRange.parse(s) | Static Methods | — | Parse, throw on failure ValueError |
| NumericRange.try_parse(s) | Static Methods | — | Security analysis, failure return None (returns an empty string all()) |
| str(r) | — | — | Serialization (__str__, returns the entire segment "") |
| r.start / r.end / r.sub_ranges / r.is_all | Properties | Read | Field (-1 = Not set) |
| ua.read_range(node_id, range_, attribute=Value) | Extension Methods | Read | Read the excerpt → OpcUaDataValue |
| ua.write_range(node_id, range_, value, attribute=Value) | Extension Methods | Write | Write a sub-section → StatusCode |
Code Example
from darra_opcua import DarraOpcUa, NumericRange, OpcUaVariant, StatusCode
with DarraOpcUa("opc.tcp://localhost:4840") as ua:
ua.connect()
# 1) Construction + Serialization (purely local, not transmitted RPC)
print(str(NumericRange.single(3))) # "3"
print(str(NumericRange.span(3, 7))) # "3:7"
r2d = NumericRange.multi(NumericRange.span(0, 9), NumericRange.span(0, 9))
print(str(r2d)) # "0:9,0:9"
# 2) Analysis
r = NumericRange.parse("3:7")
print(r.start, r.end, r.is_all) # 3 7 False
if NumericRange.try_parse("garbage") is None:
print("解析失败")
# 3) Block-level read/write — Directly forwards read/write is_all operations; now available
with ua.read_range("ns=2;s=Arr", NumericRange.all()) as dv:
print(dv.value) # Converted Python values
with OpcUaVariant().set_double(42.5) as v:
rc = ua.write_range("ns=2;s=Arr", NumericRange.all(), v)
if rc != StatusCode.Good:
print(f"写失败: {rc.name}")
# 4) Subsection read/write — Currently unavailable; throws an exception when a subsection is present NotImplementedError
try:
with ua.read_range("ns=2;s=Arr", NumericRange.single(3)) as dv:
print(dv.value)
except NotImplementedError as ex:
print(f"子段读暂不可用: {ex}")
Implementation Constraints
- Sub-segment read/write is currently unavailable:
read_range/write_rangeOnly follow the standardrange_.is_allwhenread/write; As long as there's a tape segment, discard itNotImplementedError. range_The parameter must be an instanceNumericRange; passing any other type will result in a throwTypeError, and passing an instance will result in a throwNoneValueError.write_rangeItvaluemust beOpcUaVariant, passed andNonethrownValueError.- Python's
OpcUaVariantNo array setter,is_arrayHengFalse— So even if the subsegment is parsed correctly, array elements must be determined byAttributeId.ArrayDimensionsprobing the nodes, not byOpcUaVariantchecking the conditions.
Error Handling
| Phenomenon | Meaning |
|---|---|
Returns StatusCode.Good | Writing Success (Entire Interval) |
Throw NotImplementedError | Reading and writing to segments is not currently supported. |
Sell TypeError / ValueError | range_ Or the parameter value type is incorrect |
Returns Other StatusCode | Server rejection; check the rc.name specific error code |
StatusCode The enumeration only includes commonly used codes; other codes returned by the server will be converted to StatusCode.Bad, so do not use rc.name as an exact error code.
Best Practices
- Construct a range-based expression only when a sub-segment is actually needed
NumericRange; use a comma to select an entire segmentNumericRange.all(), which follows the standard read/write procedure—it runs now. - Splitting a matrix by rows or columns:
NumericRange.multi(NumericRange.span(i, i), NumericRange.span(0, cols - 1)) - Don't use
span/parsesinglefactory every time - When passing a string IndexRange to the server
str(r), it's easy to end up with an invalid format if you construct it yourself.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| NumericRange | NumericRange | NumericRange | NumericRange | NumericRange | DarraUa_NumericRange |
| NumericRange.All (static property) | NumericRange.all() | — | — | — | — |
| ToString() | str(r) | — | — | — | — |
| NumericRangeExtensions.ReadRange | read_range | readRange | ReadRange | read_range | DarraUa_Session_ReadRange |
| NumericRangeExtensions.WriteRange | write_range | writeRange | WriteRange | write_range | DarraUa_Session_WriteRange |
In C#, ReadRange / WriteRange is NumericRangeExtensions theExtension Methods, Not an instance method DarraOpcUa of — usage remains the same ua.ReadRange(...). The behavior is consistent on both sides: non-integer parts throw NotImplementedException / NotImplementedError.
Java / C++ / Rust / C: The four columns marked "read-only, unverified" in this entry — indicate that the information has not been verified; it does not mean that it does not exist.