-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBVTserialInterfacer.c
405 lines (341 loc) · 14.1 KB
/
BVTserialInterfacer.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* A silly command line wrapper to the Bruker BVT3000 NMR Variable Temperature
* Device -- written by Jack J Miller, University of Oxford 2018, but heavily
* "inspired" by the sterling work of fsc2, an open-source spectrometer library
* predominantly aimed at EPR and UV-VIS instruments.
*
* Huge thanks to Jens and the open source movement for letting us use the
* overly expensive hardware we have already paid for...
*
* As an aside, this might be useful as an implementation of the Bi-Synch
* protocol for talking to Eurotherm devices (or equivalent others)
*
* ------
* 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, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include "cmdline.h"
#include "serial_jjm.h"
#include "convenient_wrapper_functions.h"
bool verboseFlag = false;
struct sp_port *port;
int main(int argc, char **argv){
struct gengetopt_args_info ai;
/* ----- Parse arguments ---- */
if (cmdline_parser(argc, argv, &ai) != 0){
return 1;
}
if(ai.list_devices_given) {
list_ports();
return 0; /* quit */
}
/* --- Open device ---*/
port = open_and_init_port(ai.device_arg, port);
if (ai.verbose_given) {
bool *b=&verboseFlag;
++*b;
printf("Port %s opened successfully\n", ai.device_arg);
}
/* --- Send relevant commands, reply with data --- */
//Read temperature
if(ai.read_temperature_given) {
if(verboseFlag){printf("Reading temperature!\n");};
double temp = eurotherm902s_get_temperature(port);
printf("***TEMP: %f\n", temp);
}
//Check for temperature sensor breaks
if(ai.check_sensor_break_given || ai.status_all_given ){
if(verboseFlag){printf("Checking sensor!\n"); }
bool result = eurotherm902s_check_sensor_break(port);
if (result == SET) {
fprintf(stderr,"Warning: PT100/Thermocouple sensor break on device.\n");
fprintf(stderr,"***SBC : FAIL\n");
} else{
printf("***SBC : OK\n");
}
}
//Check heater
if(ai.check_heater_given|| ai.status_all_given){
if(verboseFlag){ printf("Checking heater!\n"); }
int result = bvt3000_check_heater(port);
if (result == HEATER_OVERHEATING ) {
fprintf(stderr,"***HCC : FAIL\n");
if(verboseFlag){printf("Disabling heater...\n"); }
bvt3000_set_heater_state(UNSET,port);
fprintf(stderr,"Heater overheating, heater disabled.\n");
} else if(result == HEATER_OK){
printf("***HCC : OK\n");
} else {
fprintf(stderr, "Error: heater unexpected result");
}
}
//Get heater state
if(ai.get_heater_state_given|| ai.status_all_given) {
checkHeater(port);
}
// Enable heater
if(ai.heater_on_given) {
if(verboseFlag){printf("Enabling heater!\n"); }
bvt3000_set_heater_state(SET, port);
checkHeater(port);
}
// Disable heater
if(ai.heater_off_given) {
if(verboseFlag){printf("Disabling heater!\n"); }
bvt3000_set_heater_state(UNSET, port);
checkHeater(port);
}
// Get heater power limit
if(ai.get_heater_power_limit_given) {
if(verboseFlag){printf("Getting heater power limit!\n"); }
double result = eurotherm902s_get_heater_power_limit(port);
printf("***HPWL: %lf\n", result);
}
// Set heater power limit
if(ai.set_heater_power_limit_given) {
if(verboseFlag){printf("Setting heater power limit!\n"); }
setHeaterPowerLimit((double) ai.set_heater_power_limit_arg, port);
}
//Set heater power as a percentage
if(ai.set_heater_power_given) {
double power = (double) ai.set_heater_power_arg;
if ((power < 0.0 ) || (power > 100.0) ){
fprintf(stderr,"FATAL: Heater power not in range 0-100 [percent]\n");
} else {
if(verboseFlag){printf("Setting heater power to %lf!\n", power); }
eurotherm902s_set_heater_power(power, port);
}
}
//Get heater power
if ( ai.get_heater_power_given) {
if(verboseFlag) { printf("Getting heater power as a percentage!\n"); }
double result = eurotherm902s_get_heater_power(port);
printf("***HPWR: %lf\n",result);
}
//Enable automatic PID control
if(ai.enable_PID_control_given) {
if(verboseFlag) { printf("Enabling PID control!\n"); }
if(! ai.manual_mode_given) {
eurotherm902s_set_mode(AUTOMATIC_MODE, port);
} else {
fprintf(stderr,"FATAL: Both manual and automatic mode requested\n");
}
}
//Enable manual mode (the heater power can be controlled by keys on the device, and also by software)
if(ai.manual_mode_given) {
if(verboseFlag) { printf("Enabling manual control!\n"); }
if(! ai.enable_PID_control_given) {
eurotherm902s_set_mode(MANUAL_MODE, port );
} else {
fprintf(stderr,"FATAL: Both manual and automatic mode requested\n");
}
}
//Ask the device about its current mode
if(ai.get_mode_given|| ai.status_all_given) {
if(verboseFlag) { printf("Getting current PID mode!\n"); }
int result = eurotherm902s_get_mode(port) ;
if (result == AUTOMATIC_MODE) {
printf("***PIDM: AUTO\n");
} else if (result == MANUAL_MODE) {
printf("***PIDM: MANUAL\n");
}
}
//Get gas flow rate
if(ai.get_gas_flow_rate_given) {
if(verboseFlag){printf("Getting gas flow rate!\n"); }
unsigned int gfr = bvt3000_get_flow_rate(port);
assert (gfr <= 15) ; // For the 4-valve block gas flow device at any rate -- you might need to change this.
double result = translate_flow_rate(gfr);
printf("***GASR: %lf\n", result);
}
//Set gas flow rate
if(ai.set_gas_flow_rate_given){
if(verboseFlag) { printf("Requested change to gas flow rate to %f l/hr!\n",
ai.set_gas_flow_rate_arg); }
if (verboseFlag) {printf("Checking heater status...\n");}
bool result = bvt3000_get_heater_state(port);
if (result == SET) {
if (verboseFlag) {printf("Setting flow rate...\n");}
set_flow_rate((double) ai.set_gas_flow_rate_arg, port);
if (verboseFlag) {printf("Checking flow rate...\n");}
unsigned int gfr = bvt3000_get_flow_rate(port);
if (verboseFlag) {printf("Actual flow rate %u...\n", gfr);}
} else if (result == UNSET) {
fprintf(stderr,"FATAL: Cannot change gas flow rate with heater off\n");
}
}
//Get temperature setpoint
if(ai.get_temperature_setpoint_given) {
if(verboseFlag) { printf("Getting temperature setpoint!\n"); }
double tpsp = eurotherm902s_get_setpoint( SP1, port );
printf("***TSP : %lf\n", tpsp);
}
//Set temperature setpoint
if(ai.set_temperature_setpoint_given) {
if(verboseFlag) {printf("Setting temperature setpoint SP1 to %f!\n", ai.set_temperature_setpoint_arg); }
double temp = (double) ai.set_temperature_setpoint_arg;
eurotherm902s_set_setpoint(SP1, temp, port);
}
//Get Eurotherm temperature box status
if(ai.get_eurotherm_status_given|| ai.status_all_given) {
if(verboseFlag) { printf("Getting Eurotherm status!\n"); }
bool result = eurotherm902s_get_alarm_state( port );
if(result == SET) {
fprintf(stderr,"***EALM: ON\n");
if(verboseFlag) { printf("Eurotherm is alarming!\n"); }
} else if (result == UNSET) {
printf("***EALM: OFF\n");
}
}
//Lock or unlock keypad
if(ai.lock_keypad_given) {
if(verboseFlag) { printf("Keyboard lock/unlock state change requested!\n"); }
if (ai.lock_keypad_arg == 1) {
if(verboseFlag){printf("Unlocking requested....\n"); }
eurotherm902s_lock_keyboard((bool) ai.lock_keypad_arg, port);
} else {
if(verboseFlag){printf("Locking requested....\n"); }
eurotherm902s_lock_keyboard((bool) ai.lock_keypad_arg, port);
}
}
//Get PID -- P
if(ai.get_proportional_band_given) {
if (verboseFlag) {printf("Getting proportional band...\n");}
double result = eurotherm902s_get_proportional_band(port);
printf("***PPID: %lf\n", result);
}
//Get PID -- I
if(ai.get_integral_time_given) {
if (verboseFlag) {printf("Getting integral time...\n");}
double result = eurotherm902s_get_integral_time(port);
printf("***IPID: %lf\n", result);
}
//Get PID -- D
if(ai.get_differential_time_given) {
if (verboseFlag) {printf("Getting derivative time...\n");}
double result = eurotherm902s_get_derivative_time(port);
printf("***DPID: %lf\n", result);
}
//Set PID -- P
if(ai.set_proportional_band_given) {
float setValue = ai.set_proportional_band_arg;
if (verboseFlag) {printf("Setting proportional band to %f...\n", setValue);}
eurotherm902s_set_proportional_band(setValue, port);
}
//Set PID -- I
if(ai.set_integral_time_given) {
float setValue = ai.set_integral_time_arg;
if (verboseFlag) {printf("Setting integral time to %f...\n", setValue);}
eurotherm902s_set_integral_time(setValue, port);
}
//Get PID -- D
if(ai.set_differential_time_given) {
float setValue = ai.set_differential_time_arg;
if (verboseFlag) {printf("Setting derivative time to %f...\n", setValue);}
eurotherm902s_set_derivative_time(setValue, port);
}
//LN2 methods -- get Ln2 heater state
if(ai.get_ln2_heater_state_given) {
if(verboseFlag) {printf("Getting LN2 heater state!\n");}
bool result = bvt3000_get_ln2_heater_state(port);
if (result == SET){
printf("***N2HE: ON\n");
} else if (result == UNSET) {
printf("***N2HE: OFF\n");
}
}
//Set heater state
if(ai.set_ln2_heater_state_given) {
if(verboseFlag) {printf("Changing state of ln2 LN2 heater to %u!\n", (bool)ai.set_ln2_heater_state_arg); }
bvt3000_set_ln2_heater_state((bool)ai.set_ln2_heater_state_arg, port);
}
//Get LN2 heater power
if(ai.get_ln2_heater_power_given) {
if(verboseFlag) {printf("Getting LN2 heater power!\n");}
double result = bvt3000_get_ln2_heater_power(port);
printf("***N2HP: %lf\n", result);
}
//Set LN2 heater power
if(ai.set_ln2_heater_power_given) {
float gvnPwr = ai.set_ln2_heater_power_arg;
if( (gvnPwr < 0 ) || (gvnPwr > 100)) {
fprintf(stderr,"FATAL: Supplied heater power %f out of range [0, 100]\n", gvnPwr);
} else {
if(verboseFlag) { printf("Setting LN2 heater power to %f!\n", gvnPwr); }
bvt3000_set_ln2_heater_power(gvnPwr, port);
}
}
//Check LN2 tank
if(ai.check_ln2_heater_given) {
if(verboseFlag) { printf("Checking LN2 tank!\n"); }
int result = bvt3000_check_ln2_heater(port);
if(result == LN2_OK) {
printf("***N2TK: OK\n");
} else if (result == LN2_NEEDS_REFILL ) {
printf("***N2TK: FILL_ME\n");
} else if (result == LN2_TANK_EMPTY) {
printf("***N2TK: EMPTY\n");
}
}
//Get high cutback value
if(ai.get_high_cutback_given){
if(verboseFlag) {printf("Getting high cutback!\n"); }
double result = eurotherm902s_get_cutback_high(port);
printf("***HCUT: %lf\n", result);
}
//Set high cutback value
if(ai.set_high_cutback_given){
if(verboseFlag) {printf("Setting high cutback to %lf!\n", ai.set_high_cutback_arg); }
eurotherm902s_set_cutback_high((double) ai.set_high_cutback_arg, port);
}
//Get low cutback value
if(ai.get_low_cutback_given){
if(verboseFlag) {printf("Getting low cutback!\n"); }
double result = eurotherm902s_get_cutback_low(port);
printf("***LCUT: %lf\n", result);
}
//Set low cutback value
if(ai.set_low_cutback_given){
if(verboseFlag) {printf("Setting low cutback to %lf!\n", ai.set_low_cutback_arg); }
eurotherm902s_set_cutback_low((double) ai.set_low_cutback_arg, port);
}
//Get adaptive tune value in K
if(ai.get_adaptive_tune_level_given){
if(verboseFlag) { printf("Getting adaptive tune!\n"); }
double result = eurotherm902s_get_adaptive_tune_trigger(port);
printf("***ADTR: %lf\n", result);
}
//Set adaptive tune value in K
if(ai.set_adaptive_tune_level_given){
if(verboseFlag) {printf("Setting adaptive tune to %lf!\n", ai.set_adaptive_tune_level_arg); }
eurotherm902s_set_adaptive_tune_trigger((double) ai.set_adaptive_tune_level_arg, port);
}
/*------------------------------------------------------------------------*/
/* --- Close device --- */
enum sp_return anyErrors = sp_close(port);
if (ai.verbose_given && (anyErrors == SP_OK) ) {
printf("Port %s closed successfully\n", ai.device_arg);
} else if (anyErrors != SP_OK) {
fprintf(stderr,"Error closing port %s, return code %d\n", ai.device_arg, anyErrors);
}
return 0;
}