NumericRange
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" | No. 5 |
| "3:7" | 3–7 |
| "1:3,2:4" | Two-dimensional |
API
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| NumericRange::all() | Factory | — | The entire interval |
| NumericRange::single(i32) | Factory | — | Single Index |
| NumericRange::span(start, end) | Factory | — | Range |
| NumericRange::multi(Vec<NumericRange>) | Factory | — | Multidimensional |
| NumericRange::parse(s) | Static | — | Parsing, Returns on Failure Err(String) |
| NumericRange::try_parse(s) | Static | — | Parsing, Returns on Failure None |
| range.to_string() | — | — | Serialization (impl Display) |
| range.start / end / sub_ranges | Field | Read | Public Fields, Direct Access |
| range.is_all() | Methods | Read | Is this the entire interval? |
| ua.read_range(nid, &range, attr) | Extensions | Read | Read the passage |
| ua.write_range(nid, &range, &value, attr) | Extensions | Write | Writing Subintervals |
Code Example
use darra_opcua::{Session, NumericRange, Variant, AttributeId};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ua = Session::new("opc.tcp://localhost:4840")?;
ua.connect()?;
// 1) A single element
let dv = ua.read_range("ns=2;s=Arr", &NumericRange::single(3), AttributeId::Value)?;
println!("Arr[3] = {:?}", dv.variant().as_f64());
// 2) Range — Returns an array of values; use to_display_string() to view
let dv2 = ua.read_range("ns=2;s=Arr", &NumericRange::span(0, 99), AttributeId::Value)?;
println!("{}", dv2.variant().to_display_string());
// 3) Two-dimensional sub-segments
let r2d = NumericRange::multi(vec![
NumericRange::span(0, 9), NumericRange::span(0, 9),
]);
let dv3 = ua.read_range("ns=2;s=Matrix", &r2d, AttributeId::Value)?;
println!("{}", dv3.variant().to_display_string());
// 4) Write a single item — 4 parameters: node_id / range / value / attr
let mut v = Variant::new();
v.set_f64(42.5);
let rc = ua.write_range("ns=2;s=Arr", &NumericRange::single(0), &v, AttributeId::Value)?;
println!("write = {}", rc);
// 5) Analysis
let r = NumericRange::parse("3:7")?;
println!("{} (start={}, end={})", r, r.start, r.end);
// 6) The entire paragraph
let dv = ua.read_range("ns=2;s=Arr", &NumericRange::all(), AttributeId::Value)?;
Ok(())
}
Implementation Notes
- Stack is currently not exported
*WithRangePInvoke:range.is_all()whenread_range/write_rangedirect forwardingread_attr/write_attr; Non-full interval Returns at that timeErr(OpcUaError)andstatus == StatusCode::BAD_NOT_SUPPORTED VariantProvides only scalar getters (as_bool/as_i32/as_u32/as_i64/as_f64/as_string/as_filetime/as_datetime), No array getter; view the retrieved array values usingto_display_string()
Error Handling
The following table lists the protocol OPC UA status codes:Text Name (Part 4, Appendix A).BadIndexRangeInvalid / BadIndexRangeNoData If it is not in the set of SDK constants StatusCode, use the general method (is_bad() / raw() / status_name()) to determine it; do not write it as a constant StatusCode::XXX:
| Status (Protocol Code) | Meaning |
|---|---|
| Good | Success |
| BadIndexRangeInvalid | Invalid string range / Node is not an array |
| BadIndexRangeNoData | "range" is outside the data range |
| BadTypeMismatch | Write type mismatch (SDK constant StatusCode::BAD_TYPE_MISMATCH) |
match ua.write_range("ns=2;s=Arr", &NumericRange::single(0), &v, AttributeId::Value) {
Ok(st) if st.is_good() => { /* Success */ }
Ok(st) => eprintln!("写入被拒 0x{:08X} {}", st.raw(), darra_opcua::status_name(st)),
Err(e) => eprintln!("调用失败 0x{:08X} {} {}", e.status.raw(), darra_opcua::status_name(e.status), e.message),
}
Best Practices
- Extracting a Subset from a Large Array
- Splitting a Matrix by Rows / Columns
- Write only to the modified indexes
- Don't parse it every time
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 |