我们正在使用 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'
关于我们如何读/写定义为功能 block 属性的变量有什么建议吗?
最佳答案
当您定义一个新属性时,您会自动为该属性创建一个get 和一个set。
您通常使用属性来读取或写入功能 block 的 VAR 部分中的变量。
VAR 部分中的所有变量都是私有(private),因此需要属性从功能 block 外部访问这些 VAR。
与方法不同,理论上的属性不应执行任何复杂的计算或运行任何逻辑。
我想说的是,您不需要也不应该通过 ADS 调用属性。 无论如何,您都可以通过 ADS 访问所有私有(private) VAR,因此无需首先通过 ADS 调用属性。
@编辑
我仍然认为属性不应包含任何逻辑,因此无需通过 ADS 调用它们。
但总有异常(exception)。
请注意,根据 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();
}
}
关于c# - 使用 C# 从 TwinCAT 功能 block 中读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64060431/