summaryrefslogtreecommitdiffstats
path: root/servers/gpib_server_lin/src/companalyzer_functions.cpp
blob: 4eda333c500461553d09edf6c72e03f3163ba0f3 (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
/*
 * 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 - 2015 Timothy Pearson
 * Raptor Engineering
 * http://www.raptorengineeringinc.com
 */

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

#include "companalyzer_functions.h"

extern char falpha[1024];
double companalyzer_raw_trace_data[1024];

unsigned long companalyzerTraceLength (const char * companalyzerType) {
	if (strcmp("HP4191A", companalyzerType) == 0) {
		return 417;
	}
	else {
		return 1;
	}
}

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

	if (strcmp("HP4191A", companalyzerType) == 0) {
		printf("[INFO] Setting date on component analyzer\n\r");
		if (strcmp("HP4191A", companalyzerType) == 0) {
			printf("[INFO] Instrument does not contain a clock, ignoring...\n\r");
			return 0;
		}
		else {
			return 1;
		}
	}
	else {
		return 1;
	}
}

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

	if (strcmp("HP4191A", companalyzerType) == 0) {
		printf("[INFO] Setting time on component analyzer\n\r");
		if (strcmp("HP4191A", companalyzerType) == 0) {
			printf("[INFO] Instrument does not contain a clock, ignoring...\n\r");
			// Since this function is used to detect instrument presence, reset the instrument displays here...
			snprintf(timebuffer, 80, "A1B1");
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Writing: %s\n\r", timebuffer);
			#endif
			if (gpib_write(gpibDevice, timebuffer) == 0) {
				return 0;
			}
			else {
				return 2;
			}
			return 0;
		}
		else {
			return 1;
		}
	}
	else {
		return 1;
	}
}

int companalyzer_lock_local_controls (const char * companalyzerType, int gpibDevice) {
	if (strcmp("HP4191A", companalyzerType) == 0) {
		printf("[INFO] Locking component analyzer local controls\n\r");
		if (strcmp("HP4191A", companalyzerType) == 0) {
			#ifdef ENABLE_EXTRA_DEBUGGING
			printf("[DEBG] Setting REN\n\r");
			#endif
			if (ibsre(gpibDevice, 1) == 0) {
				return 0;
			}
			else {
				return 2;
			}
		}
		else {
			return 1;
		}
	}
	else {
		return 1;
	}
}

int companalyzer_set_measurement_parameters(companalyzer_measurement::companalyzer_measurement_t parameter_a, companalyzer_measurement::companalyzer_measurement_t parameter_b, const char * companalyzerType, int gpibDevice, char * extendedError) {
	char first_parameter[1024];

	if ((strcmp("HP4191A", companalyzerType) == 0)) {
		printf("[INFO] Setting component analyzer measurement parameters to types %d:%d\n\r", parameter_a, parameter_b);
		if (strcmp("HP4191A", companalyzerType) == 0) {
			falpha[0] = 0;
			first_parameter[0] = 0;
			if (parameter_a == companalyzer_measurement::resistance) {
				sprintf(first_parameter, "A4");
			}
			else if (parameter_a == companalyzer_measurement::conductance) {
				sprintf(first_parameter, "A5");
			}
			else if (parameter_a == companalyzer_measurement::inductance) {
				sprintf(first_parameter, "A7");
			}
			else if (parameter_a == companalyzer_measurement::capacitance) {
				sprintf(first_parameter, "A8");
			}
			else if (parameter_a == companalyzer_measurement::impedance) {
				sprintf(first_parameter, "A1");
			}
			else if (parameter_a == companalyzer_measurement::admittance) {
				sprintf(first_parameter, "A2");
			}
			else if (parameter_a == companalyzer_measurement::reflection_absolute) {
				sprintf(first_parameter, "A3");
			}
			else if (parameter_a == companalyzer_measurement::reflection_x) {
				sprintf(first_parameter, "A6");
			}

			if (first_parameter[0] != 0) {
				if (parameter_b == companalyzer_measurement::resistance) {
					sprintf(falpha, "%sB1", first_parameter);
				}
				else if (parameter_b == companalyzer_measurement::conductance) {
					sprintf(falpha, "%sB2", first_parameter);
				}
				else if (parameter_b == companalyzer_measurement::dissipation_factor) {
					sprintf(falpha, "%sB3", first_parameter);
				}
				else if (parameter_b == companalyzer_measurement::quality_factor) {
					sprintf(falpha, "%sB4", first_parameter);
				}
				else if (parameter_b == companalyzer_measurement::phase_angle_deg) {
					sprintf(falpha, "%sB1", first_parameter);
				}
				else if (parameter_b == companalyzer_measurement::phase_angle_rad) {
					sprintf(falpha, "%sB2", first_parameter);
				}
			}
			if (strlen(falpha) == 0) {
				sprintf(extendedError, "EXTAn invalid parameter or combination of parameters was requested°");
				return 2;
			}
			else {
				#ifdef ENABLE_EXTRA_DEBUGGING
				printf("[DEBG] Writing: %s\n\r", falpha);
				#endif
				if (gpib_write(gpibDevice, falpha) == 0) {
					return 0;
				}
				else {
					sprintf(extendedError, "EXTAn unknown communications error has occured!°");
					return 2;
				}
			}
		}
		else {
			sprintf(extendedError, "EXTAn invalid device was selected!°");
			return 1;
		}
	}
	else {
		sprintf(extendedError, "EXTAn invalid device was selected!°");
		return 1;
	}
}

