how can MATLAB handle .NET methods that have a REF parameter?

7 vues (au cours des 30 derniers jours)
Cristian Berceanu
Cristian Berceanu le 26 Mar 2019
Modifié(e) : Guillaume le 27 Mar 2019
I have a .NET dll which provides the following capability:
TMCTL cTmctl = new TMCTL();
int ret = 0;
ret = cTmctl.Initialize(TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", ref id );
I need to use this dll file in Matlab, and so far I can succesfully reference the dll and create the necessary object:
YK_TMCTL = NET.addAssembly('C:\Windows\SysWOW64\TmctlAPINet64.dll');
Oscilloscope = TmctlAPINet.TMCTL;
However, I have difficulties in callig the Initialize function, this code always retunrs 1, wihch means the function could not establish a connection:
int32DeviceID = int32(0);
int32InstrumentAnswer = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER,'10.60.142.50,anonymous',int32DeviceID);
Oscilloscope.TM_CTL_ETHER is just a constant, it's not giving me any trouble. But I believe the int32DeviceID is the problem and I do not know how to succesfully pass this 'reference' variable.
Note that I have looked at this answer and I have subsequently tried:
[int32InstrumentAnswer,int32DeviceID] = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER,'10.60.142.50,anonymous')
Unfortunatelly this fails with the following error: No method 'Initialize' with matching signature found for class 'TmctlAPINet.TMCTL'.
Hence, the question in the title.
Regards,
Cristian
  1 commentaire
Guillaume
Guillaume le 26 Mar 2019
Modifié(e) : Guillaume le 26 Mar 2019
what does
methodsview(Oscilloscope)
%or
methods(Oscilloscope, '-full')
says the full signature of Initialize is (there may be several, in which case, show them all).
Oh, and what is the actual documented .Net signature?

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 26 Mar 2019
Modifié(e) : Guillaume le 26 Mar 2019
If we go by your .Net code, the correct matlab version should be:
id = int32(0); %you probably don't even need the int32. matlab should do the conversion automatically
[ret, id] = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER, '11.22.33.44,anonymous,', id);
ref parameters are both inputs and outputs in matlab. See Call .Net methods With ref Keyword
If that doesn't work then answer my comment to your question.
  3 commentaires
Guillaume
Guillaume le 27 Mar 2019
Cristian's comment mistakenly posted as an answer moved here:
I found the problem, eventually. The correct formulation of the instrumction is as you suggested:
[int32InstrumentAnswer,int32DeviceID] = Oscilloscope.Initialize(Oscilloscope.TM_CTL_ETHER,"10.60.142.50,anonymous,",int32DeviceID)
However, the key is the "," character at the end of the second argument, which I did not have before. Adding this small chang makes it work, I noticed this when actually trying to make it work in C# (I never got around to finishing that, as when I noticed this difference I went back to Matlab and it worked!)
The string argument consists of an IP address, username and pasword, and they must be separated by commas. According to the documentation, , if the username is "anonymous" then there is no further need for a password, however, it seems the comma still needs to be there.
Best regards,
Cristian
Guillaume
Guillaume le 27 Mar 2019
Modifié(e) : Guillaume le 27 Mar 2019
In C# and a lot of other languages, the compiler automatically adds an extra input to class member functions, usually as the first input. This argument is always the class instance on which the function is called. So, for example, the method:
//.Net code
//class definition
public class TMCTL{
public int Initialize(int wire, string comm, ref int id){
//... implementation
}
}
is automatically converted, at compile-time to
public int Initialise(TMCTL this, int wire, string comm, ref int id)
because the function actually needs to know which object it operates on. This extra argument is all invisible to you, and the compiler also adds it in function calls:
ret = cTmctl.Initialize(TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", ref id );
is compiled as
ret = Initialize(cTmctl, TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", ref id);
In matlab however, the developpers decided to leave that job to you. So whenever you implement a class method in matlab, you must make sure that the object is an explicit input argument.
%matlab code
classdef TMCTL
methods (Access = public)
function [result, id] = Initialise(this, wire, comm, id)
%...
end
end
end
Matlab still does the job of transforming function calls, but you can also do it yourself
%both of these work in matlab and are more or less equivalent (there's a slight difference of scope that is not relevant)
[ret, id] = cTmctl.Initialize(TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", id);
[ret, id] = Initialize(cTmctl, TMCTL.TM_CTL_ETHER, "11.22.33.44,anonymous,", id);
if the username is "anonymous" then there is no further need for a password, however, it seems the comma still needs to be there.
Yes, that makes sense. The code probably split the string at commas and expect 3 strings after the split.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by