-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcrun.c
304 lines (271 loc) · 10 KB
/
lcrun.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
/*
This file is part of the LCONFIG laboratory configuration system.
LCONFIG 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.
LCONFIG 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 LCONFIG. If not, see <https://www.gnu.org/licenses/>.
Authored by C.Martin crm28@psu.edu
*/
#include "lctools.h"
#include "lconfig.h"
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h> // For forming file names from timestamps
#define CONFIG_FILE "lcrun.conf"
#define MAXLOOP "-1"
#define NBUFFER 65535
#define MAX_DEV 8
#define MAXSTR 128
#define halt(){\
for(devnum=0; devnum<ndev; devnum++){\
lc_stream_stop(&dconf[devnum]);\
lc_stream_clean(&dconf[devnum]);\
lc_close(&dconf[devnum]);\
if(dfile[devnum]){fclose(dfile[devnum]);dfile[devnum]=NULL;}\
}\
};
/*....................
. Help text
.....................*/
const char help_text[] = \
"lcrun [-h] [-r PREFILE] [-p POSTFILE] [-d DATAFILE] [-c CONFIGFILE] \n"\
" [-f|i|s param=value]\n"\
" Runs a data acquisition job until the user exists with a keystroke.\n"\
"\n"\
"-c CONFIGFILE\n"\
" By default, LCRUN will look for \"lcrun.conf\" in the working\n"\
" directory. This should be an LCONFIG configuration file for the\n"\
" LabJackT7 containing no more than three device configurations.\n"\
" The -c option overrides that default.\n"\
" $ lcrun -c myconfig.conf\n"\
"\n"\
"-d DATAFILE\n"\
" This option overrides the default continuous data file name\n"\
" \"YYYYMMDDHHmmSS_lcrun.dat\"\n"\
" $ lcrun -d mydatafile\n"\
" For configurations with only one device, a .dat is automatically\n"\
" appended. For configurations with multiple devices, a data file\n"\
" is created for each device, mydatafile_#.dat"\
"\n"\
"-n MAXREAD\n"\
" This option accepts an integer number of read operations after which\n"\
" the data collection will be halted. The number of samples collected\n"\
" in each read operation is determined by the NSAMPLE parameter in the\n"\
" configuration file. The maximum number of samples allowed per channel\n"\
" will be MAXREAD*NSAMPLE. By default, the MAXREAD option is disabled.\n"\
"\n"\
"-f param=value\n"\
"-i param=value\n"\
"-s param=value\n"\
" These flags signal the creation of a meta parameter at the command\n"\
" line. f,i, and s signal the creation of a float, integer, or string\n"\
" meta parameter that will be written to the data file header.\n"\
" $ lcrun -f height=5.25 -i temperature=22 -s day=Monday\n"\
"\n"\
"GPLv3\n"\
"(c)2017-2022 C.Martin\n";
/*....................
. Main
.....................*/
int main(int argc, char *argv[]){
int ndev; // actual number of devices
// NDEV is the maximum
int devnum;
char go; // Flag for whether to continue the stream loop
char param[MAXSTR];
// Options
char data_file_base[MAXSTR],
data_file[MAXSTR],
config_file[MAXSTR] = CONFIG_FILE;
int count; // count the number of loops for safe exit
// Temporaries
double ftemp;
int itemp;
char stemp[MAXSTR];
// Config and file
lc_devconf_t dconf[MAX_DEV];
time_t start;
FILE* dfile[MAX_DEV];
lct_idle_t idle;
// TO DO:
// Rewrite option parsing to use optarg
// Move globals into main()
// Create a parallel file naming convention
// Transition stream operations to parallel
// optarg processing is split in two parts:
// Save the meta parameters for after the configuration file has been
// parsed. (see below)
while((go = getopt(argc, argv, "hc:d:n:i:f:s:"))!=-1){
switch(go){
case 'c':
strcpy(config_file, optarg);
break;
case 'd':
strcpy(data_file_base, optarg);
break;
case 'n':
if(sscanf(optarg, "%d", &count)!=1){
fprintf(stderr, "LCRUN: -c requires an integer, but got: %s\n", optarg);
return -1;
}
break;
case 'h':
printf(help_text);
return 0;
break;
case 'f':
case 'i':
case 's':
// do nothing for now; we'll process meta parameters after config
// load.
break;
default:
fprintf(stderr, "LCRUN: Got unsupported command line option: %c\n", go);
return -1;
}
}
// Load the configuration
printf("Loading configuration file...");
if(lc_load_config(dconf, MAX_DEV, config_file)){
printf("FAILED\n");
fprintf(stderr, "LCRUN failed while loading the configuration file \"%s\"\n", config_file);
return -1;
}else
printf("DONE\n");
// Detect the number of configured device connections
ndev = lc_ndev(dconf, MAX_DEV);
time(&start);
// Build file names from a timestamp
if(data_file_base[0] == '\0')
strftime(data_file_base, MAXSTR, "%Y%m%d%H%M%S", localtime(&start));
// go back and process meta parameters
optind=1;
while((go = getopt(argc, argv, "c:d:n:i:f:s:"))!=-1){
switch(go){
case 'c':
case 'd':
case 'n':
break;
// It's time; let's process the meta parameters
case 'f':
if(sscanf(optarg,"%[^=]=%lf",(char*) param, &ftemp) != 2){
fprintf(stderr, "LCRUN: expected param=float format, but found %s\n", optarg);
return -1;
}
printf("flt:%s = %lf\n",param,ftemp);
for(devnum=0;devnum<ndev;devnum++){
if (lc_put_meta_flt(&dconf[devnum], param, ftemp))
fprintf(stderr, "LCRUN: failed to set device %d parameter %s to %lf\n", devnum, param, ftemp);
}
break;
case 'i':
if(sscanf(optarg,"%[^=]=%d",(char*) param, &itemp) != 2){
fprintf(stderr, "LCRUN: expected param=integer format, but found %s\n", optarg);
return -1;
}
printf("int:%s = %d\n",param,itemp);
for(devnum=0;devnum<ndev;devnum++){
if (lc_put_meta_int(&dconf[devnum], param, itemp))
fprintf(stderr, "LCRUN: failed to set device %d parameter %s to %d\n", devnum, param, itemp);
}
break;
case 's':
if(sscanf(optarg,"%[^=]=%s",(char*) param, (char*) stemp) != 2){
fprintf(stderr, "LCRUN: expected param=string format, but found %s\n", optarg);
return -1;
}
printf("str:%s = %s\n",param,stemp);
for(devnum=0;devnum<ndev;devnum++){
if (lc_put_meta_str(&dconf[devnum], param, stemp))
fprintf(stderr, "LCRUN: failed to set device %d parameter %s to %s\n", devnum, param, stemp);
}
break;
default:
fprintf(stderr,"LCRUN: Got unsupported command line option: %c\n", go);
return -1;
}
}
// Verify that there are any configurations to execute
if(ndev<=0){
fprintf(stderr,"LCRUN did not detect any valid devices for data acquisition.\n");
return -1;
}
// announce how many devices there are
printf("Found %d device configurations\n",ndev);
// Before we start the streaming process, setup all the devices
for(devnum=0; devnum<ndev; devnum++){
printf("Setting up device %d of %d...", devnum,ndev);
fflush(stdout);
// Open the connection
if(lc_open(&dconf[devnum])){
fprintf(stderr, "LCRUN: Failed while opening device %d of %d\n", devnum, ndev);
halt();
return -1;
}
// Upload the configuration
if(lc_upload_config(&dconf[devnum])){
fprintf(stderr, "LCRUN: Failed while configuring device %d of %d.\n", devnum, ndev);
halt();
return -1;
}
// Prepare the data file
if(ndev == 1)
sprintf(data_file, "%s.dat", data_file_base, devnum);
else
sprintf(data_file, "%s_%d.dat", data_file_base, devnum);
dfile[devnum] = fopen(data_file,"wb");
if(dfile[devnum] == NULL){
fprintf(stderr, "LCRUN: Failed to open data file: %s\n", data_file);
halt();
return -1;
}
lc_datafile_init(&dconf[devnum], dfile[devnum]);
printf("DONE.\n");
}
// Setup the keypress for exit conditions
printf("Press \"Q\" to quit the process\nStreaming measurements...");
fflush(stdout);
lct_setup_keypress();
// Start the stream!
for(devnum=0;devnum<ndev;devnum++){
// Start the data collection
if(lc_stream_start(&dconf[devnum], -1)){
fprintf(stderr, "LCRUN: Failed to start stream on device %d of %d.\n", devnum, ndev);
halt();
return -1;
}
}
go = 1;
lct_idle_init(&idle, 100, 5);
while(go){
for(devnum=0; devnum<ndev; devnum++){
if(lc_stream_service(&dconf[devnum])){
fprintf(stderr, "LCRUN: failed while trying to service device %d of %d\n", devnum, ndev);
lct_finish_keypress();
halt();
return -1;
}
// If data came in
if(!lc_stream_isempty(&dconf[devnum])){
fflush(stdout);
lc_datafile_write(&dconf[devnum], dfile[devnum]);
}
}
// Test for exit conditions
if(lct_is_keypress() && getchar() == 'Q')
go = 0;
fflush(stdout);
lct_idle(&idle);
}
lct_finish_keypress();
halt();
printf("Exited successfully.\n");
return 0;
}