Matlab connection with device by .net library by .dll

Hello,
I just bought a hardware that is USB connected to the computer, with a software to control it.
The hardware is a captor, which has an acquisition frequency between 1-100hz. My problem is that from the software that they provide you can choose between 1 or 100 and some of my experiences need acquisition frequencies with values between them.
When I contacted the provider, they told me that if I need another frequency I will need to work around myself. The captor has a .dll (a .NET library) file that was created from the installation of the software, and from its application note there is a MODBUS communication protocol too.
From the application note, the library is a .NET assembly and any .net supporting programming language or program like C#, LabVIEW, IronPython, Visual Basic .net, or Mathworks MATLAB should be able to use it and fully control the detector. From what this phase and as I have no knowledge of the other languages/programs and I have used Matlab and I have a license to use it I thought I would follow that path.
The problem is that I haven’t work with a library before, and the application note only gives more information for C# and IronPython.
From what I read in the matlab forum, matlab needs a header file too (besides the library file .dll) where I don’t have anything else from the provider.
Is it that I am missing something or is there another way to work around?
If any essential information is missing, I excuse myself as it is not at all my field of work and I am doing this to be able to use a captor correctly.
Thanks in advance,

Respuestas (1)

Guillaume
Guillaume el 30 de Abr. de 2019
It's actually fairly easy to call .Net functions from matlab. No you don't need a header file for Net assemblies (these don't exist in the .Net world), header files are for non .Net dlls (that use Windows API instead of .Net).
Instead for .Net you make the assembly visible to matlab with NET.addAssembly, e.g
net.addAssembly('C:\somewhere\somefolder\yourdll.dll'); %assuming the assembly has not been added to the GAC, otherwise use the GAC name
Then you use more or less the same code as you'd use in VB.Net, C#, or Python to interact with the assembly. Just instantiate a class and call its methods and properties. If you show an example in any language of the code you want to use we can probably show you the matlab equivalent.

6 comentarios

THAAAAANKS, I was looking at net.addassembly actually.
the thing was that i didnt have any tokens for the license till half an hour ago so I just succed to open matlab.
here is everything I have in the docummentation for C#
All the code is in C#
Creationg object and connecting
Using clDigital;
Digital Detector = new Digital();
Detector.Port 15; //port name COM15
Detector.Open(); //opening the serial port
Detector.connect(); // connecting to the detector
If (Detector.IsConnected) // do something
Starting and Stopping a measurement
//startong a measurement
Detector.StartMethod();
//Optional waiting until the measurement done
While (Detector.IsMethodRunning)
{
// do something or just wait
}
//when the measurement donne it will be stopped automatically
//forcing the measurement to stop
Detector.StopMethod();
Reading measurement values
//getting the values
//measurement value in mV recorded while the excitation on
double OnValue;
//measurement value in mV recorded while the excitation off (background)
double OffValue;
//channel 1 E1D1 (OnValue1 and OfValue1)
OnValue = Detector.OnValue1;
OffValue = Detector.OffValue1;
//channel 2 E1D2 (OnValue2 and OfValue2)
OnValue = Detector.OnValue2;
OffValue = Detector.OffValue2;
//channel 3 E2D2 (OnValue3 and OfValue3)
OnValue = Detector.OnValue3;
OffValue = Detector.OffValue3;
Reading, modifying and saving settings
//example of reading and setting a parameter
// setting method cycle to 1
Detector.MethodCycles = 1;
//setting method type to 1
Detector.MethodType = 1;
//reading the detector temperature
double Temp = Detector.BoardTemperature;
//saving the settings permanently in memory
Detector.Save2NVRam();
//reading the detector infos
string SN = Detector.BoardSN;
closing connection and disconnecting
Detector.Disconnect();
Detector.Close();
ufff i just typed everything as I had an scanned copy only.
the thing is as you can see in the codes that i mentioned before is that I can not change the adquisition frequency with them for example ( and normally there would be more functions....)
I can imagine that this functions are defined inside the .dll file like "Detector.BoardTemperature".
when I opened the .dll in matlab it is a assembly of:
an assembly with others assemblies inside
[CodeBase ,FullName, EntryPoint, DefinedTypes, Evidence, PermissionSet, SecurityRuleSet, ManifestModule, ReflectionOnly, Location, imageRunTimeVersion, GlobalAssemblyCache, HostContext, IsDynamic, EscapedCodeBase, ExportedTypes, IsFullyTrusted, CustomAttributes, Modules]
and other matrix.
is there any way to inspect this to be able to "recover" all the functions of the library? even if they dont say what they do? (as the ones in C# where relative simple to understad what they were doing even without the comments....)
thanks a lot for the help, is super important to be able to continue my experiences.
best regards, franco
The
using clDigital;
is
import clDigital.*
in matlab. However, I'd recommend you use fully qualified names instead of import. It might be a bit more typing but it's clearer for the reader where the classes come from.
Then the code in matlab simply:
detector = clDigital.Detector; %or just Detector if used import
detector.Port = 15;
detector.Open;
detector.Connect;
if detector.IsConnected
detector.StartMethod;
while detector.IsMethodRunning
%do something
end
detector.StopMethod;
onvalue = detector.OnValue1;
%... etc.
end
As you can see, it's fairly straightforward.
To see all the public properties and methods of the detector class:
properties('clDigital.Detector'); %or if you have an instance: properties(detector)
methods('clDigital.Detector', '-full')
%or
methodsview('clDigital.Detector')
great! i am beging to understand a little bit more, thanks for everything if it doesnt bother you I have some questions :)
1st
i asume that line 2 detector.Port (and all after that one) works because you called detector to clDigital.Detector; no?
%if it was
example = clDigital.Detector; %or just Detector if used import
example.Port = 15;
example.Open;
example.Connect;
etc.... (am I correct?)
2nd
why clDigital.Detector? the "clDigital" because of the name of the file but I am lost about the ".Detector"
I dont understand from where you get the clDigital.Detector, from the NET.addAssebly('C:\somewhere\somefolder\yourdll.dll') to it.
3rd
the ".Open" as for examble "detector.Open;" is the way to get a property, no?
sorry for the dumb questions
The assembly (the dll) will contain a namespace. Because your C# code has a using clDigital, I assume that the namespace is clDigital. Once you've added the assembly with addAssembly, that namespace is visible to matlab. Within that namespace, you'll have one or more several classes, structures, enums, etc. defined. You can see all of these by inspecting the output of addAssembly if you want.
One of these classes is called Detector, so when you do
example = clDigital.Detector;
You're invoking the class constructor and creating an instance of the class. You can indeed name that instance any way you want. It's a variable like any other.
example.Port = 15;
just alter the Port property of the object.
example.Open;
just call the Open method of the object. You can use () to make it clear that it's a method if you prefer:
example.Open();
As I said, you'll get the list of all properties and methods with the properties and methods (or methodsview) functions of matlab.
okey, now I understand you better
if I do Test=NET.addAssembly('C:\Program Files (x86)\QIAGEN\FLDigital\clDigital.dll');
i have a "Test" assembly with as you said, it has differents properties ( assemblyHandle/Classes/Structures/Enums/GenericTypes/Interfaces/Delegates)
"One of these classes is called Detector, so when you do [...]" non is called Detector neither directly in Test neigther inside classes (Test.Classes), I think it is from that, that I wasnt following you.... i have all the classes different from what it was in C#
for example inside classes (Test.Classes) I have:('clDigital.Digital'/'clDigital.Digital+MethodDataEventArgs'/'clDigital.Digital+MethodDoneEventArgs'/'clDigital.Digital+ScanDataEventArgs'/'clDigital.Digital+ScanDataDoneEventArgs'/'clDigital.Digital+AutozeroDoneEventArgs')
is there anyway to see at least what the function gives me? if it is a double a matrix?
also then i can begin a function (that would be measure it) as i run the code and is connected to the pc, no?
is there anyway to explore more? as from the functions of C# I have they were evidents (what they were doing) but in the names I have no, E.G nothing is called temperature, or temp....
thanks
I probably had a momentary case of blindness. Indeed, the original code was:
Digital Detector = new Digital();
The class name is therefore Digital. So the construction in matlab is
example = clDigital.Digital;
methodsview or methods with the '-full' option will allow you to see all the methods with all their input and output arguments (translated to matlab types):
methodsview('clDigital.Digital'); %displays in a an window
methods('clDigital.Digital', '-full'); %displays at the command prompt
Whatever function would allow you to change the frequency will be a method of that Digital class. The other classes listed in that assembly are just to encapsulate event data. It is also possible that the frequency is a property of the object:
properties('clDigital.Digital'); %displays at the command prompt. There's no pop-up equivalent.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Preguntada:

el 30 de Abr. de 2019

Comentada:

el 2 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by