-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadout.cpp
More file actions
175 lines (137 loc) · 4.88 KB
/
readout.cpp
File metadata and controls
175 lines (137 loc) · 4.88 KB
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
#include "stdafx.h"
#include "windows.h"
#include "quickusb.h"
#include <iostream>
#include <fstream>
#include "PlatformIndependentTimer.h"
#include "APIUSB.h"
using namespace std;
ofstream myfile ("example.bin", ios::out | ios::app | ios::binary | ios::ate);
static bool writing = false;
const int NumBuffers = 8;
const int BufferByteSize = 65536;
QBULKSTREAM BulkStream[NumBuffers];
///////////////////////////////////////
// The BulkStream completion routine //
///////////////////////////////////////
QVOIDRETURN CompletionRoutine(PQBULKSTREAM BulkStream)
{
if (!BulkStream) { return; }
// Check for errors
if (BulkStream->Error) {
cout << "--> CR: ERROR! Failed with error " << BulkStream->Error << " (" << BulkStream->BytesTransferred << " of " << BulkStream->BytesRequested << " bytes)" << endl;
} else {
//cout << "--> CR: Completed request for " << BulkStream->BytesTransferred << " bytes" << endl;
}
while (true) {
if (writing) continue;
writing = true;
/*for (unsigned short i=0; i<NumBuffers; i++) {
//const unsigned short i = 2; //FIXME for debugging
for (unsigned int j=0; j<BufferByteSize/2; j++) {
unsigned short word = (BufferArray[i][2*j]<<8)+BufferArray[i][2*j+1]; // group the two bytes into a 16-bit word
if (word==0x00ff) continue;
myfile << word;
//std::cout << std::hex << word << std::endl;
}
}*/
for (unsigned int j=0; j<BufferByteSize/2; j++) {
unsigned short word = (BulkStream->Buffer[2*j]<<8)+BulkStream->Buffer[2*j+1]; // group the two bytes into a 16-bit word
if (word==0x00ff) continue;
myfile << word;
std::cout << std::hex << word << std::endl; //FIXME delete me!!
}
//std::cout << BulkStream->StreamID << " --- " << BulkStream->RequestID << " --- " << BulkStream->Reserved << std::endl;
//myfile << BufferArray;
writing = false;
return;
}
}
int main(int argc, char* argv[])
{
// Variables
QCHAR nameList[1024];
QRESULT qResult;
QULONG qusbError;
QHANDLE hDevice;
QBYTE streamID;
PlatformIndependentTimer timer;
double tElapsed;
if (myfile.is_open()){
cout<<"File 'namefile.bin' created" << endl;
}
else {
cout << "Unable to open/create 'filename'"<<endl;
return 0;
}
printf("Hello QuickUSB World!\n");
// Query connected modules
qResult = QuickUsbFindModules(nameList, 1024);
if (!qResult) {
QuickUsbGetLastError(&qusbError);
cout << "QuickUSB Error: " << qusbError << endl;
return qusbError;
}
// Open the first module
qResult = QuickUsbOpen(&hDevice, nameList);
if (!qResult) {
QuickUsbGetLastError(&qusbError);
cout << "QuickUSB Error: " << qusbError << endl;
return qusbError;
}
// Setting timeout
QuickUsbSetTimeout (hDevice, 1000);
QuickUsbWriteSetting(hDevice, 1, 1); // 16 bits words
QuickUsbWriteSetting(hDevice, 2, 32768+200); // readout register address
// Start timing
timer.Start();
// Begin streaming data
//qResult = QuickUsbReadBulkDataStartStream(hDevice, BufferArray, NumBuffers, BufferByteSize, &CompletionRoutine, 0, &streamID, 8, 4);
qResult = QuickUsbReadBulkDataStartStream(hDevice, 0, NumBuffers, BufferByteSize, &CompletionRoutine, 0, &streamID, 8, 4);
if (!qResult) {
QuickUsbGetLastError(&qusbError);
cout << "QuickUSB Error: " << qusbError << endl;
return qusbError;
}
cout << "Streaming data for 5 seconds..." << endl;
// Acquire data in the background for 5 seconds
do {
// Check if our time has elapsed
tElapsed = timer.GetElapsedTimeInSeconds();
} while (tElapsed < 5.0);
// Stop the data aquisition, but do not stop the stream from the buffer to file
cout << "Stop Data Aquisition!" << endl;
qResult = QuickUsbStopStream(hDevice, streamID, TRUE);
if (!qResult) {
QuickUsbGetLastError(&qusbError);
cout << "QuickUSB Error: " << qusbError << endl;
return qusbError;
}
// Stop the data stream
cout << "Stop Data Streaming from buffer to file" << endl;
// Stop the data stream, but block this time
qResult = QuickUsbStopStream(hDevice, streamID, FALSE);
if (!qResult) {
QuickUsbGetLastError(&qusbError);
// Check the error
if (qusbError == QUICKUSB_ERROR_NOT_STREAMING) {
// The stream has already finished shutting down. This is not an error
// because we want the stream to be stopped, so continue on normally.
} else {
cout << "QuickUSB Error: " << qusbError << endl;
}
}
// Close the device
qResult = QuickUsbClose(hDevice);
if (!qResult) {
QuickUsbGetLastError(&qusbError);
cout << "QuickUSB Error: " << qusbError << endl;
return qusbError;
}
// Display statistics
timer.Stop();
tElapsed = timer.GetElapsedTimeInSeconds();
myfile.close();
// Exit without error
return 0;
}