首页 > 解决方案 > OPC UA:如何打开服务器文件以在 OPC UA 中写入原始数据?

问题描述

我正在创建一个使用 OPC UA 将文件发送到 Siemens 数控系统的 C# 应用程序。

我正在使用该CopyFileToServer方法。文件已创建,但我看到要传递文件中的原始数据,您必须使用Openfiletype 中包含的方法,传递原始数据并调用该Close方法关闭文件。我尝试了几次使用该Open方法而没有成功。有人能帮我吗?

我在装有 Visual Studio 2017 的 Windows 10 64 位计算机上进行了尝试。

public void SendFile(Opc.Ua.Client.Session session)
    {
        try
        {
            if (session != null)
            {

                NodeId node0 = new NodeId("ns=2;s=/Methods");
                NodeId node1 = new NodeId("ns=2;s=/Methods/GiveUserAccess");
                object[] argument0 = new object[2];

                argument0[0] = "USER";
                argument0[1] = "SinuWriteAll";

                session.Call(node0, node1, argument0);

                NodeId node = new NodeId("ns=2;s=/Methods");
                NodeId method = new NodeId("ns=2;s=/Methods/CopyFileToServer");
                object[] argument = new object[3];
                byte[] data = new byte[1];

                argument[0] = "Sinumerik/FileSystem/Part Program/sendFile.mpf";
                argument[1] = data;
                argument[2] = true;

                var a = session.Call(node, method, argument);

                NodeId nodeFile = new NodeId("file to open"); // The problem is this (i don't find the method for the file server nodeid)
                NodeId methodOpen = new NodeId("ns=0;i=11580");
                object[] argument1 = new object[1];
                argument1[0] = OpenFileMode.Write;

                var hndl = session.Call(nodeFile, methodOpen, argument1); // Exception 
            }
        }
        catch (Exception ex)
        {

        }
    }

上面的代码返回以下异常:

“错误的无效论点”。

标签: c#opc-ua

解决方案


重要的是,在创建新的 NodeId 后使用 Browse 方法。Browse 方法,允许使用刚刚创建的新 NodeID 重新加载服务器的树。

     private ReferenceDescriptionCollection Browse(Session session, BrowseDescription nodeToBrowse, bool throwOnError)
    {
        try
        {
            var descriptionCollection = new ReferenceDescriptionCollection();
            var nodesToBrowse = new BrowseDescriptionCollection { nodeToBrowse };
            BrowseResultCollection results;
            DiagnosticInfoCollection diagnosticInfos;
            session.Browse(null, null, 0U, nodesToBrowse, out results, out diagnosticInfos);
            ClientBase.ValidateResponse(results, nodesToBrowse);
            ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse);
            while (!StatusCode.IsBad(results[0].StatusCode))
            {
                for (var index = 0; index < results[0].References.Count; ++index)
                    descriptionCollection.Add(results[0].References[index]);
                if (results[0].References.Count == 0 || results[0].ContinuationPoint == null)
                    return descriptionCollection;
                var continuationPoints = new ByteStringCollection();
                continuationPoints.Add(results[0].ContinuationPoint);
                session.BrowseNext(null, false, continuationPoints, out results, out diagnosticInfos);
                ClientBase.ValidateResponse(results, continuationPoints);
                ClientBase.ValidateDiagnosticInfos(diagnosticInfos, continuationPoints);
            }
            throw new ServiceResultException(results[0].StatusCode);
        }
        catch (Exception ex)
        {
            if (throwOnError)
                throw new ServiceResultException(ex, 2147549184U);
            return null;
        }
    }
}

推荐阅读