forked from mccdaq/uldaq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDOutScan.c
228 lines (175 loc) · 6.41 KB
/
DOutScan.c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
UL call demonstrated: ulDOutScan()
Purpose: Continuously output user generated
data on the first digital port
Demonstration: Outputs user specified data
on the first digital port
Steps:digital
1. Call ulGetDaqDeviceInventory() to get the list of available DAQ devices
2. Call ulCreateDaqDevice() to to get a handle for the first DAQ device
3. Verify the DAQ device has an digital output subsystem
4. Verify the digital output subsystem has a hardware pacer
5. Call ulConnectDaqDevice() to establish a UL connection to the DAQ device
6. Call ulDConfigPort to configure the port for output
7. Generate the data to be output
8. Call ulDOutScan() to start the scan for the DIO subsystem
9. Call ulDOutScanStatus() to check the status of the background operation
10. Call ulDOutScanStop() to stop the background operation
11. Call ulDisconnectDaqDevice() and ulReleaseDaqDevice() before exiting the process.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "uldaq.h"
#include "utility.h"
// prototypes
void CreateOutputData(int samplesPerPort, long long bitsPerPort, unsigned long long* buffer);
#define MAX_DEV_COUNT 100
#define MAX_STR_LENGTH 64
#define MAX_SCAN_OPTIONS_LENGTH 256
int main(void)
{
int descriptorIndex = 0;
DaqDeviceDescriptor devDescriptors[MAX_DEV_COUNT];
DaqDeviceInterface interfaceType = ANY_IFC;
DaqDeviceHandle daqDeviceHandle = 0;
unsigned int numDevs = MAX_DEV_COUNT;
DigitalPortType lowPort = AUXPORT;
DigitalPortType highPort = AUXPORT;
const int samplesPerPort = 1000;
double rate = 1000;
ScanOption scanOptions = (ScanOption) (SO_DEFAULTIO | SO_CONTINUOUS);
DOutScanFlag flags = DOUTSCAN_FF_DEFAULT;
int hasDIO = 0;
int hasPacer = 0;
int index = 0;
int bitsPerPort = 0;
char portTypeStr[MAX_STR_LENGTH];
char scanOptionsStr[MAX_SCAN_OPTIONS_LENGTH];
DigitalPortType portType = AUXPORT;
int chanCount = 1;
unsigned long long* buffer;
UlError err = ERR_NO_ERROR;
int i = 0;
int __attribute__((unused)) ret;
char c;
// Get descriptors for all of the available DAQ devices
err = ulGetDaqDeviceInventory(interfaceType, devDescriptors, &numDevs);
if (err != ERR_NO_ERROR)
goto end;
// verify at least one DAQ device is detected
if (numDevs == 0)
{
printf("No DAQ device is detected\n");
goto end;
}
printf("Found %d DAQ device(s)\n", numDevs);
for (i = 0; i < (int) numDevs; i++)
printf(" %s: (%s)\n", devDescriptors[i].productName, devDescriptors[i].uniqueId);
// get a handle to the DAQ device associated with the first descriptor
daqDeviceHandle = ulCreateDaqDevice(devDescriptors[descriptorIndex]);
if (daqDeviceHandle == 0)
{
printf ("\nUnable to create a handle to the specified DAQ device\n");
goto end;
}
// verify the specified DAQ device supports the DIO subsystem
err = getDevInfoHasDio(daqDeviceHandle, &hasDIO);
if (!hasDIO)
{
printf("\nThis device does not support digital I/O\n");
goto end;
}
// verify the specified DAQ device supports hardware output pacing
err = getDioInfoHasPacer(daqDeviceHandle, DD_OUTPUT, &hasPacer);
if (!hasPacer)
{
printf("\nThe specified DAQ device does not support hardware paced digital output\n");
goto end;
}
printf("\nConnecting to device %s - please wait ...\n", devDescriptors[descriptorIndex].devString);
// establish a connection to the DAQ device
err = ulConnectDaqDevice(daqDeviceHandle);
if (err != ERR_NO_ERROR)
goto end;
// get the first supported port type
err = getDioInfoFirstSupportedPortType(daqDeviceHandle, &portType, portTypeStr);
lowPort = portType;
highPort = portType;
// get the number of bits in the port
err = getDioInfoNumberOfBitsForFirstPort(daqDeviceHandle, &bitsPerPort);
err = ulDConfigPort(daqDeviceHandle, portType, DD_OUTPUT);
// allocate a buffer for the output data
buffer = (unsigned long long*) malloc(chanCount * samplesPerPort * sizeof(unsigned long long));
if(buffer == 0)
{
printf("\nOut of memory, unable to create scan buffer\n");
goto end;
}
CreateOutputData(samplesPerPort, bitsPerPort, buffer);
ConvertScanOptionsToString(scanOptions, scanOptionsStr);
printf("\n%s ready\n", devDescriptors[descriptorIndex].devString);
printf(" Function demonstrated: ulDOutscan()\n");
printf(" Port: %s\n", portTypeStr);
printf(" Bits per port: %d\n", bitsPerPort);
printf(" Samples per port: %d\n", samplesPerPort);
printf(" Rate: %f\n", rate);
printf(" Scan options: %s\n", scanOptionsStr);
printf("\nHit ENTER to continue\n");
ret = scanf("%c", &c);
ret = system("clear");
// start the output
err = ulDOutScan(daqDeviceHandle, lowPort, highPort, samplesPerPort, &rate, scanOptions, flags, buffer);
if(err == ERR_NO_ERROR)
{
ScanStatus status;
TransferStatus transferStatus;
ulDOutScanStatus(daqDeviceHandle, &status, &transferStatus);
while(status == SS_RUNNING && err == ERR_NO_ERROR && !enter_press())
{
err = ulDOutScanStatus(daqDeviceHandle, &status, &transferStatus);
if(err == ERR_NO_ERROR)
{
resetCursor();
printf("Hit 'Enter' to terminate the process\n\n");
printf("actual scan rate = %f\n\n", rate);
index = transferStatus.currentIndex;
printf("currentScanCount = %10llu \n", transferStatus.currentScanCount);
printf("currentTotalCount = %10llu \n", transferStatus.currentTotalCount);
printf("currentIndex = %10d \n\n", index);
usleep(10000);
}
}
ulDOutScanStop(daqDeviceHandle);
}
// before leaving, configure the entire first port for input
err = ulDConfigPort(daqDeviceHandle, portType, DD_INPUT);
// disconnect from the DAQ device
ulDisconnectDaqDevice(daqDeviceHandle);
end:
// release the handle to the DAQ device
ulReleaseDaqDevice(daqDeviceHandle);
if(err != ERR_NO_ERROR)
{
char errMsg[ERR_MSG_LEN];
ulGetErrMsg(err, errMsg);
printf("Error Code: %d \n", err);
printf("Error Message: %s \n", errMsg);
}
return 0;
}
void CreateOutputData(int samplesPerPort, long long bitsPerPort, unsigned long long* buffer)
{
const double twoPI = 2 * M_PI;
int sample;
double amplitude = pow((double)2.0, bitsPerPort) - 1;
double f = twoPI / samplesPerPort;
double phase = 0.0;
for (sample=0; sample<samplesPerPort; sample++)
{
*buffer++ = (unsigned long long) (sin(phase) * amplitude/2 + (amplitude / 2));
phase += f;
if( phase > twoPI )
phase = phase - twoPI;
}
}