Scope
DAQmxTask.h
1 #pragma once
2 
3 // Include and link National Instruments DAQmx library
4 #include <NIDAQmx.h>
5 #pragma comment(lib, "NIDAQmx.lib")
6 
7 #include "ScopeDatatypes.h"
8 
10 namespace DAQmx {
11 
15 bool CheckError(const int32& error);
16 
33 double PredictSampleRate(const double& _desiredrate, const uint32_t& _nochannels, const double& _refclockrate, const int32& _mode);
34 
45 double CoerceSampleRates(const double& _desiredrate, const uint32_t& _nochannelsin, const uint32_t& _nochannelsout, const double& _refclockrate);
46 
51 std::wstring ClockString(const scope::DaqTimingHelper::Mode& _timing, const std::wstring& _externalclocksource);
52 
54 class CDAQmxTask {
55 
56 protected:
58  TaskHandle task_handle;
59 
60 public:
61  CDAQmxTask();
62 
64  ~CDAQmxTask(void);
65 
67  void CreateTask(const std::wstring& _name = L"");
68 
70  bool Created() const;
71 
75  void ConfigureImplicitTiming(const int32& _mode, const int32& _samplesperchan);
76 
83  void ConfigureSampleTiming(const std::wstring& _src, const float64& _rate, const int32& _samplesperchan, const int32& _samplingtype = DAQmx_Val_ContSamps, const int32& _actedge = DAQmx_Val_Rising);
84 
88  void ConfigureReferenceClock(const std::wstring& _src, const float64& _rate);
89 
93  void ConfigureDigStartTrigger(const std::wstring& _src, const int32& _trigedge = DAQmx_Val_Rising);
94 
96  void Start(void);
97 
99  void Stop(void);
100 
102  void Clear(void);
103 
105  bool IsTaskDone(void);
106 
110  int32_t WaitUntilDone(const float64& _waittime);
111 
127  void ExportSignal(int32 _signal, const std::wstring& _terminal);
128 
130  void SetWriteOffset(int32 posreltofirst);
131 
133  void SetRegeneration(const bool& _regenerate);
134 };
135 
138  : public CDAQmxTask {
139 
140 public:
149  void CreateAOVoltageChannel(const std::wstring& _devices,
150  const std::wstring& _channelname = L"",
151  float64 _minval = -10.0,
152  float64 _maxval = 10.0,
153  int32 _units = DAQmx_Val_Volts,
154  const std::wstring& _customscalename = L"");
155 
159  uInt32 ConfigureBuffer(const uInt32& _sampsperchannel);
160 
164  uInt32 ConfigureOnboardBuffer(const uInt32& _sampsperchannel);
165 
169  void UseOnlyOnboardMemory(const std::wstring& _channel);
170 
177  void SetIdleOutputBehaviour(const std::wstring& _channel, const int32_t& _behav);
178 
185  void SetDataTransferCondition(const std::wstring& _channel, const int32_t& _cond);
186 
194  void SetDataTransferMode(const std::wstring& _channel, const int32_t& _mode);
195 
204  int32 WriteAnalog(const float64* _data, int32 _sampsperchan, bool _autostart = false, int32 _timeout = 2, bool32 _layout = DAQmx_Val_GroupByChannel);
205 
210  void WriteAnalogScalar(const float64& _value, bool _autostart = false, int32 _timeout = 1000);
211 
220  int32 WriteAnalogI16(const int16* _data, int32 _sampsperchan, bool _autostart = false, int32 _timeout = 2, bool32 _layout = DAQmx_Val_GroupByChannel);
221 };
222 
225  : public CDAQmxTask {
226 
227 public:
231  void CreateDOChannel(const std::wstring& _devicelines, const std::wstring& _channelname = L"");
232 
241  int32 WriteDigitalU8(const uInt8* _data, int32 _sampsperchan, bool _autostart = false, float64 _timeout = 2, bool32 _layout = DAQmx_Val_GroupByChannel);
242 
251  int32_t WriteDigitalLines(const uInt8* _data, int32 _sampsperchan, bool _autostart = false, float64 _timeout = 2, bool32 _layout = DAQmx_Val_GroupByChannel);
252 };
253 
254 
259  : public CDAQmxTask {
260 
261 protected:
263  std::mersenne_twister<uint32_t, 32, 624, 397, 31, 0x9908b0df, 11, 7, 0x9d2c5680, 15, 0xefc60000, 18> mtgen;
264 
265 public:
267 
280  void CreateAIVoltageChannel(const std::wstring& _devicechannel,
281  const std::wstring& _name = L"",
282  int32 _terminalconfig = DAQmx_Val_Cfg_Default ,
283  float64 _minval = -10.0,
284  float64 _maxval = 10.0,
285  int32 _units = DAQmx_Val_Volts,
286  const std::wstring& _customscalename = L"");
287 
290  void ConfigureBuffer(const uInt32& _sampsperchannel);
291 
301  int32 ReadU16(std::vector<uint16_t>& _data, const int32& _sampsperchan, const uint32_t& _channels, bool& _timedout, const float64& _timeout = 2);
302 
311  int32 ReadU16Dummy(std::vector<uint16_t>& _data, const int32& _sampsperchan, const uint32_t& _channels, bool& _timedout, const float64& _timeout = 2);
312 
323  int32 ReadI16(std::vector<int16_t>& _data, const int32& _sampsperchan, const uint32_t& _channels, bool& _timedout, const float64& _timeout = 2);
324 };
325 
328  : public CDAQmxTask {
329 
330 public:
340  void CreateCOChannel(const std::wstring& _counter
341  , const std::wstring& _name = L""
342  , int32 _idlestate = 0
343  , float64 _initdelay = 0
344  , float64 _lowtime = 0.1
345  , float64 _hightime = 0.1);
346 };
347 
348 }
int32_t WriteDigitalLines(const uInt8 *_data, int32 _sampsperchan, bool _autostart=false, float64 _timeout=2, bool32 _layout=DAQmx_Val_GroupByChannel)
Writes to up to 8 digital lines (in one port, I suppose), depending on which lines are configured int...
Definition: DAQmxTask.cpp:296
uInt32 ConfigureOnboardBuffer(const uInt32 &_sampsperchannel)
Configure the output buffer on the device hardware.
Definition: DAQmxTask.cpp:211
int32 WriteAnalog(const float64 *_data, int32 _sampsperchan, bool _autostart=false, int32 _timeout=2, bool32 _layout=DAQmx_Val_GroupByChannel)
Writes several samples to the buffer of an analog output task.
Definition: DAQmxTask.cpp:239
void SetRegeneration(const bool &_regenerate)
Sets the mode of sample regeneration.
Definition: DAQmxTask.cpp:177
void WriteAnalogScalar(const float64 &_value, bool _autostart=false, int32 _timeout=1000)
Writes a single sample to the buffer of an analog output task.
Definition: DAQmxTask.cpp:251
void ConfigureReferenceClock(const std::wstring &_src, const float64 &_rate)
Configures the reference clock for the task.
Definition: DAQmxTask.cpp:128
int32_t WaitUntilDone(const float64 &_waittime)
Waits until the task is done.
Definition: DAQmxTask.cpp:163
std::wstring ClockString(const scope::DaqTimingHelper::Mode &_timing, const std::wstring &_externalclocksource)
Generates a string for the sample clock from daq_timing.
Definition: DAQmxTask.cpp:46
bool Created() const
Definition: DAQmxTask.cpp:96
uInt32 ConfigureBuffer(const uInt32 &_sampsperchannel)
Configure the daqmx output buffer on the PC.
Definition: DAQmxTask.cpp:201
void CreateDOChannel(const std::wstring &_devicelines, const std::wstring &_channelname=L"")
Creates a digital output channel.
Definition: DAQmxTask.cpp:270
void SetIdleOutputBehaviour(const std::wstring &_channel, const int32_t &_behav)
Configures the output behaviour when the task is not running.
Definition: DAQmxTask.cpp:224
void ExportSignal(int32 _signal, const std::wstring &_terminal)
Exports a signal to an output terminal.
Definition: DAQmxTask.cpp:167
Wraps a DAQmx Counter Output task.
Definition: DAQmxTask.h:327
int32 WriteAnalogI16(const int16 *_data, int32 _sampsperchan, bool _autostart=false, int32 _timeout=2, bool32 _layout=DAQmx_Val_GroupByChannel)
Writes several samples to the buffer of an analog output task.
Definition: DAQmxTask.cpp:258
void SetWriteOffset(int32 posreltofirst)
Sets the write offset relative to the first buffer position.
Definition: DAQmxTask.cpp:172
void SetDataTransferCondition(const std::wstring &_channel, const int32_t &_cond)
Configures the condition for data transfer to the device.
Definition: DAQmxTask.cpp:229
void Start(void)
Starts the task.
Definition: DAQmxTask.cpp:145
Base class for DAQmx tasks.
Definition: DAQmxTask.h:54
void UseOnlyOnboardMemory(const std::wstring &_channel)
Configures the task to use only the devices' onboard memory.
Definition: DAQmxTask.cpp:219
std::mersenne_twister< uint32_t, 32, 624, 397, 31, 0x9908b0df, 11, 7, 0x9d2c5680, 15, 0xefc60000, 18 > mtgen
For the ReadDummy function which returns random numbers.
Definition: DAQmxTask.h:263
In here all declarations for all kinds of datatypes Scope needs.
bool CheckError(const int32 &error)
Checks return value of NI DAQmx function for error, prints out error information and throws an except...
Definition: DAQmxTask.cpp:9
TaskHandle task_handle
the DAQmx task handle
Definition: DAQmxTask.h:58
Wraps a DAQmx Digital Output task.
Definition: DAQmxTask.h:224
~CDAQmxTask(void)
Clear the task.
Definition: DAQmxTask.cpp:86
This is basically a C++ wrapper around NI's DAQmx C library.
Definition: DAQmxTask.cpp:7
void CreateAOVoltageChannel(const std::wstring &_devices, const std::wstring &_channelname=L"", float64 _minval=-10.0, float64 _maxval=10.0, int32 _units=DAQmx_Val_Volts, const std::wstring &_customscalename=L"")
Creates an analog output channel Allowed values for terminal configuration (from NiDAQmx.h): Values for DAQmx_PhysicalChan_AI_TermCfgs and DAQmx_PhysicalChan_AO_TermCfgs Value set TerminalConfigurationBits .
Definition: DAQmxTask.cpp:182
Wraps a DAQmx Analog Output task.
Definition: DAQmxTask.h:137
void Clear(void)
Clear the task.
Definition: DAQmxTask.cpp:153
void CreateTask(const std::wstring &_name=L"")
Definition: DAQmxTask.cpp:91
void ConfigureBuffer(const uInt32 &_sampsperchannel)
Configures input buffer (there is one buffer per channel)
Definition: DAQmxTask.cpp:333
void ConfigureSampleTiming(const std::wstring &_src, const float64 &_rate, const int32 &_samplesperchan, const int32 &_samplingtype=DAQmx_Val_ContSamps, const int32 &_actedge=DAQmx_Val_Rising)
Configures a sample timing for the task.
Definition: DAQmxTask.cpp:109
int32 ReadI16(std::vector< int16_t > &_data, const int32 &_sampsperchan, const uint32_t &_channels, bool &_timedout, const float64 &_timeout=2)
Reads from a device as signed 16bit integers.
Definition: DAQmxTask.cpp:369
int32 WriteDigitalU8(const uInt8 *_data, int32 _sampsperchan, bool _autostart=false, float64 _timeout=2, bool32 _layout=DAQmx_Val_GroupByChannel)
Writes to an 8 bit digital output port, lowest bit is line0, highest bit is line7.
Definition: DAQmxTask.cpp:284
double PredictSampleRate(const double &_desiredrate, const uint32_t &_nochannels, const double &_refclockrate, const int32 &_mode)
Predicts the actual sampling rate.
Definition: DAQmxTask.cpp:23
void ConfigureImplicitTiming(const int32 &_mode, const int32 &_samplesperchan)
Configures implicit timing for the task.
Definition: DAQmxTask.cpp:100
void SetDataTransferMode(const std::wstring &_channel, const int32_t &_mode)
Configures the mode of data transfer between the daqmx buffer on the PC and the onboard buffer of the...
Definition: DAQmxTask.cpp:234
void CreateCOChannel(const std::wstring &_counter, const std::wstring &_name=L"", int32 _idlestate=0, float64 _initdelay=0, float64 _lowtime=0.1, float64 _hightime=0.1)
Creates a counter output task.
Definition: DAQmxTask.cpp:395
double CoerceSampleRates(const double &_desiredrate, const uint32_t &_nochannelsin, const uint32_t &_nochannelsout, const double &_refclockrate)
Calculates a sampling rate which both analog in and out can comply to.
Definition: DAQmxTask.cpp:37
void CreateAIVoltageChannel(const std::wstring &_devicechannel, const std::wstring &_name=L"", int32 _terminalconfig=DAQmx_Val_Cfg_Default, float64 _minval=-10.0, float64 _maxval=10.0, int32 _units=DAQmx_Val_Volts, const std::wstring &_customscalename=L"")
Definition: DAQmxTask.cpp:312
void ConfigureDigStartTrigger(const std::wstring &_src, const int32 &_trigedge=DAQmx_Val_Rising)
Configures the start trigger.
Definition: DAQmxTask.cpp:134
void Stop(void)
Stops the task.
Definition: DAQmxTask.cpp:149
int32 ReadU16Dummy(std::vector< uint16_t > &_data, const int32 &_sampsperchan, const uint32_t &_channels, bool &_timedout, const float64 &_timeout=2)
Generates random numbers or zeros instead of real read from a device.
Definition: DAQmxTask.cpp:361
Wraps a DAQmx Analog Input Task.
Definition: DAQmxTask.h:258
bool IsTaskDone(void)
Definition: DAQmxTask.cpp:157
int32 ReadU16(std::vector< uint16_t > &_data, const int32 &_sampsperchan, const uint32_t &_channels, bool &_timedout, const float64 &_timeout=2)
Definition: DAQmxTask.cpp:340