在Milo中写入OPC-UA节点时如何设置正确的数据类型?

问题描述 投票:0回答:1

我是使用Milo堆栈将非OPC-UA系统集成到OPC-UA服务器的OPC-UA新手。其中的一部分包括将值写入OPC-UA服务器中的节点。我的问题之一是来自另一个系统的值以Java String的形式出现,因此需要转换为Node的适当数据类型。我的第一个暴力概念证明使用以下代码创建了一个Variant,可用于写入Node(如WriteExample.java中一样)。变量value是Java字符串,其中包含要写入的数据,例如“ 123”代表整数,“ 32.3”代表双精度。现在,该解决方案包括对Identifiers类(org.eclipse.milo.opcua.stack.core,请参阅switch语句)中的“类型”进行硬编码,这不是很漂亮,并且我确定有更好的方法吗?另外,如果要转换并将“ 123”写入节点(例如,UInt64?

       try {
            VariableNode node = client.getAddressSpace().createVariableNode(nodeId);
            Object val = new Object();
            Object identifier = node.getDataType().get().getIdentifier();
            UInteger id = UInteger.valueOf(0);

            if(identifier instanceof UInteger) {
                id = (UInteger) identifier;
            }

            System.out.println("getIdentifier: " + node.getDataType().get().getIdentifier());
            switch (id.intValue()) {
                // Based on the Identifiers class in org.eclipse.milo.opcua.stack.core; 
                case 11: // Double
                    val = Double.valueOf(value);
                    break;
                case 6: //Int32
                    val = Integer.valueOf(value);
                    break;
            }

            DataValue data = new DataValue(new Variant(val),StatusCode.GOOD, null);
            StatusCode status = client.writeValue(nodeId, data).get();
            System.out.println("Wrote DataValue: " + data + " status: " + status);
            returnString = status.toString();
        } catch (Exception e) {
            System.out.println("ERROR: " + e.toString());
        }

我看过Kevin对这个线程的回应:How do I reliably write to a OPC UA server?但是我还是有点迷茫……一些小的代码示例确实会有所帮助。

opc-ua milo
1个回答
0
投票

距离您不远。每个相当大的代码库最终都有一个或多个“ TypeUtilities”类,您将在这里需要一个。

没有这个事实,您需要能够将系统中的类型映射到OPC UA类型,反之亦然。

对于无符号类型,您将使用UShort包中的UIntegerULongorg.eclipse.milo.opcua.stack.core.types.builtin.unsigned类。有一些方便的静态工厂方法,使它们的使用不再那么冗长:

UShort us = ushort(foo);
UInteger ui = uint(foo);
ULong ul = ulong(foo);

我将探讨在即将发布的版本中包含某种类型转换实用程序的想法,但是即使如此,OPC UA的工作方式也必须知道要写入节点的DataType,在大多数情况下,我也想知道ValueRank和ArrayDimensions。

您既可以先验地知道这些属性值,也可以通过其他带外机制获得它们,或者从服务器读取它们。

© www.soinside.com 2019 - 2024. All rights reserved.