Clear Filters
Clear Filters

Using .NET function with input argument 'Type'

4 views (last 30 days)
Simon
Simon on 17 Apr 2023
Commented: Simon on 18 Apr 2024
Hi,
I am trying to read some data by using TwinCat ads interface. The syntax should be:
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.

Answers (2)

Vijeta
Vijeta on 11 May 2023
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).

Torsten
Torsten on 29 Feb 2024
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
  1 Comment
Simon
Simon on 18 Apr 2024
Hi Torsten,
may you try the simple one:
var5 = tcClient.ReadSymbolInfo('MAIN.Aout_to_Matlab');
value5 = tcClient.ReadSymbol(var5);

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!