-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleMeas.c
381 lines (314 loc) · 14.2 KB
/
SingleMeas.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/****************************************************************************
Thorlabs TXP PAX57xx single measurement demo application
This demo program connects to an instrument, configures it and takes
single measurment results in a loop. Finally it closes the connection.
Source file 'SingleMeas.c'
Date: Jul-22-2005
Software-Nr: N/A
Version: 1.1
Copyright: Copyright(c) 2005, Thorlabs GmbH (www.thorlabs.com)
Author: Michael Biebl (mbiebl@thorlabs.com)
Changelog: Mar-01-2005 -> V1.0
Jul-22-2005 -> adjustable wavelength -> V1.1
Disclaimer:
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
****************************************************************************/
#include <stdio.h>
#include <signal.h> //signals for break handler
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <string.h>
#include "TXP_DRV_PAX.h"
#include <windows.h> // for millisecond counter
//using namespace System;
/*---------------------------------------------------------------------------
Macros
---------------------------------------------------------------------------*/
//define type bool
#define false 0
#define true 1
#define DEF_IP_ADDR "localhost" // instrument IP address or hostname ('localhost' for own PC)
#define DEF_IP_PORT 2402 // TXP standard IP port number
#define DEF_SLOT 2 // zero based slot number
#define DEF_COMM_TIMEOUT 30000 // 10sec communication timeout
#define DEF_BOOT_TIMEOUT 30000 // wait for card to finish booting (max 10sec)
#define DEF_WAVELENGTH 1550 // operating wavelength 1550.0nm
#define DEF_WRITE_TO_FILE 0 // write to file or not. 0 = false, 1 = true
#define DEF_FILE_NAME "output.txt" // instrument IP address or hostname ('localhost' for own PC)
#define DEF_BASIC_MEAS_SPEED 200.0 // measuremnet speed (2* motor speed) per second
#define DEF_PERIODS_PER_BUFFER 2 // take 2 half revolutions per measuremnt buffer
#define DEF_BUFFERS_PER_FFT 5 // take 10 buffers per transformation
#define DEF_FFT_PER_RESULT 2 // average 2 transformations per result
/*---------------------------------------------------------------------------
Global Variables
---------------------------------------------------------------------------*/
int write_to_file = DEF_WRITE_TO_FILE;
FILE * pFile;
/*---------------------------------------------------------------------------
Prototypes
---------------------------------------------------------------------------*/
void INThandler(int);
void errorMessage(ViSession, ViStatus);
void statusMessage(ViUInt32);
void waitKeypress(void);
void delay1sec(void);
void usage(char*);
//declarations
/*---------------------------------------------------------------------------
Main
---------------------------------------------------------------------------*/
int main(int argc, char** argv)
{
time_t timer;
LARGE_INTEGER StartingTime, EndingTime, ElapsedMilliseconds;
LARGE_INTEGER Frequency; //frequency of timer
char ip_addr[60] = DEF_IP_ADDR;
char file_name[60] = DEF_FILE_NAME;
unsigned short ip_port = DEF_IP_PORT;
unsigned int slot = DEF_SLOT;
unsigned int comm_tmo = DEF_COMM_TIMEOUT;
unsigned int boot_tmo = DEF_BOOT_TIMEOUT;
unsigned int wavelength = DEF_WAVELENGTH;
unsigned int BASIC_MEAS_SPEED = DEF_BASIC_MEAS_SPEED; // measuremnet speed (x* motor speed) per second
unsigned int PERIODS_PER_BUFFER = DEF_PERIODS_PER_BUFFER; // take x half revolutions per measuremnt buffer
unsigned int BUFFERS_PER_FFT = DEF_BUFFERS_PER_FFT; // take x buffers per transformation
unsigned int FFT_PER_RESULT = DEF_FFT_PER_RESULT; // average x transformations per result
char resName[80]; // buffer to build resource name
ViSession instr; // instruenmt handle
ViUInt32 stat; // instrument status variable
ViStatus err; // error number
int i; // loop counter
double st1, st2, st3, azi, ell, dop, pow;
// take in arguments
if(argc > 1) strncpy(ip_addr, argv[1], sizeof(ip_addr));
if(argc > 2) ip_port = (unsigned short)atoi(argv[2]);
if(argc > 3) slot = (unsigned int)atoi(argv[3]);
if(argc > 4) wavelength = (unsigned int)atoi(argv[4]);
if(argc > 5) comm_tmo = (unsigned int)atoi(argv[5]);
if(argc > 6) boot_tmo = (unsigned int)atoi(argv[6]);
if(argc > 7 && strcmp(argv[7],"true") == 0) write_to_file = 1;
if(argc > 8) strncpy(file_name, argv[8], sizeof(file_name));
if(argc > 9) BASIC_MEAS_SPEED = (unsigned int)atoi(argv[9]);
if(argc > 10) PERIODS_PER_BUFFER = (unsigned int)atoi(argv[10]);
if(argc > 11) BUFFERS_PER_FFT = (unsigned int)atoi(argv[11]);
if(argc > 12) FFT_PER_RESULT = (unsigned int)atoi(argv[12]);
//dissable buffer - for low latency
setbuf(stdout, NULL);
//initiate break handler
signal(SIGINT, INThandler);
//open file for write
if(write_to_file)
pFile = fopen (file_name,"a+"); //open file for write
// print title
printf("\nThorlabs TXP PAX Single Measurement Sample Application\n");
printf("------------------------------------------------------\n\n");
if(write_to_file)
printf("Output file: %s \n", file_name);
// connect to instrument
sprintf(resName, "TCPIP::%s::%u::SOCKET", ip_addr, ip_port); // build the resource name
printf("Connecting to instrument: \"%s\" Slot %u ...\n", resName, slot);
err = TXPPAX_init (resName, slot, VI_NULL, comm_tmo, boot_tmo, 0, &instr);
if(err)
{
errorMessage(VI_NULL, err);
usage(argv[0]);
waitKeypress();
return 1;
}
// configure measuremnt - motor speed, measurement parameters
printf("Configuring instrument (wavelength: %u nm) ...\n", wavelength);
err = TXPPAX_SetOperatingMode (instr, TXPPAX_OPMODE_SINGLE_MEAS);
if(!err) err = TXPPAX_SetWavelength (instr, (wavelength * 1.0E-9));
if(!err) err = TXPPAX_SetPowerRange (instr, TXPPAX_RANGE_AUTO); // autorange
if(!err) err = TXPPAX_SetBasicMeasSpeed (instr, BASIC_MEAS_SPEED);
if(!err) err = TXPPAX_SetSingleMeasParameters (instr, PERIODS_PER_BUFFER, BUFFERS_PER_FFT, FFT_PER_RESULT);
if(err)
{
errorMessage(instr, err);
TXPPAX_close(instr);
waitKeypress();
return 1;
}
// check instrument status and break on status we can't measure - loop until motor has stabilized
printf("Checking status - waiting for motor to stabilize ...\n");
delay1sec();
do
{
err = TXPPAX_GetStatus (instr, VI_NULL, &stat); // we don't take care of the event register value
stat &= ( TXPPAX_STATBIT_SPEED | // ignore all other status bits
TXPPAX_STATBIT_OVERTEMP |
TXPPAX_STATBIT_VCC_FAIL |
TXPPAX_STATBIT_SENSOR_MISSING |
TXPPAX_STATBIT_SENSOR_TEMP |
TXPPAX_STATBIT_SENSOR_ADJ |
TXPPAX_STATBIT_CARD_ADJ );
}
while (!err && (stat == TXPPAX_STATBIT_SPEED));
if(err)
{
errorMessage(instr, err);
TXPPAX_close(instr);
waitKeypress();
return 1;
}
if(stat)
{
statusMessage(stat &= ~TXPPAX_STATBIT_SPEED); // print status message excluding motor speed failure
TXPPAX_close(instr);
waitKeypress();
return 1;
}
// print header
printf("\n");
printf(" Stokes1 Stokes2 Stokes3 Azimuth Ellipt DOP Power [W] Time [ms]\n");
printf("-------- -------- -------- -------- -------- -------- ----------- ------------\n");
err = TXPPAX_RestartSingleMeas (instr); // Clear measurement buffers
i = 0;
//Start Counter
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartingTime);
//Start measurement loop - print out results
while(!err)
{
err = TXPPAX_GetSingleMeas (instr, VI_TRUE, &st1, &st2, &st3, &azi, &ell, &dop, &pow, VI_NULL);
//if(!err) printf("%+8.5lf %+8.5lf %+8.5lf %+8.3lf %+8.3lf %8.4lf %11.3le\n", st1, st2, st3, azi, ell, dop, pow);
time(&timer);// get current time; same as: timer = time(NULL)
//Get current ms count
QueryPerformanceCounter(&EndingTime);
ElapsedMilliseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMilliseconds.QuadPart *= 1000;
ElapsedMilliseconds.QuadPart /= Frequency.QuadPart;
//printf("milliseconds are %d",ElapsedMilliseconds.QuadPart);
//print to screen
if(!err) printf("%.5lf\t%.5lf\t%.5lf\t%.5lf\t%.5lf\t%.5lf\t%.10lf\t%d\n", st1, st2, st3, azi, ell, dop, pow, ElapsedMilliseconds.QuadPart);
// Will now print everything in the stdout buffer -
//fflush(stdout);
//print to file
if(write_to_file)
if(!err) fprintf(pFile, "%.5lf\t%.5lf\t%.5lf\t%.5lf\t%.5lf\t%.5lf\t%.10lf\t%d\n", st1, st2, st3, azi, ell, dop, pow, ElapsedMilliseconds.QuadPart);
i++;
}
if(err)
{
errorMessage(instr, err);
TXPPAX_close(instr);
waitKeypress();
return 1;
}
printf("\n");
// done - close connection
printf("Closing connection ...\n\n");
TXPPAX_close(instr);
// Close file
if(write_to_file)
fclose (pFile);
waitKeypress();
return 0;
}
///*---------------------------------------------------------------------------
// Break handler - verify if user want's to break - close output file if open
//---------------------------------------------------------------------------*/
void INThandler(int sig)
{
if(write_to_file){
printf("Closing output file...");
fclose(pFile);
printf("done.\n");
}
exit(0);
//char c;
//signal(sig, SIG_IGN);
//printf("OUCH, did you hit Ctrl-C?\n"
// "Do you really want to quit? [y/n] ");
//c = getchar();
//if (c == 'y' || c == 'Y')
// exit(0);
//else
// signal(SIGINT, INThandler);
//getchar(); // Get new line character
}
/*---------------------------------------------------------------------------
Error message - Query the message from the driver and print it to 'stderr'
---------------------------------------------------------------------------*/
void errorMessage(ViSession instr, ViStatus err)
{
char buf[TXPPAX_ERR_DESCR_BUFFER_SIZE];
TXPPAX_errorMessage(instr, err, buf);
fprintf(stderr, "Error: %s\n\n", buf);
}
/*---------------------------------------------------------------------------
Print instrument status description - Print readable text ot status value
---------------------------------------------------------------------------*/
void statusMessage(ViUInt32 stat)
{
printf("Status: ");
if(stat & TXPPAX_STATBIT_CARD_EVENT) printf("Card event occured\t");
if(stat & TXPPAX_STATBIT_OVERTEMP) printf("Overtemperature\t");
if(stat & TXPPAX_STATBIT_COOLING) printf("Cooling required\t");
if(stat & TXPPAX_STATBIT_VCC_FAIL) printf("Power supply failure\t");
if(stat & TXPPAX_STATBIT_ALARM) printf("Chassis alarm active\t");
if(stat & TXPPAX_STATBIT_CARD_PRESENT) printf("Card is present\t");
if(stat & TXPPAX_STATBIT_CARD_READY) printf("Card is ready\t");
if(stat & TXPPAX_STATBIT_GPIO) printf("GPIO event occured\t");
if(stat & TXPPAX_STATBIT_SENSOR_MISSING) printf("Sensor missing\t");
if(stat & TXPPAX_STATBIT_MACRO_ACTIVE) printf("Macro active\t");
if(stat & TXPPAX_STATBIT_TRIGGER_WAIT) printf("Waiting for trigger\t");
if(stat & TXPPAX_STATBIT_POWER_RANGE) printf("Input power out of range\t");
if(stat & TXPPAX_STATBIT_SENSOR_TEMP) printf("Sensor temperature failure\t");
if(stat & TXPPAX_STATBIT_SPEED) printf("Motor not stabilized\t");
if(stat & TXPPAX_STATBIT_SENSOR_ADJ) printf("Sensor adjustment failure\t");
if(stat & TXPPAX_STATBIT_CARD_ADJ) printf("Card adjustment failure\t");
printf("\n\n");
}
/*---------------------------------------------------------------------------
Print keypress message and wait
---------------------------------------------------------------------------*/
void waitKeypress(void)
{
printf("Press <ENTER> to exit\n");
while(getchar() == EOF);
}
/*---------------------------------------------------------------------------
Print usage
---------------------------------------------------------------------------*/
void usage(char* name)
{
printf("\nUsage:\n");
printf("%s [attributes - ordered as below] \n\n", name);
printf(" IP-address IP address or hostname (default: " DEF_IP_ADDR ")\n\n");
printf(" IP-port IP port number (default: %u)\n\n", DEF_IP_PORT);
printf(" slot zero based slot number 0..63 (default: %u)\n\n", DEF_SLOT);
printf(" wavelength wavelength [nm] (default: %u)\n\n", DEF_WAVELENGTH);
printf(" comm-tmo communication timeout [ms] (default: %u)\n\n", DEF_COMM_TIMEOUT);
printf(" boot-tmo card boot timeout [ms] (default: %u)\n\n", DEF_BOOT_TIMEOUT);
printf(" write-to-file write output to file (default: %u)\n\n", DEF_WRITE_TO_FILE);
printf(" file-name filename for output (default: " DEF_FILE_NAME ")\n\n");
printf(" Meas. speed measuremnet speed ( X * motor speed ) per second (default: %u)\n\n", DEF_BASIC_MEAS_SPEED);
printf(" periods number of measurements per buffer\n take X half revolutions per measurement buffer (default: %u)\n\n", DEF_PERIODS_PER_BUFFER);
printf(" buffers buffers per FFT\n take X buffers per transformation (default: %u)\n\n", DEF_BUFFERS_PER_FFT);
printf(" FFT's FFT's per result\n average X transformations per result (default: %u)\n\n", DEF_FFT_PER_RESULT);
printf("\n");
}
/*---------------------------------------------------------------------------
Delay ~1sec
---------------------------------------------------------------------------*/
void delay1sec(void)
{
time_t timestamp;
timestamp = time(NULL);
while(time(NULL) == timestamp);
}
/****************************************************************************
End of sourc file
****************************************************************************/