1

该方法需要的 CustomDataType:

<UADataType NodeId="ns=1;i=3010" BrowseName="1:ScanSettings">
<DisplayName>ScanSettings</DisplayName>
<References>
<Reference ReferenceType="HasEncoding">ns=1;i=5015</Reference>
<Reference ReferenceType="HasEncoding">ns=1;i=5016</Reference>
<Reference ReferenceType="HasSubtype" IsForward="false">i=22</Reference>
</References>
<Definition Name="1:ScanSettings">
<Field DataType="Duration" Name="Duration"/>
<Field DataType="Int32" Name="Cycles"/>
<Field DataType="Boolean" Name="DataAvailable"/>
<Field IsOptional="true" DataType="LocationTypeEnumeration" Name="LocationType"/>
</Definition>
</UADataType>

方法调用:

CallMethodRequest tCallMethodRequest = new CallMethodRequest(tObjectId, tMethodId, new Variant[]{});
CallMethodResult tCallMethodResult = pOpcUaClient.call(tCallMethodRequest).get();
System.out.println(tCallMethodResult.getStatusCode());

如何调用需要 ScanSettings 的方法?我是否需要通过包含 Duration、Cycles 和 DataAvailable 的三个 Variant 来传递 Variant-array?

或者

我需要做这样的事情

编辑:

用 ScanSettings-class 试了一下,得到了错误:

10:08:52.655 [ua-shared-pool-2] WARN org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder - Not a built-in type: class ScanSettings

我的 ScanSettings 类:

public class ScanSettings {

private final double duration;
private final int cycles;
private final boolean dataAvailable;

public ScanSettings() {
    this(1000.0, 1, true);
}

public ScanSettings(double pDuration, int pCycles, boolean pDataAvailable) {
    duration = pDuration;
    cycles = pCycles;
    dataAvailable = pDataAvailable;
}

public double getDuration() {
    return duration;
}

public int getCycles() {
    return cycles;
}

public boolean isDataAvailable() {
    return dataAvailable;
}

@Override
public int hashCode() {
    return Objects.hashCode(duration);
}

@Override
public boolean equals(final Object obj) {
    return super.equals(obj);
}

@Override
public String toString() {
    return duration + " " + cycles + " " + dataAvailable;
}

public static class Codec extends GenericDataTypeCodec<ScanSettings> {

    @Override
    public Class<ScanSettings> getType() {
        return ScanSettings.class;
    }

    @Override
    public ScanSettings decode(final SerializationContext context, final UaDecoder reader) throws UaSerializationException {
        double tDuration = reader.readDouble("Duration");
        int tCycle = reader.readInt32("Cycle");
        boolean tDataAvalible = reader.readBoolean("DataAvailable");

        return new ScanSettings(tDuration, tCycle, tDataAvalible);
    }

    @Override
    public void encode(final SerializationContext context, final ScanSettings pScanSettings, final UaEncoder writer) throws UaSerializationException {
        writer.writeDouble("Duration", pScanSettings.duration);
        writer.writeInt32("Cycle", pScanSettings.cycles);
        writer.writeBoolean("DataAvailable", pScanSettings.dataAvailable);
    }
}

}

注册它:

OpcUaBinaryDataTypeDictionary tOpcUaBinaryDataTypeDictionary = new OpcUaBinaryDataTypeDictionary("urn:ScanSettings");

            NodeId binaryEncodingId = new NodeId(2, "DataType.ScanSettings.BinaryEncoding");

            tOpcUaBinaryDataTypeDictionary.registerStructCodec(new ScanSettings.Codec().asBinaryCodec(), "ScanSettings", binaryEncodingId);

            OpcUaDataTypeManager.getInstance().registerTypeDictionary(tOpcUaBinaryDataTypeDictionary);
4

1 回答 1

0

我认为因为现在没有代码生成支持,最好的办法是利用客户端已经知道如何读取数据类型字典并将它们转换为通用 Struct 对象,以及接收这些 Struct 对象并对其进行编码的事实必要时。

您的方法调用看起来像这样:

Struct scanSettings = Struct.builder("ScanSettings")
    .addMember("LocationTypeSpecified", 0)
    .addMember("Reserved1", 0)
    .addMember("Duration", 3.14)
    .addMember("Cycles", 1)
    .addMember("DataAvailable", false)
    .build();

ExtensionObject xo = ExtensionObject.encodeAsByteString(
    scanSettings,
    new NodeId(1, 3010),
    client.getDataTypeManager()
);

CallMethodRequest request = new CallMethodRequest(
    objectId,
    methodId,
    new Variant[]{new Variant(xo)}
);
于 2018-12-13T13:57:28.870 回答