From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

.NET and mathscript node help

Hello world,

 

It has been a whilse since I have posted and now I have a new problem altogether.

 

Before I go into the problem I list the specifications of the Labview/matlab versions.

 

Matlab 2012: 32bit

Labview 2012: 32bit

 

I have 32 and 64 bit versions of the .Net library including the associated .dll files.

 

Problem:

 

I have a Novelda chip purchased from the University and got it working in Matlab. The Matlab code talks to the device via .NET where I specify the location of the .dll library path as shown below and then create an instance of it so that I can get data from the device.

 

 

 

Basically, I naiively imported the Matlab code into the Mathscript node and found that Labview doesn't work with try/catch statements. I therefore removed these statements from the code and Labview did not flag this as an error anymore and the code is attatched at the bottom of this query. However, I now have a problem in that the Mathscript node does not recogize .NET within the mathscript node and consequently, flags an error on the code pasted above.

 

Q1) I realise I need to do some .NET programming in Labview. I am prepared to do so and have read how to do it in labview. I believe all I need to do is:

a) open a new vi 

b) go into connectivity, .NET, constructor node

c) Browse to my .dll file

 

however when I do this, I get an error:

 

".NET DLL System.BadImageFormatException" saying something along the lines that

"This assembly is built by a runtime newer than the currently loaded runtime and can not be loaded"

 

Having seen the above error I thought maybe I need the latest .NET framework and so installed the .NET framework 4.5 and to no luck the error is still given. I even tried version 4.0 but there is no change. So I am stuck and do not know how to resolve it.

 

Q2) Am I doing the right thing? Is there an easier way than what I am doing. I am to use the 32bit library  which contains lots of .dll files which the matlab code uses. 

 

I paste the matlab code for you to see how I can implement this code into labview basically. I have not any experiance with .dll however I think I understand how I would implement it by first instantiating the main dll, an then calling the functions I want. The problem is I can not even instantiate the .dll in the first place due to the error above.

 

In short can I use my Matlab code into labview by making the least amount of change. Oh and I know Matlab script exists 🙂 however I want it to be independent of it. It works if I use the Matlab script. 

 

Regards

 

Saqib

 

 

Matlab code:

 


% ----------------------------------------------------------
% Add Assembly Radarlib3.NET.dll
% ----------------------------------------------------------


radarlib='E:\Yr 4\Little chip\Radarlib3_API\';
asmInfo = NET.addAssembly([radarlib,'Radarlib3.NET.dll']);

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Script Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Port description where the radar is conneced
% Format is:
% '<interface type>!Serial No: (<interface serial number>)!<radar module name>!<radar chip name>'
portname = 'FTx232H!Serial No: (F2X)!NVA-R631!NV100'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

% Variables
% The variables that are going to be used are stored in a map for
% convenient access and automatic update of the radar software and hardware
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

Variables = containers.Map;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

% Variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Variables('AveragingFactor') = 5;
Variables('Iterations') = 10;
Variables('Gain') = 5
Variables('ZoomMin') = 30;
Variables('ZoomMax') = 70;
Variables('FrameOffset') = 0; %50;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize connection
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
radar = NoveldaAPI.Radarlib3Wrapper.RadarWrapper;
radar.Open(portname);
fprintf('Object counter %d\n', radar.ObjectCounter);
fprintf('Connected to Novelda Radar on port %s\n', portname);

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize some local variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Read the NumberOfCounters variable from library
numCounters = radar.Item('SamplersPerFrame').Value.ToInt;
% Allocate space for the datavector
maxlen = 100;
datavector = zeros(maxlen,numCounters);
frame = zeros(numCounters,1);
frameptr = libpointer('doublePtr',frame);
n = 0;
l = 0;
i = 0;
time= 0;

% Update parameters by browsing through the Variables map
varlist = keys(Variables);
for n=1:length(varlist)
fprintf('Setting %s to %d\n', char(varlist(n)), Variables(char(varlist(n))));
radar.TryUpdateChip(char(varlist(n)),Variables(char(varlist(n))));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define graphics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 1 is used to plot the raw signal
figure;
axes('xlim', [1,numCounters], 'ylim', [0, 100]);
h = line(1:numCounters, frame);
shg;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read and plot data from the radar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Continue reading frames as long as the plot window is valid
while(ishandle(h))
tic;
c = double(radar.GetFrameNormalizedDouble);
set(h,'ydata',c);
% Draw frames takes some time.
% Uncomment "if statement" to get higher frame rate
%if (mod(i, 10) == 0)
drawnow;
%end
time = time + toc;
i = i+1;
% Store the data to datavector
if(i<=maxlen)
datavector(i,:) = c;
end
end

%fprintf('FPS: %f\n',i/time);


return

% Close the connection to the radar
fprintf('Closing connection..\n');
radar.Close;

0 Kudos
Message 1 of 6
(3,143 Views)

Hi Saqib,

 

You are on the right track with calling the DLL in LabVIEW. Do you have access to the source code for the DLLs you are calling? You may need to change the supported runtime to 4.0 or .NetFramework 4.0. There is some more info on the error you mentioned here:

 

http://social.msdn.microsoft.com/Forums/en-US/sqlmds/thread/54802f11-daec-4ade-9299-ed57ab7141f8/

 

Regards,

Zach P.

Staff Software Engineer | LabVIEW R&D | National Instruments
0 Kudos
Message 2 of 6
(3,104 Views)

Thank you for your reply,

 

What do you mean by source code for the DLL's I am calling?  Is it the code/application which I used to generate my dll library assembly files? If so, unfortunately these came from an installer which upon installing, I was left with the library dll files and I htink these library files are basically of a higher version. Can I not somehow do something in Labview to make it compatible?

 

I had a look at your link and it was interesting to see that they had problems like myself but they resolved it because they could specifiy selecting an older .NET framework 3.5 or less, such that they could build an older version library. Given that I am stuck with an installer which gives me an assembled library (which looks like its been compiled at a higher version) is there a way around this? 

 

I am open to alternatives - even TCP/IP comms if we have to (as the chip links to a computer via USB) - however I do prefer sticking to this .Net method.

 

I really look forward you reply,

 

Saqib

0 Kudos
Message 3 of 6
(3,097 Views)

Hi Saqib,

 

I understand that you can't work with .NET 3.5 but would it be possible to at least downgrade to .NET 4.0? 

 

.NET 4.0 and later does not intrinsically work with LabVIEW, and changes need to be made to allow communication when a .NET 4.0 framework is involved. I recommend reading the following as they have provided solutions to similar issues in the past:

Loading .NET 4.0 assemblies in LabVIEW

Loading .NET 4.0 Assemblies in LabVIEW

 


ShalimarA | CLA
0 Kudos
Message 4 of 6
(3,069 Views)

Playing with .NET versions had resulted in my PC disfunctioning altogether, however I have managed to reinstall windows with .NET 4.0 and yet the same message.

 

Can somebody please suggest an alternative solution as opposed to playing with .NET versions as I won't want to risk my PC again.

0 Kudos
Message 5 of 6
(3,031 Views)

Hi Saqib,

 

As you are now running .NET 4.0 I think it would be useful to take a look at the links that I sent in my previous post. Those discuss placing a configuration file within LabVIEW to allow it to communicate with .NET assemblies.


ShalimarA | CLA
0 Kudos
Message 6 of 6
(3,017 Views)