431 lines
15 KiB
C#
431 lines
15 KiB
C#
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
#pragma warning disable IDE1006 // Naming Styles
|
|
#pragma warning disable SYSLIB1054 // Type or member is obsolete
|
|
|
|
namespace Helpers.DAQmx;
|
|
|
|
internal class Interop
|
|
{ // cSpell:disable
|
|
|
|
private const string lib = "DAQmx";
|
|
private static IntPtr libHandle = IntPtr.Zero;
|
|
|
|
static Interop() =>
|
|
NativeLibrary.SetDllImportResolver(typeof(Interop).Assembly, ImportResolver);
|
|
|
|
private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
|
|
{
|
|
if (libraryName == "DAQmx" && !(libHandle != IntPtr.Zero))
|
|
{
|
|
bool loaded;
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
loaded = NativeLibrary.TryLoad("C:/Windows/System32/nicaiu.dll", assembly, searchPath, out libHandle);
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
{
|
|
loaded = NativeLibrary.TryLoad("/usr/local/lib64/libnidaqmxbase.so", assembly, searchPath, out libHandle);
|
|
if (!loaded)
|
|
loaded = NativeLibrary.TryLoad("/usr/local/lib/libnidaqmxbase.so", assembly, searchPath, out libHandle);
|
|
if (!loaded)
|
|
loaded = NativeLibrary.TryLoad("/usr/local/natinst/lib/libnidaqmx.so", assembly, searchPath, out libHandle);
|
|
if (!loaded)
|
|
loaded = NativeLibrary.TryLoad("/usr/lib/x86_64-linux-gnu/libnidaqmx.so", assembly, searchPath, out libHandle);
|
|
}
|
|
else
|
|
throw new PlatformNotSupportedException("Unsupported platform for DAQmx library.");
|
|
if (!loaded)
|
|
throw new DllNotFoundException($"Failed to load {lib} library.");
|
|
}
|
|
return libHandle;
|
|
}
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxGetSampClkRate(IntPtr taskHandle, ref double data);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxSetSampClkRate(IntPtr taskHandle, double data);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxGetSampClkMaxRate(IntPtr taskHandle, ref double data);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxSetWriteRegenMode(IntPtr taskHandle, int data);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCreateAIVoltageChan(
|
|
IntPtr taskHandle,
|
|
string physicalChannel,
|
|
string nameToAssignToChannel,
|
|
int terminalConfig,
|
|
double minVal,
|
|
double maxVal,
|
|
int units,
|
|
string customScaleName);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCreateAOVoltageChan(
|
|
IntPtr taskHandle,
|
|
string physicalChannel,
|
|
string nameToAssignToChannel,
|
|
double minVal,
|
|
double maxVal,
|
|
int units,
|
|
string customScaleName);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCreateDOChan(
|
|
IntPtr taskHandle,
|
|
string lines,
|
|
string nameToAssignToLines,
|
|
int lineGrouping);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxGetErrorString(
|
|
int errorCode,
|
|
StringBuilder errorString,
|
|
uint buffersize);
|
|
|
|
#if unsafe
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern unsafe int DAQmxReadAnalogF64(
|
|
IntPtr taskHandle,
|
|
int numSampsPerChan,
|
|
double timeout,
|
|
int fillMode,
|
|
double* readArray,
|
|
uint arraySizeInSamps,
|
|
out IntPtr sampsPerChanRead,
|
|
IntPtr reserved);
|
|
#endif
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxReadAnalogScalarF64(
|
|
IntPtr taskHandle,
|
|
double timeout,
|
|
out double value,
|
|
IntPtr reserved);
|
|
|
|
#if unsafe
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern unsafe int DAQmxReadBinaryI16(
|
|
IntPtr taskHandle,
|
|
int numSampsPerChan,
|
|
double timeout,
|
|
int fillMode,
|
|
bool* readArray,
|
|
uint arraySizeInSamps,
|
|
out IntPtr sampsPerChanRead,
|
|
IntPtr reserved);
|
|
#endif
|
|
|
|
#if unsafe
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern unsafe int DAQmxReadBinaryU16(
|
|
IntPtr taskHandle,
|
|
int numSampsPerChan,
|
|
double timeout,
|
|
int fillMode,
|
|
ushort* readArray,
|
|
uint arraySizeInSamps,
|
|
out IntPtr sampsPerChanRead,
|
|
IntPtr reserved);
|
|
#endif
|
|
|
|
#if unsafe
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern unsafe int DAQmxReadBinaryI32(
|
|
IntPtr taskHandle,
|
|
int numSampsPerChan,
|
|
double timeout,
|
|
int fillMode,
|
|
int* readArray,
|
|
uint arraySizeInSamps,
|
|
out IntPtr sampsPerChanRead,
|
|
IntPtr reserved);
|
|
#endif
|
|
|
|
#if unsafe
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern unsafe int DAQmxReadBinaryU32(
|
|
IntPtr taskHandle,
|
|
int numSampsPerChan,
|
|
double timeout,
|
|
int fillMode,
|
|
uint* readArray,
|
|
uint arraySizeInSamps,
|
|
out IntPtr sampsPerChanRead,
|
|
IntPtr reserved);
|
|
#endif
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxConfigureLogging(
|
|
IntPtr taskHandle,
|
|
string filePath,
|
|
int loggingMode,
|
|
string groupName,
|
|
int operation);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxLoadTask(string taskName, out IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCreateTask(string taskName, out IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxAddGlobalChansToTask(IntPtr taskHandle, string[] channelNames);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxStartTask(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxStopTask(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxClearTask(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxWaitUntilTaskDone(IntPtr taskHandle, double timeToWait);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxWaitForValidTimestamp(
|
|
IntPtr taskHandle,
|
|
int timestampEvent,
|
|
double timeout,
|
|
CVIAbsoluteTime timestamp);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxIsTaskDone(IntPtr taskHandle, out uint isTaskDone);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxTaskControl(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxGetNthTaskChannel(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxGetNthTaskDevice(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxGetTaskAttribute(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCfgSampClkTiming(
|
|
IntPtr taskHandle,
|
|
string source,
|
|
double rate,
|
|
int activeEdge,
|
|
int sampleMode,
|
|
ulong sampsPerChan);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCfgHandshakingTiming(
|
|
IntPtr taskHandle,
|
|
int sampleMode,
|
|
ulong sampsPerChan);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCfgBurstHandshakingTimingImportClock(
|
|
IntPtr taskHandle,
|
|
int sampleMode,
|
|
ulong sampsPerChan,
|
|
double sampleClkRate,
|
|
string sampleClkSrc,
|
|
int sampleClkActiveEdge,
|
|
int pauseWhen,
|
|
int readyEventActiveLevel);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCfgBurstHandshakingTimingExportClock(
|
|
IntPtr taskHandle,
|
|
int sampleMode,
|
|
ulong sampsPerChan,
|
|
double sampleClkRate,
|
|
string sampleClkOutpTerm,
|
|
int sampleClkPulsePolarity,
|
|
int pauseWhen,
|
|
int readyEventActiveLevel);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCfgChangeDetectionTiming(
|
|
IntPtr taskHandle,
|
|
string risingEdgeChan,
|
|
string fallingEdgeChan,
|
|
int sampleMode,
|
|
ulong sampsPerChan);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCfgImplicitTiming(
|
|
IntPtr taskHandle,
|
|
int sampleMode,
|
|
ulong sampsPerChan);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxCfgPipelinedSampClkTiming(
|
|
IntPtr taskHandle,
|
|
string source,
|
|
double rate,
|
|
int activeEdge,
|
|
int sampleMode,
|
|
ulong sampsPerChan);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxGetTimingAttribute(
|
|
IntPtr taskHandle,
|
|
int attribute,
|
|
out object value);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxSetTimingAttribute(
|
|
IntPtr taskHandle,
|
|
int attribute,
|
|
object value);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxResetTimingAttribute(IntPtr taskHandle, int attribute);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxGetTimingAttributeEx(
|
|
IntPtr taskHandle,
|
|
string deviceNames,
|
|
int attribute,
|
|
out object value);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxSetTimingAttributeEx(
|
|
IntPtr taskHandle,
|
|
string deviceNames,
|
|
int attribute,
|
|
object value);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
internal static extern int DAQmxResetTimingAttributeEx(
|
|
IntPtr taskHandle,
|
|
string deviceNames,
|
|
int attribute);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxDisableStartTrig(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgDigEdgeStartTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSource,
|
|
int triggerEdge);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgAnlgEdgeStartTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSource,
|
|
int triggerSlope,
|
|
double triggerLevel);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgAnlgMultiEdgeStartTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSources,
|
|
int[] triggerSlopeArray,
|
|
double[] triggerLevelArray,
|
|
uint arraySize);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgAnlgWindowStartTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSource,
|
|
int triggerWhen,
|
|
double windowTop,
|
|
double windowBottom);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgTimeStartTrig(
|
|
IntPtr taskHandle,
|
|
CVIAbsoluteTime when,
|
|
int timescale);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgDigPatternStartTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSource,
|
|
string triggerPattern,
|
|
int triggerWhen);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxDisableRefTrig(IntPtr taskHandle);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgDigEdgeRefTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSource,
|
|
int triggerEdge,
|
|
uint pretriggerSamples);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgAnlgEdgeRefTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSource,
|
|
int triggerSlope,
|
|
double triggerLevel,
|
|
uint pretriggerSamples);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgAnlgMultiEdgeRefTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSources,
|
|
int[] triggerSlopeArray,
|
|
double[] triggerLevelArray,
|
|
uint pretriggerSamples,
|
|
uint arraySize);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgAnlgWindowRefTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSource,
|
|
int triggerWhen,
|
|
double windowTop,
|
|
double windowBottom,
|
|
uint pretriggerSamples);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxCfgDigPatternRefTrig(
|
|
IntPtr taskHandle,
|
|
string triggerSource,
|
|
string triggerPattern,
|
|
int triggerWhen,
|
|
uint pretriggerSamples);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxGetTrigAttribute(
|
|
IntPtr taskHandle,
|
|
int attribute,
|
|
out object value);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxSetTrigAttribute(IntPtr taskHandle, int attribute, object value);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxResetTrigAttribute(IntPtr taskHandle, int attribute);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxWriteAnalogF64(
|
|
IntPtr taskHandle,
|
|
int numSampsPerChan,
|
|
bool autoStart,
|
|
double timeout,
|
|
bool dataLayout,
|
|
double[] writeArray,
|
|
out IntPtr sampsPerChanWritten,
|
|
IntPtr reserved);
|
|
|
|
[DllImport("DAQmx", CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int DAQmxWriteDigitalLines(
|
|
IntPtr taskHandle,
|
|
int numSampsPerChan,
|
|
bool autoStart,
|
|
double timeout,
|
|
bool dataLayout,
|
|
byte[] writeArray,
|
|
out IntPtr sampsPerChanWritten,
|
|
IntPtr reserved);
|
|
|
|
} |