summaryrefslogtreecommitdiffstats
path: root/servers/gpib_server_lin/src/commanalyzer_functions.c
blob: 7ad705ee5fe5c64011949066357c1d6080d38a4a (plain)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
 * Remote Laboratory Instrumentation Server
 *
 * 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 3 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (c) 2009 Timothy Pearson
 * Raptor Engineering
 * http://www.raptorengineeringinc.com
 */

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <allegro.h>
#include "parameters.h"
#include "gpib_functions.h"
#include "gpib/ib.h"

char falpha[1024];
double commanalyzer_raw_trace_data[1024];

unsigned long commanalyzerTraceLength (char * commanalyzerType) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		return 417;
	}
	else {
		return 1;
	}
}

int commanalyzer_set_date(struct tm * datetime, char * commanalyzerType, int gpibDevice) {
	char datebuffer [80];
	strftime(datebuffer,80,"CONF:DATE %m%d%y",datetime);

	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting date on communications analyzer\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", datebuffer);
			#endif
			if (gpib_write(gpibDevice, datebuffer) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_time(struct tm * datetime, char * commanalyzerType, int gpibDevice) {
	char timebuffer [80];
	strftime(timebuffer,80,"CONF:TIME +%H.%M",datetime);	// FIXME wrong format

	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting time on communications analyzer\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", timebuffer);
			#endif
			if (gpib_write(gpibDevice, timebuffer) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_switch_to_spectrum_analyzer_mode(char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting communications analyzer to spectrum anayzer mode\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"DISP SAN");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_lock_screen (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Locking communications analyzer screen\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SPEC:DISP 'LOCKED'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_spectrum_analyzer_set_generator_mode_tracking (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting spectrum analyzer generator to tracking mode\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:RFG 'TRACK'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_spectrum_analyzer_set_generator_mode_fixed (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting spectrum analyzer generator to fixed mode\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:RFG 'FIXED'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_spectrum_analyzer_set_rf_input_dedicated (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting spectrum analyzer RF input to dedicated connector\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:INP 'ANT'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_spectrum_analyzer_set_rf_input_muxed (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting spectrum analyzer RF input to multiplexed connector\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:INP 'RF IN'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_spectrum_analyzer_set_generator_output_dedicated (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting spectrum analyzer generator output to dedicated connector\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:TGEN:DEST 'DUPL'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_spectrum_analyzer_set_generator_output_muxed (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting spectrum analyzer generator output to multiplexed connector\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:TGEN:DEST 'RF OUT'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_get_spectrum_analyzer_trace(char * commanalyzerType, int gpibDevice) {
	int max_num_bytes = 0;

	unsigned char segarray[4194304];
	unsigned char floatstring[1024];
	long array_pointer;
	long ai;
	long left_char;
	long right_char;

	int x;
	int y;

	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		// Send request
		printf("[INFO] Getting spectrum analyzer trace [Stage 1]\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"MEAS:SAN:TRACE?");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				max_num_bytes = (commanalyzerTraceLength(commanalyzerType)*24);	// Request more bytes than are possible to ensure no bytes are left behind
			}
			else {
				return 2;
			}
		}

		// Read response
		#ifdef ENABLE_EXTRA_DEBUGGING
		printf("[DEBG] Trying to read %i bytes from GPIB device...\n", max_num_bytes);
		#endif
	
		ibtmo(gpibDevice, T30s);
		ibeos(gpibDevice, 0x0);
	
		ai = gpib_read_array(gpibDevice, max_num_bytes, segarray);
		if (ai == -1) {
			return 1;
		}
		else {
			if (strcmp("HP8924C", commanalyzerType) == 0) {
				left_char = 0;
				right_char = 0;
				array_pointer = 0;
				while (left_char < ai) {
					for (right_char=left_char;right_char<ai;right_char++) {
						if (segarray[right_char] == ',') {
							break;
						}
					}
					strncpy(floatstring, segarray+left_char, right_char-left_char);
					floatstring[right_char-left_char] = 0;
					commanalyzer_raw_trace_data[array_pointer] = atof(floatstring);
					array_pointer++;
					left_char = right_char+1;
				}
			}
			else {
				return 2;
			}
		}
	
		ibtmo(gpibDevice, T10s);
	
		#ifdef ENABLE_EXTRA_DEBUGGING
		printf("[DEBG] Read %li bytes from GPIB device\n", array_pointer);
		#endif

		return 0;
	}
}

int commanalyzer_set_spectrum_analyzer_center_frequency(float desired_frequency, char * commanalyzerType, int gpibDevice) {
	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		printf("[INFO] Setting spectrum analyzer center frequency to %f\n\r", desired_frequency);
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:CFR %E", desired_frequency);
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_frequency_span(float desired_frequency, char * commanalyzerType, int gpibDevice) {
	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		printf("[INFO] Setting spectrum analyzer span frequency to %f\n\r", desired_frequency);
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:SPAN %E", desired_frequency);
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_input_attenuation(float desired_attenuation, char * commanalyzerType, int gpibDevice) {
	int instrument_att;

	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			// Only three attenuation values are valid
			if (desired_attenuation < 10) {
				instrument_att = 0;
			}
			else if (desired_attenuation < 30) {
				instrument_att = 20;
			}
			else {
				instrument_att = 40;
			}
		}
		printf("[INFO] Setting spectrum analyzer attenuation to %d dB\n\r", instrument_att);
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:ATT '%d DB'", instrument_att);
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_scale(float desired_scale, char * commanalyzerType, int gpibDevice) {
	int instrument_scale;

	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			// Only three scale values are valid
			if (desired_scale < 1.5) {
				instrument_scale = 1;
			}
			else if (desired_scale < 6) {
				instrument_scale = 2;
			}
			else {
				instrument_scale = 10;
			}
		}
		printf("[INFO] Setting spectrum analyzer scale to %d dB/div\n\r", instrument_scale);
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:DISP:SCAL '%d DB/DIV'", instrument_scale);
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_input_attenuator_mode_auto( char * commanalyzerType, int gpibDevice) {
	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		printf("[INFO] Setting spectrum analyzer attenuation mode to automatic\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:ATT:MODE 'AUTO'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_input_attenuator_mode_fixed( char * commanalyzerType, int gpibDevice) {
	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		printf("[INFO] Setting spectrum analyzer attenuation mode to fixed\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:ATT:MODE 'HOLD'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_generator_power(float desired_power, char * commanalyzerType, int gpibDevice) {
	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		printf("[INFO] Setting spectrum analyzer generator power to %f\n\r", desired_power);
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:TGEN:AMPL %E", desired_power);
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_generator_frequency(float desired_frequency, char * commanalyzerType, int gpibDevice) {
	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		printf("[INFO] Setting spectrum analyzer generator frequency to %f\n\r", desired_frequency);
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:TGEN:OFR %E", desired_frequency);
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_spectrum_analyzer_set_generator_sweep_ascending (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting spectrum analyzer generator output sweep to ascending\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:TGEN:SWE 'NORM'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_spectrum_analyzer_set_generator_sweep_descending (char * commanalyzerType, int gpibDevice) {
	if (strcmp("HP8924C", commanalyzerType) == 0) {
		printf("[INFO] Setting spectrum analyzer generator output sweep to descending\n\r");
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:TGEN:SWE 'INVERT'");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_trace_averaging(float desired_avg_samples, char * commanalyzerType, int gpibDevice) {
	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		int instrument_samples;
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			// Only certain values are valid, and I am interpreting 0 as no pk/avg and -1 as peak hold
			instrument_samples = desired_avg_samples;
			if (desired_avg_samples > 5) {
				if (desired_avg_samples < 5) {
					instrument_samples = desired_avg_samples;
				}
				else if (desired_avg_samples < 7) {
					instrument_samples = 5;
				}
				else if (desired_avg_samples < 15) {
					instrument_samples = 10;
				}
				else if (desired_avg_samples < 35) {
					instrument_samples = 20;
				}
				else if (desired_avg_samples < 75) {
					instrument_samples = 50;
				}
				else {
					instrument_samples = 100;
				}
			}
		}
		printf("[INFO] Setting spectrum analyzer generator trace averaging to %d\n\r", instrument_samples);
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			if (instrument_samples > 0) {
				sprintf(falpha,"SAN:TRAC:MHOL 'AVG %d'", instrument_samples);
			}
			else if (instrument_samples == -1) {
				sprintf(falpha,"SAN:TRAC:MHOL 'PK HOLD'");
			}
			else {
				sprintf(falpha,"SAN:TRAC:MHOL 'NO PK/AVG'");
			}
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}

int commanalyzer_set_spectrum_analyzer_reference_power_level(float desired_reflevel, char * commanalyzerType, int gpibDevice) {
	if ((strcmp("HP8924C", commanalyzerType) == 0)) {
		printf("[INFO] Setting spectrum analyzer generator reference level to %f dBm\n\r", desired_reflevel);
		if (strcmp("HP8924C", commanalyzerType) == 0) {
			sprintf(falpha,"SAN:RLEV %E", desired_reflevel);
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", falpha);
			#endif
			if (gpib_write(gpibDevice, falpha) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
	}
	else {
		return 1;
	}
}