【发布时间】:2020-09-25 08:19:04
【问题描述】:
我们正在使用 C# 应用程序通过 TwinCAT ADS v.3 从 Beckhoff PLC 读取变量。如果我们尝试使用相同的代码来读取属性,代码会失败并出现异常。
FUNCTION_BLOCK FB_Sample
VAR
SomeVariable : INT;
END_VAR
PROPERTY SomeProp : INT // declared in a separate file
// Code used to read variable (symbol)
var handle = client.CreateVariableHandle("sampleProgram.Source.SomeVariable");
var result = client.ReadAny(handle, typeof(int));
client.DeleteVariableHandle(handle);
// Adapted code used to read property (not a symbol)
var handle = client.CreateVariableHandle("sampleProgram.Source.SomeProp"); // This fails
var result = client.ReadAny(handle, typeof(int));
client.DeleteVariableHandle(handle);
当尝试使用上述代码创建一个变量句柄时,我们会收到TwinCAT.Ads.AdsErrorException: 'Ads-Error 0x710 : Symbol could not be found.'。
既然我们知道METHOD 必须用{attribute 'TcRpcEnable'} 标记,所以它可以用这个代码调用:
client.InvokeRpcMethod("{symbolPath}", "{methodName}", {parameters} });
我们也尝试在属性上使用该属性{attribute 'TcRpcEnable'}。使用TcAdsClient.CreateSymbolLoader 并遍历所有可用符号,我们发现该属性的 getter/setter 被标记为 rpc-methods。
Console.WriteLine($"Name: {rpcMethod.Name}; Parameters.Count: {rpcMethod.Parameters.Count}; ReturnType: {rpcMethod.ReturnType};");
RpcMethods: 2
Name: __setSomeProp; Parameters.Count: 1; ReturnType: ;
Name: __getSomeProp; Parameters.Count: 0; ReturnType: INT;
但是尽管尝试,我们还是无法调用 rpc 方法:
var propertyResult = client.InvokeRpcMethod("sampleProgram.Source", "__getSomeProp", Array.Empty<object>());
// Throws: TwinCAT.Ads.AdsErrorException: 'Ads-Error 0x710 : Symbol could not be found.'
var propertyResult = client.InvokeRpcMethod("sampleProgram.Source", "__get{SomeProp}", Array.Empty<object>());
// Throws: TwinCAT.Ads.RpcMethodNotSupportedException: 'The RPC method '__get{SomeProp}' is not supported on symbol 'sampleProgram.Source!'
var propertyResult = client.InvokeRpcMethod("sampleProgram.Source", "get{SomeProp}", Array.Empty<object>());
// Throws: TwinCAT.Ads.RpcMethodNotSupportedException: 'The RPC method 'get{SomeProp}' is not supported on symbol 'sampleProgram.Source!'
var propertyResult = client.InvokeRpcMethod("sampleProgram.Source.SomeProp", "get", Array.Empty<object>());
// Throws: System.ArgumentNullException: 'Value cannot be null.
// Parameter name: symbol'
关于我们如何读/写定义为功能块属性的变量有什么建议吗?
【问题讨论】:
标签: c# ads twincat twincat-ads-.net