int companalyzer_set_measurement_frequency(double frequency, const char * companalyzerType, int gpibDevice, char * extendedError) {
	if ((strcmp("HP4191A", companalyzerType) == 0)) {
		printf("[INFO] Setting component analyzer measurement frequency to %eMHz\n\r", frequency);
		if (strcmp("HP4191A", companalyzerType) == 0) {
			falpha[0] = 0;
			
			if ((frequency >= 1.0) && (frequency <= 1000.0)) {
				sprintf(falpha, "FR%04.04fEN", frequency);
			}
			if (strlen(falpha) == 0) {
				sprintf(extendedError, "EXTAn invalid parameter or combination of parameters was requested°");
				return 2;
			}
			else {
				#ifdef ENABLE_EXTRA_DEBUGGING
				printf("[DEBG] Writing: %s\n\r", falpha);
				#endif
				if (gpib_write(gpibDevice, falpha) == 0) {
					return 0;
				}
				else {
					sprintf(extendedError, "EXTAn unknown communications error has occured!°");
					return 2;
				}
			}
		}
		else {
			sprintf(extendedError, "EXTAn invalid device was selected!°");
			return 1;
		}
	}
	else {
		sprintf(extendedError, "EXTAn invalid device was selected!°");
		return 1;
	}
}

int companalyzer_get_parameter_measurement(companalyzer_measurements_t * retval, const char * companalyzerType, int gpibDevice) {
	char segarray[1024];
	char meas_a_string[1024];
	char meas_b_string[1024];
	int i;
	long ai;
	long left_char;
	long right_char;
	char* display_data_string;

	int max_num_bytes = 0;
	double parameter_value;
	companalyzer_measurements_t measurement;
	companalyzer_status::companalyzer_status_t parameter_status;
	companalyzer_measurement_type::companalyzer_measurement_type parameter_type;

	if ((strcmp("HP4191A", companalyzerType) == 0)) {
		printf("[INFO] Getting component analyzer measurement\n\r");

		// Read measurement
		max_num_bytes = 512;	// Request more bytes than are possible to ensure no bytes are left behind
		#ifdef ENABLE_EXTRA_DEBUGGING
		printf("[DEBG] Trying to read %i bytes from GPIB device...\n", max_num_bytes);
		#endif

		ibtmo(gpibDevice, T10s);
		ibeos(gpibDevice, 0x0);

		ai = gpib_read_array(gpibDevice, max_num_bytes, segarray);
		if (ai == -1) {
			return 1;
		}
		else {
			if (strcmp("HP4191A", companalyzerType) == 0) {
				segarray[ai] = 0;

				left_char = 1;
				right_char = 0;
				for (right_char=left_char;right_char<ai;right_char++) {
					if (segarray[right_char] == ',') {
						break;
					}
				}
				strncpy(meas_a_string, segarray+left_char, right_char-left_char);
				meas_a_string[right_char-left_char] = 0;

				left_char = right_char + 1;
				for (right_char=left_char;right_char<ai;right_char++) {
					if (segarray[right_char] == '\r') {
						break;
					}
				}
				strncpy(meas_b_string, segarray+left_char, right_char-left_char);
				meas_b_string[right_char-left_char] = 0;

				// Parse display data strings
				for (i = 0; i < 2; i++) {
					if (i == 0) {
						display_data_string = meas_a_string;
					}
					else {
						display_data_string = meas_b_string;
					}

					switch (display_data_string[0]) {
						case 'N':
							parameter_status = companalyzer_status::ok;
							break;
						case 'O':
							parameter_status = companalyzer_status::overflow;
							break;
						case 'D':
							parameter_status = companalyzer_status::invalid;
							break;
						case 'U':
							parameter_status = companalyzer_status::cal_req;
							break;
					}

					if (i == 0) {
						switch (display_data_string[1]) {
							case 'Z':
								measurement.parameter_a = companalyzer_measurement::impedance;
								break;
							case 'Y':
								measurement.parameter_a = companalyzer_measurement::admittance;
								break;
							case 'M':
								measurement.parameter_a = companalyzer_measurement::reflection_absolute;
								break;
							case 'R':
								measurement.parameter_a = companalyzer_measurement::resistance;
								break;
							case 'G':
								measurement.parameter_a = companalyzer_measurement::conductance;
								break;
							case 'X':
								measurement.parameter_a = companalyzer_measurement::reflection_x;
								break;
							case 'L':
								measurement.parameter_a = companalyzer_measurement::inductance;
								break;
							case 'C':
								measurement.parameter_a = companalyzer_measurement::capacitance;
								break;
						}
					}
					else {
						switch (display_data_string[1]) {
							case 'D':
								measurement.parameter_b = companalyzer_measurement::phase_angle_deg;
								break;
							case 'R':
								measurement.parameter_b = companalyzer_measurement::phase_angle_rad;
								break;
							case 'X':
								measurement.parameter_b = companalyzer_measurement::reactance;
								break;
							case 'B':
								measurement.parameter_b = companalyzer_measurement::susceptance;
								break;
							case 'Y':
								measurement.parameter_b = companalyzer_measurement::reflection_y;
								break;
// 							case 'R':
// 								measurement.parameter_b = companalyzer_measurement::resistance;
// 								break;
							case 'G':
								measurement.parameter_b = companalyzer_measurement::conductance;
								break;
// 							case 'D':
// 								measurement.parameter_b = companalyzer_measurement::dissipation_factor;
// 								break;
							case 'Q':
								measurement.parameter_b = companalyzer_measurement::quality_factor;
								break;
						}
					}

					switch (display_data_string[2]) {
						case 'N':
							parameter_type = companalyzer_measurement_type::normal;
							break;
						case 'D':
							parameter_type = companalyzer_measurement_type::deviation;
							break;
						case 'P':
							parameter_type = companalyzer_measurement_type::deviation_percent;
							break;
					}

					parameter_value = atof(display_data_string + 3);

					if (i == 0) {
						measurement.parameter_a_status = parameter_status;
						measurement.parameter_a_type = parameter_type;
						measurement.parameter_a_value = parameter_value;
					}
					else {
						measurement.parameter_b_status = parameter_status;
						measurement.parameter_b_type = parameter_type;
						measurement.parameter_b_value = parameter_value;
					}
				}

				*retval = measurement;
			}
			else {
				return 2;
			}
		}
	
		ibtmo(gpibDevice, T10s);
	
		#ifdef ENABLE_EXTRA_DEBUGGING
		printf("[DEBG] Read %li bytes from GPIB device\n", ai);
		#endif

		return 0;
	}
	else {
		return -1;
	}
}