c# - OPC UA:如何打开服务器文件以在 OPC UA 中写入原始数据?
问题描述
我正在创建一个使用 OPC UA 将文件发送到 Siemens 数控系统的 C# 应用程序。
我正在使用该CopyFileToServer
方法。文件已创建,但我看到要传递文件中的原始数据,您必须使用Open
filetype 中包含的方法,传递原始数据并调用该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)
{
}
}
上面的代码返回以下异常:
“错误的无效论点”。
解决方案
重要的是,在创建新的 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;
}
}
}
推荐阅读
- java - 使用带有同步块的 ConcurrentHashMap 的 Java 并发
- pine-script - 如何仅在条形图顶部显示字符串“超买”一次
- c++ - 如何完全删除多维动态数组?
- python-3.x - 根据条件组合熊猫文本行
- windows - 将每一行文本转换为变量的批处理代码
- jupyter-notebook - 带有 Bokeh show() 命令的单元格永远运行 - 如何让它们在绘图后停止?
- google-data-studio - 如何在 Google Data Studio 中制作社区可视化以供用户在使用前购买
- python - 在交互式会话中找不到对象方法
- android - 构建建议时颤振 showSearch 异步操作
- python - 按组对数据透视表进行排序