-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reward.cpp
102 lines (90 loc) · 3.14 KB
/
Reward.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*********************************************************************
*
* ANSI C Example program:
* WriteDigPort.c
*
* Example Category:
* DO
*
* Description:
* This example demonstrates how to write values to a digital
* output port.
*
* Instructions for Running:
* 1. Select the digital port on the DAQ device to be written.
* 2. Select a value to write.
* Note: The Data to Write control is in hexadecimal.
*
* Steps:
* 1. Create a task.
* 2. Create a Digital Output channel. Use one channel for all
* lines. In this case, the port itself acts as an individual
* channel.
* 3. Call the Start function to start the task.
* 4. Write digital port data. This write function writes a single
* sample of digital data on demand, so no timeout is necessary.
* 5. Call the Clear Task function to clear the Task.
* 6. Display an error if any.
*
* I/O Connections Overview:
* Make sure your signal output terminals match the Port I/O
* Control. In this case wire the item to receive the signal to the
* first N digital lines on your DAQ Device.
*
*********************************************************************/
#include <stdio.h>
#include "NIDAQmx.h"
#if _MSC_VER // this is defined when compiling with Visual Studio
#define EXPORT_API __declspec(dllexport) // Visual Studio needs annotating exported functions with this
#else
#define EXPORT_API // XCode does not need annotating exported functions, so define is empty
#endif
// ------------------------------------------------------------------------
// Plugin itself
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
// Link following functions C-style (required for plugins)
extern "C"
{
int EXPORT_API Reward(int on)
{
int error = 0;
TaskHandle taskHandle = 0;
uInt32 data = 0xffffffff; // large number, not zero!!!
char errBuff[2048] = { '\0' };
int32 written;
if (on == 0) {
data = 0;
}
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk(DAQmxCreateTask("", &taskHandle));
DAQmxErrChk(DAQmxCreateDOChan(taskHandle, "Dev1/port0", "", DAQmx_Val_ChanForAllLines));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk(DAQmxStartTask(taskHandle));
/*********************************************/
// DAQmx Write Code
/*********************************************/
DAQmxErrChk(DAQmxWriteDigitalU32(taskHandle, 1, 1, 10.0, DAQmx_Val_GroupByChannel, &data, &written, NULL));
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
return on;
Error:
if (DAQmxFailed(error))
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if (taskHandle != 0) {
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if (DAQmxFailed(error))
printf("DAQmx Error: %s\n", errBuff);
printf("End of program, press Enter key to quit\n");
getchar();
return -1;
}
} // end of export C block