Skip to main content

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:

StringMeaning
""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​

MembersCategoryReading and WritingNote
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_allPropertiesReadField (-1 = Not set)
ua.read_range(node_id, range_, attribute=Value)Extension MethodsReadRead the excerpt → OpcUaDataValue
ua.write_range(node_id, range_, value, attribute=Value)Extension MethodsWriteWrite 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_range Only follow the standard range_.is_all when read / write; As long as there's a tape segment, discard it NotImplementedError.
  • range_ The parameter must be an instance NumericRange; passing any other type will result in a throw TypeError, and passing an instance will result in a throw None ValueError.
  • write_range It value must be OpcUaVariant, passed and None thrown ValueError.
  • Python's OpcUaVariant No array setter, is_array Heng False — So even if the subsegment is parsed correctly, array elements must be determined by AttributeId.ArrayDimensions probing the nodes, not by OpcUaVariant checking the conditions.

Error Handling​

PhenomenonMeaning
Returns StatusCode.GoodWriting Success (Entire Interval)
Throw NotImplementedErrorReading and writing to segments is not currently supported.
Sell TypeError / ValueErrorrange_ Or the parameter value type is incorrect
Returns Other StatusCodeServer 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 segment NumericRange.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 / parse single factory 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#PythonJavaC++RustC
NumericRangeNumericRangeNumericRangeNumericRangeNumericRangeDarraUa_NumericRange
NumericRange.All (static property)NumericRange.all()————
ToString()str(r)————
NumericRangeExtensions.ReadRangeread_rangereadRangeReadRangeread_rangeDarraUa_Session_ReadRange
NumericRangeExtensions.WriteRangewrite_rangewriteRangeWriteRangewrite_rangeDarraUa_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.

Next Step​