如何解决使用C#从TwinCAT功能块读取属性
我们正在使用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方法。
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'
关于如何读取/写入定义为功能块属性的变量的任何建议?
解决方法
定义新属性时,会自动为该属性创建一个 get 和一个 set 。
通常使用属性读取或写入功能块VAR部分中的变量。
VAR部分中的所有变量都是私有,因此需要 properties 才能从功能块外部访问这些VAR。
与方法不同,理论上的属性不应进行任何复杂的计算或运行任何逻辑。
我想说的是,您不需要也不应通过ADS调用属性。 无论如何,您都可以通过ADS访问所有私有VAR,因此无需首先通过ADS调用属性。
@编辑
我仍然认为属性不应包含任何逻辑,因此无需通过ADS对其进行调用。
尽管如此,总会有例外。
请注意,根据Beckhoff documentation,仅简单的数据类型和指针将起作用,而不是结构。 此外,“紧凑型运行时系统中无法进行功能监视” 。
在尝试使用{attribute'monitoring':='call'}属性
之后,这是我的工作示例在Twincat中:
{attribute 'monitoring' := 'call'}
PROPERTY RemoteCall : INT
GET:
RemoteCall := buffer;
SET:
buffer := buffer + RemoteCall;
在C#中
class Program
{
static TcAdsClient tcClient;
static void Main(string[] args)
{
tcClient = new TcAdsClient();
tcClient.Connect(851);
AdsStream dataStream = new AdsStream(2);
int iHandle = tcClient.CreateVariableHandle("MAIN.fbTest.RemoteCall");
tcClient.Read(iHandle,dataStream);
Console.WriteLine("Remote Var before property call: " + BitConverter.ToInt16(dataStream.ToArray(),0));
tcClient.WriteAny(iHandle,Convert.ToInt16(2));
tcClient.Read(iHandle,dataStream);
Console.WriteLine("Remote Var after property call: " + BitConverter.ToInt16(dataStream.ToArray(),0));
Console.WriteLine();
Console.ReadLine();
}
}
,
根据斯特凡·亨内肯(Stefan Hennecken)在其Blog上的说法,必须为该物业装饰杂物才能启用此功能:
{attribute ‘monitoring’ := ‘call’}
PROPERTY PUBLIC nProp : BYTE
然后可以使用以下代码示例对其进行读写:
using (AdsClient client = new AdsClient())
{
byte valuePlc;
client.Connect(AmsNetId.Local,851);
valuePlc = (byte)client.ReadValue(“MAIN.fbFoo.nProp”,typeof(byte));
client.WriteValue(“MAIN.fbFoo.nProp”,++valuePlc);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。