C# OPC UA:如何打开服务器文件以在OPC UA中写入原始数据?
我正在创建一个C#应用程序,使用OPC UA将文件发送到西门子数控系统 我正在使用C# OPC UA:如何打开服务器文件以在OPC UA中写入原始数据?,c#,opc-ua,C#,Opc Ua,我正在创建一个C#应用程序,使用OPC UA将文件发送到西门子数控系统 我正在使用CopyFileToServer方法。文件已创建,但我已经看到,要在文件中传递原始数据,必须使用filetype中包含的Open方法,传递原始数据并调用Close方法来关闭文件。 我多次尝试使用Open方法,但都没有成功。 有人能帮我吗 我用Visual Studio 2017在Windows 10 64位计算机上试用了它 public void SendFile(Opc.Ua.Client.Session ses
CopyFileToServer
方法。文件已创建,但我已经看到,要在文件中传递原始数据,必须使用filetype中包含的Open
方法,传递原始数据并调用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 open = new NodeId("ns=2;s=Sinumerik/FileSystem/Part Program/SENDFILE.MPF.Open");
var parent = GetMethodParent(session, open);
object[] argument1 = new object[1];
argument1[0] = Convert.ToByte(OpenFileMode.Write);
var hndl = session.Call(parent, open, argument1);
这样,我就有了与write函数一起使用的句柄。
在NodeId中,“Open”表示要打开的文件
函数“GetMethodParent”:
找到了解决办法
NodeId open = new NodeId("ns=2;s=Sinumerik/FileSystem/Part Program/SENDFILE.MPF.Open");
var parent = GetMethodParent(session, open);
object[] argument1 = new object[1];
argument1[0] = Convert.ToByte(OpenFileMode.Write);
var hndl = session.Call(parent, open, argument1);
这样,我就有了与write函数一起使用的句柄。
在NodeId中,“Open”表示要打开的文件
函数“GetMethodParent”:
重要的一点是,在创建一个新的NodeId之后使用Browse方法。Browse方法允许使用刚刚创建的新节点ID重新加载服务器的树
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;
}
}
}
private referencedescription集合浏览(会话会话,浏览描述节点浏览,bool throwOnError)
{
尝试
{
var descriptionCollection=new ReferenceDescriptionCollection();
var nodesToBrowse=新浏览描述集合{nodeToBrowse};
浏览结果收集结果;
诊断信息收集诊断信息;
session.Browse(null、null、0U、nodesToBrowse、out results、out diagnosticInfos);
ValidateResponse(结果、节点浏览);
ValidateDiagnosticInfo(诊断信息、节点浏览);
而(!StatusCode.IsBad(结果[0].StatusCode))
{
对于(var index=0;index
重要的一点是,创建新节点ID后使用浏览方法。Browse方法允许使用刚刚创建的新节点ID重新加载服务器的树
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;
}
}
}
private referencedescription集合浏览(会话会话,浏览描述节点浏览,bool throwOnError)
{
尝试
{
var descriptionCollection=new ReferenceDescriptionCollection();
var nodesToBrowse=新浏览描述集合{nodeToBrowse};
浏览结果收集结果;
诊断信息收集诊断信息;
session.Browse(null、null、0U、nodesToBrowse、out results、out diagnosticInfos);
ValidateResponse(结果、节点浏览);
ValidateDiagnosticInfo(诊断信息、节点浏览);
而(!StatusCode.IsBad(结果[0].StatusCode))
{
对于(var index=0;index
能否在您的帖子中添加您正在使用的OPC UA库、框架或sdk?.Net framework 4.7.1,DLL OPC UA(OPC UA Foundation)1.3.352.0版?能否在帖子中添加您正在使用的OPC UA库、框架或sdk?.Net framework 4.7.1,DLL OPC UA(OPC UA Foundation)1.3.352.0版