Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with using DAQmx library in Unity3D (C#)

Solved!
Go to solution

Hello everyone,

 

I'm trying to use the DAQmx C library in Unity to receive signal from National Instruments hardware in Unity.

 

Because the C library is static and there is no .dll file, only .lib and header file, I created a wrapper dll that I chose which DAQmx method to integrate into Unity.

 

Currently I implemented the following methods:

DAQmxCreateTask(taskName, taskHandle);
DAQmxStopTask(taskHandle);
DAQmxStartTask(taskHandle);
DAQmxCreateAIVoltageChan(taskHandle, physicalChannel, nameToAssignToChannel, terminalConfig, minVal, maxVal, units, customScaleName);
DAQmxSetSampTimingType(taskHandle, data);
DAQmxReadAnalogF64(taskHandle, numSampsPerChan, timeout, fillMode, readArray, arraySizeInSamps, sampsPerChanRead, reserved);
DAQmxCfgSampClkTiming(taskHandle, source, rate, activeEdge, sampleMode, sampsPerChan);
DAQmxGetExtendedErrorInfo(errorString, bufferSize);

When I try to replicate this example "Acq-IntClk" in Unity, only my DAQmxCreateTask() call returned a 0, and when I call DAQmxCreateAIVoltageChan(); DAQmxCfgSampClkTiming(); DAQmxStartTask(); DAQmxReadAnalogF64(); they all returned error code -200088, which seemed to be this https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019QMjSAM&l=en-US , meaning "Task specified is invalid or does not exist."

 

Further more, when I try to use DAQmxGetExtendedErrorInfo() to get the error message, it doesn't seem to properly write value to the char[] I'm passing in. And it doesn't seem to work with string either. However when I try to pass an empty string (string errBuff = "";) with a char[] size of 2048, Unity will crash, which means that Unity is accessing the C library successfully? (DAQmxCreateTask() returns 0 should also prove this)

 

I'm a Unity developer, and I don't have any previous experience with DAQmx. I've successfully integrated and using other native library in Unity before, but I'm stuck on this one.

0 Kudos
Message 1 of 6
(4,037 Views)

First, I'm not sure what you refer to about creating a DLL from the DAQmx lib file. That lib file is simply an import library for the actual DAQmx dll file anyhow, so there IS actually a DLL file you can call. If you need some DLL import directives in Unity3D similar to C#'s Interop DLLImport declarations, since you can't link in lib files. All you need to do is directly referencing the DAQmx user space DLL nicaiu.dll that gets installed in your System directory by the DAQmx installer.

 

The problem you see could be either that your turning the daqmx.lib into a daqmx.dll didn't quite initialize the real daqmx dll correctly or it could be something with your Unity3D import directives not being quite right. This Interop stuff is always tricky to do right.

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 6
(3,972 Views)

And since Unity3D seems to use C# in fact, you should not need to have to create your own Interop import declarations to use DAQmx from it.

 

This document describes the installation of DAQmx and other driver support for Visual Studio C# or other .Net applications. You should be able to import the according libraries directly in your C# project after that. 

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 6
(3,962 Views)

Hi, thanks for the help.

 

I tried to use the .net DLL for DAQmx in Unity, but it shows up as native plugin instead of managed plugin.

 

When I look this up, other people are having the same problem, and they suspect that DAQmx .net DLL is a "mixed library".

 

So I then try to wrap the c library (which according to internet is a "static library" hence no DLL for it) into a C++ DLL and use it in Unity.

 

It looks like at least this method is partially working, as the CreateTask() is returning a "0", meaning it succeeded, but all other methods returns the same error code -200088.

0 Kudos
Message 4 of 6
(3,951 Views)
Solution
Accepted by topic author SnowTV

While the DAQmx .Net interface is of course a mixed library internally I was under the impression that its interface is fully managed. Under the hood it simply calls into the real DAQmx DLLs which of course are fully native. So I'm not quite sure what the problem is here. Is Unity3D only able to call fully managed DLLs? Doesn't it support Interop calls, that are used to call into native libraries under the hood?

Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 6
(3,942 Views)

Yes, I believe Unity cannot read mixed library. It sees the .Net dll as unmanaged.

 

However, I succeeded using LabView to get the readings and send to Unity through UDP.

 

Thanks for the help!

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