0

我正在尝试与根据此规范实现 OPC-UA 服务器的 RFID 阅读器进行交互。

我正在尝试调用 ScanStart 方法,该方法将ScanSettings 结构作为输入参数(AutoID 数据类型),但尽管阅读了示例和文档,但我无法找到执行此操作的方法。

使用UAExpert,我可以调用该方法并使用GUI输入结构的值,这会在wireshark中产生以下转储:

    ArraySize: 1
    [0]: Variant
        Variant Type: ExtensionObject (0x16)
        Value: ExtensionObject
            TypeId: ExpandedNodeId
                EncodingMask: 0x01, EncodingMask: Four byte encoded Numeric
                    .... 0001 = EncodingMask: Four byte encoded Numeric (0x1)
                    .0.. .... = has server index: False
                    0... .... = has namespace uri: False
                Namespace Index: 3
                Identifier Numeric: 5015
            EncodingMask: 0x01, has binary body
                .... ...1 = has binary body: True
                .... ..0. = has xml body: False
            ByteString: 0000000000000000000000000000000000

有没有人成功注册一个 ExtensionObject 以使用 node-opcua 传递给方法调用?在这一点上,我很高兴只发送上面的 ByteString 而无需对结构进行编码/解码,因为它始终是静态的。

显然有一个constructExtensionObject 方法。我为此拥有的客户端代码是:

(async () => {

    const client = OPCUAClient.create({ endpoint_must_exist: false});
    client.on("backoff", () => console.log("Backoff: trying to connect to ", endpointUri));

    await client.withSessionAsync(endpointUri, async (session) => {
        let scanSettings = {
            Duration: 0,
            Cyles: 0,
            DataAvailble: false
        };
        const nodeID = new NodeId(NodeIdType.STRING, "rfr310.ScanStart.InputArguments", 4);
        const extObj = session.constructExtensionObject(nodeID, scanSettings);

        const methodsToCall = [
            {
                objectId: "ns=4;s=rfr310",
                methodId: "ns=4;s=rfr310.ScanStart",
                inputArguments: [extObj]
            }
        ];

        extObj.then(() => {
            session.call(methodsToCall,(err,results) => {
                if (err) {
                    console.log(err);
                } else {
                    console.log(results);
                }
            });
        }).catch(() => {
        })
    });
})();

产生错误“当 pendingTransactions 不为空时处理”,它被 extObj.catch() 捕获

我究竟做错了什么?我相当确定这是我的承诺处理问题......

任何帮助表示赞赏!

4

1 回答 1

0

好的,所以我终于到了那里。以下是使用 node-opcua 调用带有结构输入参数的 OPC-UA 方法的方法:

const { OPCUAClient, NodeId, NodeIdType, DataType} = require("node-opcua");

const endpointUri = "opc.tcp://<your-endpoint>:<your-port>";

(async () => {

    const client = OPCUAClient.create({ endpoint_must_exist: false});
    client.on("backoff", () => console.log("Backoff: trying to connect to ", endpointUri));

    await client.withSessionAsync(endpointUri, async (session) => {
        // Scan settings value input
        const scanSettingsParams = {
            duration: 0,
            cycles : 0,
            dataAvailable : false,
            locationType: 0
        };

        try {
            // NodeID for InputArguments struct type (inherits from ScanSettings)
            const nodeID = new NodeId(NodeIdType.NUMERIC, 3010, 3);
            // Create ExtensionObject for InputArguments
            const scanSettingsObj = await session.constructExtensionObject(nodeID, scanSettingsParams);

            // Populate Method call with ExtensionObject as InputArgument
            const methodToCall = {
                    objectId: "ns=4;s=rfr310",
                    methodId: "ns=4;s=rfr310.ScanStart",
                    inputArguments: [
                        {
                            dataType: DataType.ExtensionObject,
                            value: scanSettingsObj
                        }
                    ]
                };

                // Call method, passing ScanSettings as input argument
                session.call(methodToCall,(err,results) => {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log(results);
                    }
                });

        } catch (err) {
            console.log(err);
        }
    });
})();
于 2020-04-29T11:29:56.473 回答