11-01-2009 12:59 AM
Solved! Go to Solution.
11-02-2009 06:18 PM
11-02-2009 07:26 PM
Hi Jim,
Thank you for your response.
In case someone else comes across the same problem (lack of MemoryOptimized methods in CounterReader), I will post here my workaround, which involves building a C DLL to expose the DAQmx C API, and then calling it via platform-invoke from C#. Note that the C# digs into the internals of the Task object, so don't be suprised if it breaks in future versions of DAQmx. Disclaimers aside, this seems to work correctly on version 9.0.
/* C code: Compile to CWrapper.dll, depends on static library nidaqmx.lib */
#include <NIDAQmx.h>
dllexport int32 __CFUNC ReadCounterU32 (TaskHandle taskHandle,
int32 numSampsPerChan, float64 timeout, uInt32 readArray[],
uInt32 arraySizeInSamps, int32 *sampsPerChanRead)
{
return DAQmxReadCounterU32(taskHandle, numSampsPerChan,
timeout, readArray, arraySizeInSamps, sampsPerChanRead, NULL);
}
// C# code
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using NationalInstruments.DAQmx;
[DllImport("CWrapper.dll")]
extern static int ReadCounterU32(IntPtr taskHandle, int numSampsPerChan,
double timeout, uint[] readArray, int arraySizeInSamps,
out int samplesPerChanRead);
unsafe static IntPtr GetTaskHandle(Task t)
{
var pTaskField = typeof(Task).GetField("m_pTask",
BindingFlags.Instance | BindingFlags.NonPublic);
object asObj = pTaskField.GetValue(t);
return (IntPtr)Pointer.Unbox(asObj);
}
// can now read from a counter Task by first getting its IntPtr taskHandle
// and then passing that into the extern ReadCounterU32
// according to the C API documentation
Best,
Gabriel
11-03-2009 07:31 AM
Gabriel -
Thank you for posting your code showing how to do this via the C API.
We will add implementation of MemoryOptimizedRead for counter input to our list of items to add in a future version of the DAQmx .NET API.
Do you have a similar need for digital?
Is there any other functionality that you see as missing from the .NET API?
David Rohacek
National Instruments
11-03-2009 02:40 PM - edited 11-03-2009 02:47 PM
Hi David,
I don't have a similar need for digital read at the moment. But I'd add that even the current MemoryOptimizedRead has some disadvantages compared to the C API:
For these reasons, I prefer my ugly p/invoke hack to the API-provided MemoryOptimizedRead, and will use it even for Analog acquisition.
So for future versions of the .NET API, I'd suggest adding a set of NonAllocatingRead() methods to the AnalogReader and CounterReader classes, which just wrap the C functions:
public void NonAllocatingRead(int samplesPerChannel, double[] readArray, out int samplesPerChannelRead);
If that were available, I'd gladly use it over my p/invoke hack.
Best,
Gabriel
11-04-2009 02:58 PM - edited 11-04-2009 02:59 PM
Gabriel -
Thank you for your feedback. We did have #2 on our radar, but not #1. We're now tracking #1; I can't commit to when we'll implement these enhancements (fixes?) in the DAQmx .NET API, but we have given them a high prioriry.
Just a note of interest - the DAQmx .NET API is not a layer on top of the C API, so implementations of these functions cannot be done as simple wrappers. The C, LabVIEW, and .NET APIs are all at the same layer in the software stack. This architecture yields some performance benefit over putting the .NET API on top of the C API.
David Rohacek
National Instruments
11-04-2009 03:13 PM
Good to hear-- Thanks, David.
03-16-2010 08:54 AM
Hi Gabriel,
I just wanted to follow up with you regarding the issues you brought up. In the latest DAQmx release, NI-DAQmx 9.1, we now provide the functionality you requested in #1 for AnalogSingleChannelReader and AnalogMultiChannelReader. We have new overloads for MemoryOptimizedReads that address #1.
We still need to provide this same functionality for Digital and Counter Readers. We have plans to provide this functionality in a future NI-DAQmx release, but we cannot commit right now as to when we can provide this functionality. As soon as we have this functionality, I will update this thread again to let you know.
Canisius Rozario
National Instruments
08-09-2011 10:27 AM
Hi Gabriel,
The feature that you were looking for (MemoryOptimizedRead for Counter Input) is now available with NI-DAQmx 9.4 release.
Please see http://joule.ni.com/nidu/cds/view/p/id/2604/lang/en for more details.
Feel free to post any questions you have.
Thanks
Bhavesh Shura
National Instruments
08-10-2011 10:49 AM - edited 08-10-2011 10:51 AM
Hi Canisius and Bhavesh,
Thank you for the features, and for keeping this thread updated. I really appreciate this kind of customer support.
Have there been any changes regarding point #2 from my post of 3-Nov-2009? David Rohacek replied saying that issue was being tracked, but I haven't seen any follow-up.
Thanks for your time,
Gabriel