Using .NET function with input argument 'Type'
6 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I am trying to read some data by using TwinCat ads interface. The syntax should be:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1358818/image.png)
My code looks like:
% dll
asm = NET.addAssembly('C:\TwinCAT\AdsApi\.NET\v4.0.30319\TwinCAT.Ads.dll');
% import the dll
import TwinCAT.Ads.*;
% open a asm-Client
adsClt = TwinCAT.Ads.TcAdsClient;
% connect client to specified target-ID and port
adsClt.Connect('adsID',port);
SymbolInfo = adsClt.ReadSymbolInfo('GVL.data');
testVariable = adsClt.ReadAny(SymbolInfo.IndexGroup,SymbolInfo.IndexOffset,Single);
I have problems with the ReadAny command, as the input argument "Type" is not used in the right manner. Are there any hints how this is used correctly?
Thanks in advance.
0 件のコメント
回答 (2 件)
Vijeta
2023 年 5 月 11 日
Hi Simon,
The ReadAny method of the TcAdsClient class in the TwinCAT .NET ADS API allows you to read data from an ADS server using an arbitrary data type. The Type parameter specifies the data type of the variable being read.
In your code, you are trying to read a variable named 'GVL.data'. Before calling the ReadAny method, you are calling the ReadSymbolInfo method to get information about this variable, including its index group and index offset.
To use the ReadAny method correctly, you need to pass the correct Type parameter to it. The Type parameter specifies the type of data that you want to read. In your case, you are trying to read a single precision floating point number (i.e., a Single in .NET). Therefore, you should pass the Type parameter as follows:
vbnetCopy code
testVariable = adsClt.ReadAny(SymbolInfo.IndexGroup, SymbolInfo.IndexOffset, typeof(Single));
The typeof(Single) statement returns the .NET Type object that corresponds to the Single type, which is a single precision floating point number. This tells the ReadAny method to read a single precision floating point number from the specified index group and index offset.
If you wanted to read a different data type, you would pass a different Type parameter. For example, to read an integer, you would use typeof(Int32).
0 件のコメント
Torsten
2024 年 2 月 29 日
Hi is there any further information to this problem available?
I am still fighting with the typeof paramter.
Here is my code:
AdsAssembly = NET.addAssembly("C:\TwinCAT\AdsApi\.NET\v4.0.30319\TwinCAT.Ads.dll");
import TwinCAT.Ads.*;
tcClient = TwinCAT.Ads.TcAdsClient;
tcClient.Connect(myAmsNetID,851);
% Write Array of REAL to TwinCat works fine!
value6 = [13, 14];
var6 = tcClient.ReadSymbolInfo('MAIN.Aout_to_TwinCat');
tcClient.WriteAny(var6.IndexGroup, var6.IndexOffset, single(value6));
% Here the problem starts using ReadAny to read the Array from TwinCat
var5 = tcClient.ReadSymbolInfo('MAIN.Aout_to_Matlab');
value5 = tcClient.ReadAny(var5.IndexGroup, var5.IndexOffset, typeof(single);
I get the error:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
The problem still is the "typeof" paramter in the last line of code.
Please help me!
Thanks
TV
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!