source: hvcontrol/src/ProcessIO.cc@ 20

Last change on this file since 20 was 14, checked in by lstark, 16 years ago
first version of hvcontrol added
File size: 47.1 KB
Line 
1
2/********************************************************************\
3
4 Name: ProcessIO.cc
5
6 Created by: Sebastian Commichau, November 2008
7 commichau@phys.ethz.ch
8
9 Contents: Main class processing user input
10
11\********************************************************************/
12
13
14#include "ProcessIO.h"
15
16
17ProcessIO::ProcessIO(char *ConfigFile) {
18
19 // Get program start time - only needed for uptime command
20 time (&StartTime);
21
22 status = new Status;
23 config = new HVConfig(stdout,ConfigFile);
24 log = new Log(config->fLogPath);
25 hv = new HV(config->fUSBDevice,config->USBDeviceNumber,stdout);
26
27 // Initialize status structure (HVStatus.cc/h)
28 InitStatus(status,config);
29
30 // Print HV control configuration to the log file
31 config->PrintHVConfig(log->logptr);
32
33 // The following loop was added to allow for an arbitrary numeration of the HV boards
34 for (int i=0;i<hv->GetNumberOfBoards();i++) {
35
36 sprintf(status->fUSBDevice[i],"%s",(hv->GetHVBoard(i))->GetSerial());
37 status->USBDeviceNumber[i] = (hv->GetHVBoard(i))->GetBoardNumber();
38
39 if ((hv->GetHVBoard(i))->GetBoardNumber() > status->USBMaxDeviceNumber)
40 status->USBMaxDeviceNumber = (hv->GetHVBoard(i))->GetBoardNumber();
41
42 if ((hv->GetHVBoard(i))->GetBoardNumber() < status->USBMinDeviceNumber)
43 status->USBMinDeviceNumber = (hv->GetHVBoard(i))->GetBoardNumber();
44
45 (hv->GetHVBoard(i))->SetTimeOut(config->fTimeOut);
46 }
47
48 // Initialize HV boards (see below)
49 InitializeHV();
50
51 status->FirstBoard = 0;
52 status->NumHVBoards = hv->GetNumberOfBoards();
53 status->LastBoard = hv->GetNumberOfBoards()-1;
54
55 // Print status information to the log file
56 PrintStatus(status,config,log->logptr);
57
58 // Send reset command to all boards
59 ResetAllBoards();
60
61 // Print some information
62 sprintf(str,"status monitor: %s\n", GetStateStr(status));
63 if (hv->GetNumberOfBoards()>0) DoPrompt(str);
64
65 sprintf(str,"type (h)elp to get a list of available commands\n");
66 DoPrompt(str);
67
68 // Print empty prompt
69 DoPrompt(NULL);
70
71 // Initialize mutex variable for thread synchronization
72 pthread_mutex_init (&control_mutex, NULL);
73 pthread_cond_init (&control_cond, NULL);
74
75}
76
77
78ProcessIO::~ProcessIO() {
79
80 pthread_mutex_destroy (&control_mutex);
81 pthread_cond_destroy (&control_cond);
82
83 config->fStatusRefreshRate = status->fStatusRefreshRate;
84 config->fTimeOut = status->fTimeOut;
85 config->WriteHVConfig(stdout,config->FileName);
86
87 if (hv) delete hv;
88 if (status) delete status;
89 if (config) delete config;
90 if (log) delete log;
91
92}
93
94
95// Print list of all commands
96void ProcessIO::PrintHelp() {
97
98 puts("");
99 puts("********************************************** HELP *********************************************\n");
100 puts(" board <i>|<i> <j>|<all> Address board i, boards i-j or all boards");
101 puts(" chain <i>|<i> <j>|<all> Address chain i, chains i-j or all chains");
102 puts(" clear Clear screen");
103 puts(" config Print configuration");
104 puts(" help Print help");
105 puts(" hv <ch>|<all> [b][x]<v> Set chan. <ch>|<all> chan. of active chain(s)/board(s) to <v>");
106 puts(" hvdiff <ch>|<all> [b][x]<diff> Set chan. <ch>|<all> chan. of active chain(s)/board(s) to <diff>");
107 puts(" list List all HV boards");
108 puts(" load <file> Load HV settings from <file>");
109 puts(" log <on>|<off> Enable|disable logging");
110 puts(" quit|exit Exit program");
111 puts(" rate <rate> Set status refresh rate to <rate> [Hz]");
112 puts(" reset Reset active HV board");
113 puts(" save [file] Save current HV settings to [file]");
114 puts(" start Start HV status monitor");
115 puts(" status Show status information");
116 puts(" stop Stop HV status monitor - not recommended!");
117 puts(" time Print current date and time");
118 puts(" timeout <time> Set timeout to return from read to <time> [s]");
119 puts(" uptime Get program uptime [h:m:s]");
120 puts(" verbose <on>|<off> Enable|disable verbosity");
121 puts(" vref [b][x]<v> Set reference voltage of active chain(s)/board(s) to <v>\n");
122 puts("*************************************************************************************************\n");
123
124}
125
126
127// Process user input
128int ProcessIO::CommandControl() {
129
130 // bool IsDAC;
131 // float fHVCalibOffset;
132 // float fHVCalibSlope;
133 // float fHVMaxDiff;
134
135
136 // Adress HV board
137 if (Match(status->Param[0], "board")) {
138
139 if (!NBoards())
140 return 0;
141 else {
142
143 // Select all boards
144 if (status->Param[1][0] == 'a' && !status->Param[2][0]) {
145
146 status->FirstBoard = 0;
147 status->LastBoard = hv->GetNumberOfBoards()-1;
148
149 }
150 // Wrong type of argument
151 else if (!IsNoDigit(status->Param[1]) || !IsNoDigit(status->Param[2]) || status->Param[3][0] || !status->Param[1][0]) {
152 sprintf(str,"usage: <board> <i>|<i> <j>|<all>\n");
153 DoPrompt(str);
154 return 0;
155 }
156 // Check if board(s) exist(s)
157 else if (status->Param[1][0] || status->Param[2][0]) {
158
159 if (!IsBoard(atoi(status->Param[1]))) {
160 sprintf(str,"board #%d does not exist\n", atoi(status->Param[1]));
161 DoPrompt(str);
162 return 0;
163 }
164
165 if (status->Param[2][0])
166 if (!IsBoard(atoi(status->Param[2]))) {
167 sprintf(str,"board #%d does not exist\n", atoi(status->Param[2]));
168 DoPrompt(str);
169 return 0;
170 }
171
172 if (status->Param[1][0] && status->Param[2][0]) {
173
174 status->FirstBoard = GetBoardIdx(atoi(status->Param[1]));
175 status->LastBoard = GetBoardIdx(atoi(status->Param[2]));
176
177 }
178 else if (status->Param[1][0]) {
179
180 status->FirstBoard = GetBoardIdx(atoi(status->Param[1]));
181 status->LastBoard = status->FirstBoard;
182
183 }
184 }
185
186 //DoPrompt(NULL);
187 }
188
189 DoPrompt(NULL);
190 return 0;
191 }
192
193
194 // Adress chains
195 else if (Match(status->Param[0], "chain")) {
196
197 if (!NBoards())
198 return 0;
199 else {
200
201 if (status->Param[1][0] == 'a') {
202
203 status->FirstChain = 0;
204 status->LastChain = 3;
205
206 }
207 else if (!IsNoDigit(status->Param[1]) || !status->Param[1][0]) {
208 sprintf(str,"usage: <chain> <i>|<i> <j>|<all>\n");
209 DoPrompt(str);
210 return 0;
211 }
212 else if (status->Param[1][0] >= 0 && atoi(status->Param[1]) >= 0 && atoi(status->Param[1]) < 4 && !status->Param[2][0]) {
213
214 status->FirstChain = atoi(status->Param[1]);
215 status->LastChain = status->FirstChain;
216
217 }
218 else if ((status->Param[1][0] >= 0 && atoi(status->Param[1]) >= 0 && atoi(status->Param[1]) < 4) &&
219 (status->Param[2][0] > 0 && atoi(status->Param[2]) > 0 && atoi(status->Param[2]) < 4)) {
220
221 status->FirstChain = atoi(status->Param[1]);
222 status->LastChain = atoi(status->Param[2]);
223
224 }
225 else {
226
227 if (atoi(status->Param[1]) < 0 || atoi(status->Param[1]) > 3) {
228 sprintf(str,"chain #%d does not exist\n", atoi(status->Param[1]));
229 DoPrompt(str);
230 }
231
232
233 if ((atoi(status->Param[2]) < 0 || atoi(status->Param[2]) > 3) && (atoi(status->Param[1]) != atoi(status->Param[2]))) {
234 sprintf(str,"chain #%d does not exist\n", atoi(status->Param[2]));
235 DoPrompt(str);
236 }
237
238 return 0;
239
240 }
241
242 DoPrompt(NULL);
243 }
244
245 return 0;
246 }
247
248
249 // Clear screen
250 else if (Match(status->Param[0], "clear") || Match(status->Param[0], "cls")) {
251
252 system("clear");
253 DoPrompt(NULL);
254
255 return 0;
256
257 }
258
259
260 // Print HV utility configuration
261 else if (Match(status->Param[0], "config")) {
262
263 config->PrintHVConfig(stdout);
264
265 return 0;
266 }
267
268
269 // Print help
270 if (Match(status->Param[0], "help")) {
271
272 PrintHelp();
273 DoPrompt(NULL);
274
275 return 0;
276 }
277
278
279 // Write high voltage -----------------------------------------------------------------------------------------
280 if (Match(status->Param[0], "hv")) {
281
282 if (!NBoards())
283 return 0;
284
285 int errors = 0;
286 unsigned int hvoltage = 0;
287 float hvoltageV = 0.0;
288 int channel = 0;
289 bool allchannels = FALSE;
290
291 if (status->Param[1][0]>0 && status->Param[2][0]>0) {
292
293 // Set channel
294 if (IsNoDigit(status->Param[1]))
295 channel = atoi(status->Param[1]);
296 else if(status->Param[1][0] == 'a')
297 allchannels = TRUE;
298 else {
299 DoPrompt("error: wrong input format - usage: hv <channel>|<all> <voltage>\n");
300 return 0;
301 }
302
303 // Binary input
304 if (tolower(status->Param[2][0])=='x' && strlen(status->Param[2])>2) {
305 if (sPrintHex2Dec(Chop(status->Param[2]+1),&hvoltage)!=0) {
306 DoPrompt("error: wrong input format - usage: hv <channel>|<all> <voltage>\n");
307 return 0;
308 }
309 }
310 // Hexadecimal input
311 else if (tolower(status->Param[2][0])=='b' && strlen(status->Param[2])>2) {
312 if (sPrintBin2Dec(Chop(status->Param[2]+1),&hvoltage)!=0) {
313 DoPrompt("wrong input format - usage: hv <channel>|<all> <voltage>\n");
314 return 0;
315 }
316 }
317 // Decimal input
318 else if (IsNoDigit(status->Param[2])&&config->IsDAC)
319 hvoltage = atoi(status->Param[2]);
320 else if (IsNoDigit(status->Param[2])&&(!(config->IsDAC)))
321 hvoltageV = atof(status->Param[2]);
322 // Wrong input format
323 else {
324 DoPrompt("wrong input format - usage: hv <channel>|<all> <voltage>\n");
325 return 0;
326 }
327
328 // Check limits
329 if (channel>31 || channel <0) {
330 DoPrompt("channel out of range (0...31)!\n");
331 return 0;
332 }
333 else if ((hvoltage>0X3FFF || hvoltage <0)&&config->IsDAC) {
334 DoPrompt("high voltage out of range (Vmin: 0, Vmax: 16383)!\n");
335 return 0;
336 }
337 else if ((hvoltage>78.0 || hvoltage <0)&&(!(config->IsDAC))) {
338 DoPrompt("high voltage out of range (Vmin: 0, Vmax: 78.)!\n");
339 return 0;
340 }
341
342 if (!(config->IsDAC))
343 hvoltage=(unsigned int)((hvoltageV-config->fHVCalibOffset)/config->fHVCalibSlope);
344
345 StopMonitor();
346
347
348 for (int i=status->FirstBoard;i<=status->LastBoard;i++) {
349
350 for (int j=status->FirstChain;j<=status->LastChain;j++) {
351 if (!allchannels) {
352 if ((hv->GetHVBoard(i))->SetHV(stdout,j,channel,hvoltage,rbuf,status->Verbose)==1) {
353
354 status->HV[i][j][channel]=hvoltage;
355 sprintf(str, "Test1 status->HV = %d\n",hvoltage);
356 DoPrompt(str);
357 UpdateStatus(i,rbuf);
358
359 sprintf(str,"board %d: high voltage of chain %d channel %d set to %d | 0X%.4X\n",hv->GetHVBoard(i)->GetBoardNumber(),j,channel,hvoltage,hvoltage);
360 DoPrompt(str);
361 if (status->Verbose) {
362 sPrintStatus(status,str,i);
363 DoPrompt(str);
364 }
365 sPrintStatus(status,str,i); // Print status only to socket
366 }
367 else {
368 sprintf(str,"board %d error: could not set hv - check timeout and try again\n",hv->GetHVBoard(i)->GetBoardNumber());
369 DoPrompt(str);
370 errors++;
371 }
372
373 }
374 else {
375
376 sprintf(str,"updating board %d chain %d\n",hv->GetHVBoard(i)->GetBoardNumber(),j);
377 DoPrompt(str);
378
379
380 for (int k=0;k<MAX_NUM_CHANNELS;k++) {
381 if ((hv->GetHVBoard(i))->SetHV(stdout,j,k,hvoltage,rbuf,status->Verbose)==1) {
382
383 status->HV[i][j][k]=hvoltage;
384 UpdateStatus(i,rbuf);
385
386 if (status->Verbose) {
387 sprintf(str,"board %d: high voltage of chain %d channel %d set to %d | 0X%.4X\n",hv->GetHVBoard(i)->GetBoardNumber(),j,k,hvoltage,hvoltage);
388 DoPrompt(str);
389 sPrintStatus(status,str,i);
390 DoPrompt(str);
391 }
392 sPrintStatus(status,str,i); // Print status only to socket
393
394 }
395 else {
396 sprintf(str,"board %d error: could not set HV - check timeout and try again\n",hv->GetHVBoard(i)->GetBoardNumber());
397 DoPrompt(str);
398 errors++;
399 }
400 }
401 }
402 }
403 }
404
405
406
407 StartMonitor();
408
409 if (errors) {
410 sprintf(str,"warning %d error(s) => check timeout and try again\n",errors);
411 DoPrompt(str);
412 }
413 else {
414 sprintf(str,"no error(s)... success!\n");
415 DoPrompt(str);
416 }
417
418 }
419 else {
420 sprintf(str,"usage: hv <channel>|<all> <voltage>\n");
421 DoPrompt(str);
422 }
423
424 return 0;
425 } // End: Write high voltage ----------------------------------------------------------------------------------------
426
427
428
429 // Write difference of high voltage ------------------------------------------------------------------------------------
430 if (Match(status->Param[0], "hvdiff")) {
431
432 if (!NBoards())
433 return 0;
434
435 int errors = 0;
436 int hvdiff = 0;
437 unsigned int hvoltage = 0;
438 float hvdiffV = 0.0;
439 int channel = 0;
440 bool allchannels = FALSE;
441
442 if (status->Param[1][0]>0 && status->Param[2][0]>0) {
443
444 // Set channel
445 if (IsNoDigit(status->Param[1]))
446 channel = atoi(status->Param[1]);
447 else if(status->Param[1][0] == 'a')
448 allchannels = TRUE;
449 else {
450 DoPrompt("error: wrong input format - usage: hvdiff <channel>|<all> <hv difference>\n");
451 return 0;
452 }
453
454 // Binary input
455 if (tolower(status->Param[2][0])=='x' && strlen(status->Param[2])>2) {
456 if (sPrintHex2Dec(Chop(status->Param[2]+1),(unsigned int *)hvdiff)!=0) {
457 DoPrompt("error: wrong input format - usage: hvdiff <channel>|<all> <hv difference>\n");
458 return 0;
459 }
460 }
461 // Hexadecimal input
462 else if (tolower(status->Param[2][0])=='b' && strlen(status->Param[2])>2) {
463 if (sPrintBin2Dec(Chop(status->Param[2]+1),(unsigned int *)hvdiff)!=0) {
464 DoPrompt("wrong input format - usage: hvdiff <channel>|<all> <hv difference>\n");
465 return 0;
466 }
467 }
468 // Decimal input
469 else if (IsNoDigit(status->Param[2])&&(config->IsDAC))
470 hvdiff = atoi(status->Param[2]);
471 else if (IsNoDigit(status->Param[2])&&(!(config->IsDAC)))
472 hvdiffV = atof(status->Param[2]);
473 // Wrong input format
474 else {
475 DoPrompt("wrong input format - usage: hvdiff <channel>|<all> <hv difference>\n");
476 return 0;
477 }
478 // Check limits
479 if (channel>31 || channel <0) {
480 DoPrompt("channel out of range (0...31)!\n");
481 return 0;
482 }
483 else if (hvdiff>0X3FFF) {
484 DoPrompt("difference of high voltage out of range (Vmin: -16383, Vmax: 16383)!\n");
485 return 0;
486 }
487 else if (hvdiffV>78.0 && (-78.0<hvdiffV)) {
488 DoPrompt("difference of high voltage out of range (Vmin: -78.0, Vmax: 78.0)!\n");
489 return 0;
490 }
491
492 if (!(config->IsDAC)){
493 hvdiff=(int)(hvdiffV/config->fHVCalibSlope);
494 }
495 StopMonitor();
496
497
498 for (int i=status->FirstBoard;i<=status->LastBoard;i++) {
499
500 for (int j=status->FirstChain;j<=status->LastChain;j++) {
501 if (!allchannels) {
502 hvoltage = status->HV[i][j][channel];
503 for (int k=0;k<=abs((int)(hvdiff/config->fHVMaxDiff));k++){
504 if (k<abs((int)(hvdiff/config->fHVMaxDiff))){
505 hvoltage=(unsigned int)(hvoltage + config->fHVMaxDiff*hvdiff/abs(hvdiff));
506 }
507 if (k==(int)(abs((int)(hvdiff/config->fHVMaxDiff)))){
508 hvoltage=(unsigned int)(hvoltage + (hvdiff%(int)config->fHVMaxDiff));
509 }
510
511
512 status->HV[i][j][channel]=hvoltage;
513 if ((hv->GetHVBoard(i))->SetHV(stdout,j,channel,hvoltage,rbuf,status->Verbose)==1) {
514 UpdateStatus(i,rbuf);
515 sprintf(str,"board %d: high voltage of chain %d channel %d set to %d | 0X%.4X\n",hv->GetHVBoard(i)->GetBoardNumber(),j,channel,hvoltage,hvoltage);
516 DoPrompt(str);
517 if (status->Verbose) {
518 sPrintStatus(status,str,i);
519 DoPrompt(str);
520 }
521 sPrintStatus(status,str,i); // Print status only to socket
522 }
523 else {
524 sprintf(str,"board %d error: could not set hv - check timeout and try again\n",hv->GetHVBoard(i)->GetBoardNumber());
525 DoPrompt(str);
526 errors++;
527 }
528 // Sleep(1);
529 }
530 }
531 else {
532
533 sprintf(str,"updating board %d chain %d\n",hv->GetHVBoard(i)->GetBoardNumber(),j);
534 DoPrompt(str);
535
536
537 for (int k=0;k<MAX_NUM_CHANNELS;k++) {
538 int hvoltage = status->HV[i][j][k];
539 for (int l=0;l<=abs((int)(hvdiff/config->fHVMaxDiff));l++){
540 if (l<abs((int)(hvdiff/config->fHVMaxDiff)))
541 hvoltage=(unsigned int)(hvoltage + config->fHVMaxDiff*hvdiff/abs(hvdiff));
542 if (l==(int)(abs((int)(hvdiff/config->fHVMaxDiff))))
543 hvoltage=(unsigned int)(hvoltage + (hvdiff%(int)config->fHVMaxDiff));
544 status->HV[i][j][k]=hvoltage;
545 if ((hv->GetHVBoard(i))->SetHV(stdout,j,k,hvoltage,rbuf,status->Verbose)==1) {
546
547 UpdateStatus(i,rbuf);
548
549 if (status->Verbose) {
550 sprintf(str,"board %d: high voltage of chain %d channel %d set to %d | 0X%.4X\n",hv->GetHVBoard(i)->GetBoardNumber(),j,k,hvoltage,hvoltage);
551 DoPrompt(str);
552 sPrintStatus(status,str,i);
553 DoPrompt(str);
554 }
555 sPrintStatus(status,str,i); // Print status only to socket
556
557 }
558 else {
559 sprintf(str,"board %d error: could not set HV - check timeout and try again\n",hv->GetHVBoard(i)->GetBoardNumber());
560 DoPrompt(str);
561 errors++;
562 }
563 // Sleep(0.001);
564 }
565 }
566 }
567 }
568 }
569 StartMonitor();
570
571 if (errors) {
572 sprintf(str,"warning %d error(s) => check timeout and try again\n",errors);
573 DoPrompt(str);
574 }
575 else {
576 sprintf(str,"no error(s)... success!\n");
577 DoPrompt(str);
578 }
579 }
580 else {
581 sprintf(str,"usage: hvdiff <channel>|<all> <hv difference>\n");
582 DoPrompt(str);
583 }
584 return 0;
585
586 }
587
588 // End: Write difference of high voltage --------------------------------------------------------------------
589
590
591
592 // List HV boards
593 else if (Match(status->Param[0], "list")) {
594
595 sprintf(str,"%d HV board(s) active:\n",hv->GetNumberOfBoards());
596 DoPrompt(str);
597
598 for (int i=0;i<hv->GetNumberOfBoards();i++) {
599 sprintf(str,"board %d (%s)\n",(hv->GetHVBoard(i))->GetBoardNumber(),(hv->GetHVBoard(i))->GetSerial());
600 DoPrompt(str);
601 }
602
603 return 0;
604 }
605
606
607 // Load HV settings from file
608 else if (Match(status->Param[0], "load")) {
609
610 char param[20][MAX_COM_SIZE];
611 char buffer[128];
612
613 int nparam = 0;
614 int nrows = 0;
615 int board = 0;
616 int chain = 0;
617 int channel = 0;
618 int nboards = 0;
619 int errors = 0;
620 unsigned int hvoltage = 0;
621
622 FILE *file;
623
624 if (status->Param[1][0] && !status->Param[2][0]) {
625
626 if ((file=fopen((char *)Chop(status->Param[1]),"r"))!=NULL) {
627
628 if (status->Verbose) {
629 sprintf(str,"file \"%s\" opened\n",Chop(status->Param[1]));
630 DoPrompt(str);
631 }
632
633 StopMonitor();
634
635 while (fgets(buffer,100,file)) {
636
637 ParseInput(buffer,&nparam,param);
638
639 if (nparam==2 && Match(param[0],"Device:")) {
640
641 for (int j=0;j<hv->GetNumberOfBoards();j++)
642 if (Match(Chop(status->fUSBDevice[j]),Chop(param[1]))) {
643
644 board = j;
645
646 sprintf(str,"found HV settings for board %d (%s)\n",hv->GetHVBoard(board)->GetBoardNumber(),Chop(param[1]));
647 DoPrompt(str);
648
649 nboards++;
650 nrows = 0;
651
652 while (fgets(buffer,100,file)) {
653
654 ParseInput(buffer,&nparam,param);
655
656 if (nparam==8) {
657
658 chain = nrows/4;
659
660 if (!((nrows)%4)) {
661 sprintf(str,"updating board %d chain %d\n",hv->GetHVBoard(board)->GetBoardNumber(),chain);
662 DoPrompt(str);
663 }
664
665
666 for (int i=0;i<nparam;i++) {
667
668 hvoltage = atoi(param[i]);
669 channel = (i+8*nrows)%32;
670
671 // Submit HV values
672 if ((hv->GetHVBoard(board))->SetHV(stdout,chain,channel,hvoltage,rbuf,status->Verbose)==1) {
673
674 status->HV[board][chain][channel]=hvoltage;
675 UpdateStatus(board,rbuf);
676
677 if (status->Verbose) {
678 sprintf(str,"board %d: high voltage of chain %d channel %d set to %d | 0X%.4X\n",
679 hv->GetHVBoard(board)->GetBoardNumber(),chain,channel,hvoltage,hvoltage);
680 DoPrompt(str);
681 sPrintStatus(status,str,board);
682 DoPrompt(str);
683 }
684 }
685 else {
686 sprintf(str,"board %d error: could not set HV - check timeout and try again\n",hv->GetHVBoard(board)->GetBoardNumber());
687 DoPrompt(str);
688 errors++;
689 }
690
691 }
692
693 nrows++;
694
695 }
696 else if (nparam==2)
697 break;
698 }
699 }
700 }
701 }
702
703 if (fclose (file)) {
704 sprintf(str,"error: could not close file \"%s\"\n",Chop(status->Param[1]));
705 DoPrompt(str);
706 }
707 else {
708 if (status->Verbose) {
709 sprintf(str,"file \"%s\" closed\n",Chop(status->Param[1]));
710 DoPrompt(str);
711 }
712 }
713
714 if (nboards!=hv->GetNumberOfBoards()) {
715 sprintf(str,"warning: could not load HV settings for all connected HV boards\n");
716 DoPrompt(str);
717 }
718 else {
719 sprintf(str,"success: read HV settings for all connected HV boards\n");
720 DoPrompt(str);
721 }
722
723 if (errors) {
724 sprintf(str,"warning %d error(s) => check timeout and try again\n",errors);
725 DoPrompt(str);
726 }
727 else {
728 sprintf(str,"no error(s)... success!\n");
729 DoPrompt(str);
730 }
731
732 }
733 else {
734 sprintf(str,"error: could not open file \"%s\"\n",Chop(status->Param[1]));
735 DoPrompt(str);
736 }
737
738 }
739 else
740 DoPrompt("usage: load <file>\n");
741
742 StartMonitor();
743
744 return 0;
745
746 }
747
748
749 // Enable/disable logging
750 else if (Match(status->Param[0], "log")) {
751
752 if (Match(status->Param[1], "on") && status->Param[1][0]) {
753 status->Log = TRUE;
754 sprintf(str,"logging enabled\n");
755 DoPrompt(str);
756 }
757
758 else if (Match(status->Param[1], "off") && status->Param[1][0]) {
759 status->Log = FALSE;
760 sprintf(str,"logging disabled\n");
761 DoPrompt(str);
762 }
763
764 else
765 DoPrompt("usage: log <on>|<off>\n");
766
767 return 0;
768 }
769
770
771 // Set status refresh rate
772 if (Match(status->Param[0], "rate")) {
773
774 if (!NBoards())
775 return 0;
776
777 if (status->Param[1][0]>0) {
778
779 if (!IsNoDigit(status->Param[1])) {
780 DoPrompt("wrong input format - usage: rate <rate>\n");
781 return 0;
782 }
783 // Check limits
784 else if (atof(status->Param[1]) < MIN_RATE || atof(status->Param[1]) > MAX_RATE) {
785 sprintf(str,"refresh rate out of range (min: %.2f Hz, max: %.2f Hz)!\n",MIN_RATE,MAX_RATE);
786 DoPrompt(str);
787 return 0;
788 }
789 else {
790 StopMonitor();
791 status->fStatusRefreshRate=atof(status->Param[1]);
792 sprintf(str,"status refresh rate set to %.2f Hz\n",status->fStatusRefreshRate);
793 DoPrompt(str);
794 }
795 StartMonitor();
796 return 0;
797
798 }
799 else {
800 sprintf(str,"usage: rate <rate>\n");
801 DoPrompt(str);
802 }
803
804 return 0;
805 }
806
807 /*
808 // Read from device - NEVER use this function with a real HV board!!!
809 if (Match(status->Param[0], "read")) {
810
811 if (!NBoards())
812 return 0;
813
814 if (status->state==active) {
815 StopMonitor();
816 sprintf(str,"warning: status monitoring deactivated\n");
817 DoPrompt(str);
818 }
819
820 for (int i=status->FirstBoard;i<=status->LastBoard;i++) {
821 if (1 == ((hv->GetHVBoard(i))->Read(rbuf,1))) {
822 sPrintByteBin(rbuf[0],bdata);
823 sprintf(str,"%d byte(s) read (board %d): %d | 0X%.2X | B%s\n",1,i,rbuf[0],rbuf[0],bdata);
824 }
825 else
826 sprintf(str,"error: could not read from board %d\n",i);
827
828 usleep(1000);
829
830 DoPrompt(str);
831 }
832
833 return 0;
834 }
835 */
836
837 // Reset
838 if (Match(status->Param[0], "reset")) {
839
840 if (!NBoards())
841 return 0;
842
843 StopMonitor();
844 ResetActiveBoards();
845 ReInitStatus(status);
846 StartMonitor();
847 return 0;
848 }
849
850
851 // Start ROOT
852 else if (Match(status->Param[0], "root")) {
853
854 sprintf(str,"starting ROOT... type '.q' to return\n");
855 DoPrompt(str);
856 system ("root");
857
858 return 0;
859
860 }
861
862
863 // Save HV settings of all boards
864 else if (Match(status->Param[0], "save")) {
865
866 if (status->Param[1][0] && !status->Param[2][0]) {
867
868 if (SaveHVSettings(Chop(status->Param[1]))) {
869 sprintf(str,"HV settings written to \"%s\"\n",Chop(status->Param[1]));
870 DoPrompt(str);
871 }
872
873 }
874 else {
875
876 char buffer[MAX_COM_SIZE];
877
878 time_t time_now_secs;
879 struct tm *time_now;
880
881 time(&time_now_secs);
882 time_now = localtime(&time_now_secs);
883
884 sprintf(buffer,"hvsettings/HV_%04d-%02d-%02d_%02d-%02d-%02d.txt",
885 1900 + time_now->tm_year,
886 1 + time_now->tm_mon,
887 time_now->tm_mday,
888 time_now->tm_hour,
889 time_now->tm_min,
890 time_now->tm_sec);
891
892 //sprintf(str,"warning: HV settings will be written to \"%s\"\n",buffer);
893 //DoPrompt(str);
894
895 if (SaveHVSettings(buffer)) {
896 sprintf(str,"HV settings successfully written to \"%s\"\n",buffer);
897 DoPrompt(str);
898 }
899 }
900
901 return 0;
902
903 }
904
905
906 // Start monitoring
907 else if (Match(status->Param[0], "start")) {
908
909 if (!NBoards())
910 return 0;
911
912 if (status->state==active) {
913 sprintf(str,"status monitoring is already active\n");
914 DoPrompt(str);
915 return 0;
916 }
917
918 StartMonitor();
919
920 sprintf(str,"status monitoring activated\n");
921 DoPrompt(str);
922
923 return 0;
924
925 }
926
927
928 // Print status
929 else if (Match(status->Param[0], "status") || Match(status->Param[0], "info")) {
930
931 if (!NBoards())
932 return 0;
933
934 PrintStatus(status,config,stdout);
935 PrintStatus(status,config,log->logptr);
936
937 return 0;
938 }
939
940
941 // Stop monitoring
942 else if (Match(status->Param[0], "stop")) {
943
944 if (!NBoards())
945 return 0;
946
947 if (status->state!=active) {
948 sprintf(str,"status monitoring is already deactivated\n");
949 DoPrompt(str);
950 return 0;
951 }
952
953 StopMonitor();
954
955 sprintf(str,"warning: status monitoring deactivated\n");
956 DoPrompt(str);
957
958 return 0;
959
960 }
961
962 /*
963 // Sweep HV
964 else if (Match(status->Param[0], "sweep")) {
965
966 if (!NBoards())
967 return 0;
968
969 int errors = 0;
970 int channel = 0;
971
972 unsigned int hv1 = 0;
973 unsigned int hv2 = 0;
974 unsigned int dv = 10;
975
976 bool allchannels = FALSE;
977
978 float delay = 1.;
979
980 if (status->Param[1][0]>0 && status->Param[2][0]>0 && status->Param[3][0]>0 && status->Param[4][0]>0){
981
982 if (status->Param[1][0] == 'a' )
983 allchannels = TRUE;
984 else if (IsNoDigit(status->Param[1])) {
985 channel = atoi(status->Param[1]);
986 // Check limits
987 if (channel>31 || channel <0) {
988 DoPrompt("channel out of range (0...31)!\n");
989 return 0;
990 }
991 }
992
993 if (IsNoDigit(status->Param[2]) && IsNoDigit(status->Param[3]) && IsNoDigit(status->Param[4])) {
994
995 hv1 = atoi(status->Param[2]);
996 hv2 = atoi(status->Param[3]);
997 dv = atoi(status->Param[4]);
998
999 if ((hv1>0X3FFF || hv1 <0) || (hv2>0X3FFF || hv2 <0)) {
1000 DoPrompt("high voltage out of range (Vmin: 0, Vmax: 16383)!\n");
1001 return 0;
1002 }
1003
1004 if (hv1 > hv2) {
1005 DoPrompt("wrong limits (Vmin < Vmax)!\n");
1006 return 0;
1007 }
1008
1009 if (dv < 1 || dv > abs(hv2-hv1)) {
1010 DoPrompt("step size out of range (dVmin: 1, dVmax: |hv2-hv1|)!\n");
1011 return 0;
1012 }
1013
1014 if (status->Param[5][0]>0 && IsNoDigit(status->Param[5])) {
1015 if (atof(status->Param[5])<MIN_SWEEP_RATE || atof(status->Param[5])>MAX_SWEEP_RATE) {
1016 sprintf(str,"rate out of range (min: %.2f Hz, max: %.2f Hz)!\n",MIN_SWEEP_RATE,MAX_SWEEP_RATE);
1017 DoPrompt(str);
1018 return 0;
1019 }
1020 else
1021 delay = 1./atof(status->Param[5]);
1022 }
1023 }
1024 else {
1025 DoPrompt("error: wrong input format - usage: sweep <channel>|<all> <v1> <v2> <dv> [rate]\n");
1026 return 0;
1027 }
1028
1029
1030 StopMonitor();
1031
1032 for (int i=status->FirstBoard;i<=status->LastBoard;i++)
1033 for (int j=status->FirstChain;j<=status->LastChain;j++) {
1034 if (!allchannels) {
1035
1036 for (unsigned int v = hv1; v<=hv2; v+=dv) {
1037 if ((hv->GetHVBoard(i))->SetHV(stdout,j,channel,v,rbuf,status->Verbose)==1) {
1038 sprintf(str,"board %d: high voltage of chain %d channel %d set to %d | 0X%.4X\n",i,j,channel,v,v);
1039 status->HV[i][j][channel]=v;
1040 DoPrompt(str);
1041 UpdateStatus(i,rbuf);
1042 sPrintStatus(status,str,i);
1043 DoPrompt(str);
1044 }
1045 else {
1046 sprintf(str,"board %d error: could not set hv - check timeout and try again\n",i);
1047 DoPrompt(str);
1048 errors++;
1049 }
1050
1051 usleep((unsigned long)floor(delay*1000000.));
1052 }
1053
1054 }
1055 else {
1056 for (int k=0;k<MAX_NUM_CHANNELS;k++) {
1057
1058 for (unsigned int v = hv1; v<=hv2; v+=dv) {
1059
1060 if ((hv->GetHVBoard(i))->SetHV(stdout,j,k,v,rbuf,status->Verbose)==1) {
1061 sprintf(str,"board %d: high voltage of chain %d channel %d set to %d | 0X%.4X\n",i,j,k,v,v);
1062 status->HV[i][j][k]=v;
1063 DoPrompt(str);
1064 UpdateStatus(i,rbuf);
1065 sPrintStatus(status,str,i);
1066 DoPrompt(str);
1067 }
1068 else {
1069 sprintf(str,"board %d error: could not set hv - check timeout and try again\n",i);
1070 DoPrompt(str);
1071 errors++;
1072 }
1073
1074 usleep((unsigned long)floor(delay*1000000.));
1075
1076 }
1077 }
1078 }
1079 }
1080
1081 StartMonitor();
1082
1083 if (errors) {
1084 sprintf(str,"warning %d error(s) => check timeout and try again\n",errors);
1085 DoPrompt(str);
1086 }
1087 else {
1088 sprintf(str,"no error(s)... success!\n");
1089 DoPrompt(str);
1090 }
1091 }
1092 else {
1093 DoPrompt("usage: sweep <channel>|<all> <v1> <v2> <dv> [rate]\n");
1094 return 0;
1095 }
1096
1097 return 0;
1098
1099 }
1100 */
1101
1102 // Print time and date
1103 else if (Match(status->Param[0], "date") || Match(status->Param[0], "time")) {
1104
1105 PrintDateAndTime();
1106
1107 return 0;
1108 }
1109
1110
1111 /*
1112 // Test I/O - NEVER use this function with a real HV board!!!
1113 else if (Match(status->Param[0], "testio")) {
1114
1115 for (int i=status->FirstBoard;i<=status->LastBoard;i++)
1116 (hv->GetHVBoard(i))->TestIO();
1117
1118 return 0;
1119 }
1120 */
1121
1122
1123 // Set timeout to return from read
1124 if (Match(status->Param[0], "timeout")) {
1125
1126 if (!NBoards())
1127 return 0;
1128
1129 if (status->Param[1][0]>0) {
1130
1131 if (!IsNoDigit(status->Param[1])) {
1132 DoPrompt("wrong input format - usage: timeout <time>\n");
1133 return 0;
1134 }
1135 // Check limits
1136 else if (atof(status->Param[1]) < MIN_TIMEOUT || atof(status->Param[1]) > MAX_TIMEOUT) {
1137 sprintf(str,"timeout out of range (min: %.2f s, max: %.2f s)!\n",MIN_TIMEOUT,MAX_TIMEOUT);
1138 DoPrompt(str);
1139 return 0;
1140 }
1141 else {
1142 StopMonitor();
1143 for (int i=0;i<hv->GetNumberOfBoards();i++)
1144 (hv->GetHVBoard(i))->SetTimeOut((float)atof(status->Param[1]));
1145 status->fTimeOut = atof(status->Param[1]);
1146 sprintf(str,"timeout set to %.2f s\n",status->fTimeOut);
1147
1148 DoPrompt(str);
1149
1150 StartMonitor();
1151 return 0;
1152 }
1153 }
1154 else {
1155 sprintf(str,"usage: timeout <time>\n");
1156 DoPrompt(str);
1157 }
1158
1159 return 0;
1160 }
1161
1162
1163 // Print uptime
1164 if (Match(status->Param[0], "uptime")) {
1165
1166 double difftime = GetDiffTime(&StartTime);
1167
1168 sprintf(str,"%d:%02d:%02d\n",(int)difftime/SECONDS_HOUR,((int)difftime/SECONDS_MINUTE)%SECONDS_MINUTE,(int)difftime%SECONDS_MINUTE);
1169 DoPrompt(str);
1170
1171 return 0;
1172 }
1173
1174
1175 // Enable/disable verbosity
1176 else if (Match(status->Param[0], "verbose")) {
1177
1178 if (Match(status->Param[1], "on") && status->Param[1][0]) {
1179 if (status->Verbose == TRUE) {
1180 sprintf(str,"verbosity is already enabled\n");
1181 DoPrompt(str);
1182 }
1183 else {
1184 status->Verbose = TRUE;
1185 sprintf(str,"verbosity enabled\n");
1186 DoPrompt(str);
1187 }
1188 }
1189
1190 else if (Match(status->Param[1], "off") && status->Param[1][0]) {
1191 if (status->Verbose == FALSE) {
1192 sprintf(str,"verbosity is already disabled\n");
1193 DoPrompt(str);
1194 }
1195 else {
1196 status->Verbose = FALSE;
1197 sprintf(str,"verbosity disabled\n");
1198 DoPrompt(str);
1199 }
1200 }
1201
1202 else
1203 DoPrompt("usage: verbose <on>|<off>\n");
1204
1205 return 0;
1206 }
1207
1208
1209 // Write reference voltage
1210 if (Match(status->Param[0], "vref")) {
1211
1212 if (!NBoards())
1213 return 0;
1214
1215 unsigned int voltage = 0;
1216
1217 if (status->Param[1][0]>0) {
1218
1219 // Binary input
1220 if (tolower(status->Param[1][0])=='x' && strlen(status->Param[1])>2) {
1221 if (sPrintHex2Dec(Chop(status->Param[1]+1),&voltage)!=0) {
1222 DoPrompt("error: wrong input format - usage: vref <voltage>\n");
1223 return 0;
1224 }
1225 }
1226 // Hexadecimal input
1227 else if (tolower(status->Param[1][0])=='b' && strlen(status->Param[1])>2) {
1228 if (sPrintBin2Dec(Chop(status->Param[1]+1),&voltage)!=0) {
1229 DoPrompt("wrong input format - usage: vref <voltage>\n");
1230 return 0;
1231 }
1232 }
1233 // Decimal input
1234 else if (IsNoDigit(status->Param[1]))
1235 voltage = atoi(status->Param[1]);
1236 // Wrong input format
1237 else {
1238 DoPrompt("wrong input format - usage: vref <voltage>\n");
1239 return 0;
1240 }
1241
1242 // Check limits
1243 if (voltage>0X3FFF || voltage <0) {
1244 DoPrompt("reference voltage out of range (Vmin: 0, Vmax: 16383)!\n");
1245 return 0;
1246 }
1247
1248
1249 StopMonitor();
1250
1251 for (int i=status->FirstBoard;i<=status->LastBoard;i++)
1252 for (int j=status->FirstChain;j<=status->LastChain;j++) {
1253 if (((hv->GetHVBoard(i))->SetVRef(stdout,j,voltage,rbuf,status->Verbose)==1)) {
1254 sprintf(str,"board %d: reference voltage of chain %d set to %d | 0X%.4X\n",hv->GetHVBoard(i)->GetBoardNumber(),j,voltage,voltage);
1255 status->VRef[i][j]=voltage;
1256 DoPrompt(str);
1257 UpdateStatus(i,rbuf);
1258 sPrintStatus(status,str,i);
1259 DoPrompt(str);
1260 } else {
1261 sprintf(str,"board %d error: could not set vref - check timeout and try again\n",hv->GetHVBoard(i)->GetBoardNumber());
1262 DoPrompt(str);
1263 }
1264 }
1265
1266 StartMonitor();
1267 }
1268 else {
1269 sprintf(str,"usage: vref <voltage>\n");
1270 DoPrompt(str);
1271 }
1272
1273 return 0;
1274 }
1275
1276 /*
1277 // Write to device - NEVER use this function with a real HV board!!!
1278 if (Match(status->Param[0], "write")) {
1279
1280 if (!NBoards())
1281 return 0;
1282
1283 if (status->Param[1][0]>0) {
1284
1285 // Binary input
1286 if (tolower(status->Param[1][0])=='x' && strlen(status->Param[1])>2) {
1287 if (sPrintHex2Dec(Chop(status->Param[1]+1),(unsigned int*)wbuf)!=0) {
1288 DoPrompt("wrong input format or value out of range!\n");
1289 return 0;
1290 }
1291 }
1292 // Hexadecimal input
1293 else if (tolower(status->Param[1][0])=='b' && strlen(status->Param[1])>2) {
1294 if (sPrintBin2Dec(Chop(status->Param[1]+1),(unsigned int*)wbuf)!=0) {
1295 DoPrompt("wrong input format or value out of range!\n");
1296 return 0;
1297 }
1298 }
1299 // Decimal input
1300 else if (atoi(status->Param[1])>=0 && atoi(status->Param[1])<=255 && IsNoDigit(status->Param[1]))
1301 wbuf[0] = (unsigned char)atoi(status->Param[1]);
1302 // Wrong input
1303 else {
1304 DoPrompt("wrong input format or value out of range!\n");
1305 return 0;
1306 }
1307
1308 if (status->state==active) {
1309 StopMonitor();
1310 sprintf(str,"warning: status monitoring deactivated\n");
1311 DoPrompt(str);
1312 }
1313
1314 for (int i=status->FirstBoard;i<=status->LastBoard;i++)
1315 if ((hv->GetHVBoard(i))->Write(wbuf,1) == 1) {
1316 sPrintByteBin(wbuf[0],bdata);
1317 sprintf(str,"%d byte(s) written (board %d): %d | 0X%.2X | B%s\n",1,i,wbuf[0],wbuf[0],bdata);
1318 DoPrompt(str);
1319 }
1320 else {
1321 sprintf(str,"error: could not write to board %d\n",i);
1322 DoPrompt(str);
1323 }
1324
1325 }
1326 else {
1327 sprintf(str,"error: no input\n");
1328 DoPrompt(str);
1329 }
1330 return 0;
1331 }
1332 */
1333
1334 // Exit program
1335 else if(Match(status->Param[0], "exit") || Match(status->Param[0], "quit")) {
1336
1337 StopMonitor();
1338
1339 ResetAllBoards();
1340
1341 status->Exit = TRUE;
1342
1343 pthread_kill(status->HVMonitor, SIGUSR1);
1344 pthread_kill(status->SocketThread, SIGUSR1);
1345
1346 return 0;
1347 }
1348
1349 else if (strchr(status->Param[0],'\n')-status->Param[0]==0) {
1350 return 0;
1351 }
1352
1353 else {
1354
1355 if (strchr(status->Param[0],'\n') == 0)
1356 sprintf(str,"unknown command: %s\n",status->Param[0]);
1357 else
1358 sprintf(str,"unknown command: %s",status->Param[0]);
1359
1360 DoPrompt(str);
1361
1362 return 0;
1363
1364 }
1365
1366 return 0;
1367}
1368
1369
1370void ProcessIO::PrintDateAndTime() {
1371
1372 char str[MAX_COM_SIZE];
1373
1374 time_t rawtime;
1375 struct tm * timeinfo;
1376
1377 time(&rawtime);
1378 timeinfo = localtime(&rawtime); // Get local time
1379 fflush(stdout);
1380 sprintf(str,"current date/time is: %s",asctime(timeinfo));
1381 DoPrompt(str);
1382
1383}
1384
1385void ProcessIO::DoPrompt(char* str) {
1386
1387 if (str!=NULL) {
1388 if (status->NumHVBoards == 0) {
1389 sprintf(status->Prompt,"HV> %s",str);
1390 printf(status->Prompt);
1391 if (status->Log)
1392 log->LogWrite(status->Prompt);
1393 sprintf(status->Prompt,"HV> ");
1394 }
1395 else if (status->FirstBoard == status->LastBoard) {
1396
1397 int board = hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1398
1399 if (status->FirstChain == status->LastChain) {
1400 sprintf(status->Prompt,"HV|C%d|B%d> %s",status->FirstChain,board,str);
1401 printf(status->Prompt);
1402 if (status->Log)
1403 log->LogWrite(status->Prompt);
1404 sprintf(status->Prompt,"HV|C%d|B%d> ",status->FirstChain,board);
1405 }
1406 else {
1407 sprintf(status->Prompt,"HV|C%d-%d|B%d> %s",status->FirstChain,status->LastChain,board,str);
1408 printf(status->Prompt);
1409 if (status->Log)
1410 log->LogWrite(status->Prompt);
1411 sprintf(status->Prompt,"HV|C%d-%d|B%d> ",status->FirstChain,status->LastChain,board);
1412 }
1413
1414 }
1415 else {
1416
1417 int firstboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1418 hv->GetHVBoard(status->LastBoard)->GetBoardNumber() : hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1419
1420 int lastboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1421 hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() : hv->GetHVBoard(status->LastBoard)->GetBoardNumber();
1422
1423 if (status->FirstChain == status->LastChain) {
1424 sprintf(status->Prompt,"HV|C%d|B%d-%d> %s",status->FirstChain,firstboard,lastboard,str);
1425 printf(status->Prompt);
1426 if (status->Log)
1427 log->LogWrite(status->Prompt);
1428 sprintf(status->Prompt,"HV|C%d|B%d-%d> ",status->FirstChain,firstboard,lastboard);
1429 }
1430 else {
1431 sprintf(status->Prompt,"HV|C%d-%d|B%d-%d> %s",status->FirstChain,status->LastChain,firstboard,lastboard,str);
1432 printf(status->Prompt);
1433 if (status->Log)
1434 log->LogWrite(status->Prompt);
1435 sprintf(status->Prompt,"HV|C%d-%d|B%d-%d> ",status->FirstChain,status->LastChain,firstboard,lastboard);
1436 }
1437 }
1438 }
1439 else {
1440 if (status->NumHVBoards == 0)
1441 sprintf(status->Prompt,"HV> ");
1442 else if (status->FirstBoard == status->LastBoard) {
1443
1444 int board = hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1445
1446 if (status->FirstChain == status->LastChain)
1447 sprintf(status->Prompt,"HV|C%d|B%d> ",status->FirstChain,board);
1448 else
1449 sprintf(status->Prompt,"HV|C%d-%d|B%d> ",status->FirstChain,status->LastChain,board);
1450 }
1451 else {
1452
1453 int firstboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1454 hv->GetHVBoard(status->LastBoard)->GetBoardNumber() : hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1455
1456 int lastboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1457 hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() : hv->GetHVBoard(status->LastBoard)->GetBoardNumber();
1458
1459 if (status->FirstChain == status->LastChain)
1460 sprintf(status->Prompt,"HV|C%d|B%d-%d> ",status->FirstChain,firstboard,lastboard);
1461 else
1462 sprintf(status->Prompt,"HV|C%d-%d|B%d-%d> ",status->FirstChain,status->LastChain,firstboard,lastboard);
1463 }
1464 }
1465
1466
1467}
1468
1469
1470void ProcessIO::PrintMessage(char *str) {
1471
1472 char outstr[MAX_COM_SIZE];
1473
1474 fflush(stdout);
1475 if (str!=NULL) {
1476
1477 if (status->NumHVBoards == 0)
1478 sprintf(outstr,"%s\nHV> ",str);
1479 else if (status->FirstBoard == status->LastBoard) {
1480
1481 int board = hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1482
1483 if (status->FirstChain == status->LastChain)
1484 sprintf(outstr,"%s\nHV|C%d|B%d> ",str,status->FirstChain,board);
1485 else
1486 sprintf(outstr,"%s\nHV|C%d-%d|B%d> ",str,status->FirstChain,status->LastChain,board);
1487 }
1488 else {
1489
1490 int firstboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1491 hv->GetHVBoard(status->LastBoard)->GetBoardNumber() : hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1492
1493 int lastboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1494 hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() : hv->GetHVBoard(status->LastBoard)->GetBoardNumber();
1495
1496 if (status->FirstChain == status->LastChain)
1497 sprintf(outstr,"%s\nHV|C%d|B%d-%d> ",str,status->FirstChain,firstboard,lastboard);
1498 else
1499 sprintf(outstr,"%s\nHV|C%d-%d|B%d-%d> ",str,status->FirstChain,status->LastChain,firstboard,lastboard);
1500 }
1501 }
1502 else {
1503
1504 if (status->NumHVBoards == 0)
1505 sprintf(outstr,"HV> ");
1506 else if (status->FirstBoard == status->LastBoard) {
1507
1508 int board = hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1509
1510 if (status->FirstChain == status->LastChain)
1511 sprintf(outstr,"HV|C%d|B%d> ",status->FirstChain,board);
1512 else
1513 sprintf(outstr,"HV|C%d-%d|B%d> ",status->FirstChain,status->LastChain,board);
1514 }
1515 else {
1516
1517 int firstboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1518 hv->GetHVBoard(status->LastBoard)->GetBoardNumber() : hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1519
1520 int lastboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1521 hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() : hv->GetHVBoard(status->LastBoard)->GetBoardNumber();
1522
1523 if (status->FirstChain == status->LastChain)
1524 sprintf(outstr,"HV|C%d|B%d-%d> ",status->FirstChain,firstboard,lastboard);
1525 else
1526 sprintf(outstr,"HV|C%d-%d|B%d-%d> ",status->FirstChain,status->LastChain,firstboard,lastboard);
1527 }
1528
1529
1530 }
1531
1532 fflush(stdout);
1533 fputs(outstr, stdout);
1534 fflush(stdout);
1535
1536}
1537
1538
1539void ProcessIO::PrintMessageToLog(char *str) {
1540
1541 char outstr[MAX_COM_SIZE];
1542
1543 if (status->NumHVBoards == 0) {
1544 sprintf(outstr,"HV|B> %s\n",str);
1545 log->LogWrite(outstr);
1546 }
1547 else if (status->FirstBoard == status->LastBoard) {
1548
1549 int board = hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1550
1551 sprintf(outstr,"HV|B%d> %s\n",board,str);
1552 log->LogWrite(outstr);
1553 }
1554 else {
1555
1556 int firstboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1557 hv->GetHVBoard(status->LastBoard)->GetBoardNumber() : hv->GetHVBoard(status->FirstBoard)->GetBoardNumber();
1558
1559 int lastboard = (hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() > hv->GetHVBoard(status->LastBoard)->GetBoardNumber()) ?
1560 hv->GetHVBoard(status->FirstBoard)->GetBoardNumber() : hv->GetHVBoard(status->LastBoard)->GetBoardNumber();
1561
1562 sprintf(outstr,"HV|B%d-%d> %s\n",firstboard,lastboard,str);
1563 log->LogWrite(outstr);
1564 }
1565
1566}
1567
1568
1569// Print message to screen, log file and socket
1570void ProcessIO::PrintMessageO(char *Format, ...) {
1571
1572 char Textbuffer[MAX_COM_SIZE];
1573
1574 va_list ArgumentPointer; va_start(ArgumentPointer, Format);
1575 vsprintf(Textbuffer, Format, ArgumentPointer);
1576
1577 fputs(Textbuffer, stdout); fflush(stdout); // Print to console
1578
1579 if(status->Socket != -1) // Print to socket if open
1580 write(status->Socket,Textbuffer,strlen(Textbuffer)+1); // +1 to transmit '\0'
1581}
1582
1583
1584int ProcessIO::NBoards() {
1585
1586 if (status->NumHVBoards==0)
1587 DoPrompt("no HV boards available!\n");
1588
1589 return status->NumHVBoards;
1590}
1591
1592
1593int ProcessIO::InitializeHV() {
1594
1595 int nb = 0;
1596
1597 if (hv->GetNumberOfBoards())
1598 printf("Initialization:\n");
1599
1600 for (int i=0;i<hv->GetNumberOfBoards();i++) {
1601 printf(" HV board %d (%s):\n",hv->GetHVBoard(i)->GetBoardNumber(),hv->GetHVBoard(i)->GetSerial());
1602 nb+=hv->GetHVBoard(i)->Init(status->Verbose);
1603 }
1604
1605 return nb;
1606}
1607
1608
1609void ProcessIO::UpdateStatus(int i, unsigned char* rbuf) {
1610
1611 if (status->IsUpdated[i])
1612 status->WC[0][i] = status->WC[1][i];
1613 else {
1614 status->WC[0][i] = hv->GetHVBoard(i)->DecodeWrap(rbuf)-1;
1615 status->IsUpdated[i] = TRUE;
1616 }
1617
1618 status->WC[1][i] = hv->GetHVBoard(i)->DecodeWrap(rbuf);
1619
1620 int d = (int)abs(status->WC[0][i]-status->WC[1][i]);
1621
1622 status->isok[i] = (d==1 || d==7) ? 1 : 0;
1623
1624 hv->GetHVBoard(i)->DecodeOC(status->OC[i], rbuf);
1625 status->MR[i] = hv->GetHVBoard(i)->DecodeReset(rbuf);
1626
1627}
1628
1629
1630void ProcessIO::StartMonitor() {
1631
1632 status->state = active;
1633 status->Stop = FALSE;
1634
1635 pthread_kill(status->HVMonitor, SIGUSR1);
1636
1637}
1638
1639
1640void ProcessIO::StopMonitor() {
1641
1642 status->state = stopped;
1643 status->Stop = TRUE;
1644
1645 pthread_kill(status->HVMonitor, SIGUSR1);
1646
1647}
1648
1649
1650void ProcessIO::Monitor() {
1651
1652 char str[MAX_COM_SIZE];
1653
1654 for (int i=0;i<hv->GetNumberOfBoards() ;i++) {
1655
1656 if ((hv->GetHVBoard(i))->GetStatus(stdout,rbuf,FALSE)!=1) {
1657 sprintf(str,"board %d error: could not read status - check timeout and status refresh rate...",hv->GetHVBoard(i)->GetBoardNumber());
1658 PrintMessage(str);
1659 }
1660 else
1661 UpdateStatus(i,rbuf);
1662
1663 if (status->MR[i]) {
1664 sprintf(str,"warning: manual reset of board %d!",hv->GetHVBoard(i)->GetBoardNumber());
1665 PrintMessage(str);
1666 StopMonitor();
1667 ResetBoard(i);
1668 ReInitStatusOneBoard(status,i);
1669 StartMonitor();
1670 }
1671
1672 if (!status->isok[i]) {
1673 sprintf(str,"error: wrap counter mismatch board %d (%d,%d)!",hv->GetHVBoard(i)->GetBoardNumber(),status->WC[0][i],status->WC[1][i]);
1674 PrintMessage(str);
1675 }
1676
1677 for (int j=0;j<MAX_NUM_CHAINS;j++)
1678 if (status->OC[i][j]) {
1679 sprintf(str,"warning: overcurrent in chain %d of board %d!",j,hv->GetHVBoard(i)->GetBoardNumber());
1680 PrintMessage(str);
1681 ResetBoard(i);
1682 ReInitStatusOneBoard(status,i);
1683 }
1684 }
1685}
1686
1687
1688void ProcessIO::ResetActiveBoards() {
1689
1690 for (int i=status->FirstBoard;i<=status->LastBoard;i++) {
1691 if ((hv->GetHVBoard(i))->Reset(stdout,rbuf,status->Verbose)==1) {
1692 sprintf(str,"software reset done board %d\n",hv->GetHVBoard(i)->GetBoardNumber());
1693 DoPrompt(str);
1694 UpdateStatus(i,rbuf);
1695 sPrintStatus(status,str,i);
1696 DoPrompt(str);
1697 }
1698 else {
1699 sprintf(str,"board %d error: could not reset - check timeout and try again\n",hv->GetHVBoard(i)->GetBoardNumber());
1700 DoPrompt(str);
1701 }
1702 }
1703
1704}
1705
1706
1707void ProcessIO::ResetAllBoards() {
1708
1709 for (int i=0;i<hv->GetNumberOfBoards();i++) {
1710 if ((hv->GetHVBoard(i))->Reset(stdout,rbuf,status->Verbose)==1) {
1711 sprintf(str,"software reset done board %d\n",hv->GetHVBoard(i)->GetBoardNumber());
1712 DoPrompt(str);
1713 UpdateStatus(i,rbuf);
1714 sPrintStatus(status,str,i);
1715 DoPrompt(str);
1716 }
1717 else {
1718 sprintf(str,"board %d error: could not reset - check timeout and try again\n",hv->GetHVBoard(i)->GetBoardNumber());
1719 DoPrompt(str);
1720 }
1721 }
1722
1723}
1724
1725
1726void ProcessIO::ResetBoard(int i) {
1727
1728 if ((hv->GetHVBoard(i))->Reset(stdout,rbuf,status->Verbose)==1) {
1729 sprintf(str,"software reset done board %d",hv->GetHVBoard(i)->GetBoardNumber());
1730 PrintMessage(str);
1731 UpdateStatus(i,rbuf);
1732 sPrintStatus(status,str,i);
1733 PrintMessage(Chop(str));
1734 }
1735 else {
1736 sprintf(str,"board %d error: could not reset - check timeout and try again",hv->GetHVBoard(i)->GetBoardNumber());
1737 PrintMessage(str);
1738 }
1739
1740}
1741
1742
1743int ProcessIO::SaveHVSettings(char* filename) {
1744
1745 char str[MAX_COM_SIZE];
1746
1747 FILE *fptr;
1748
1749 time_t time_now_secs;
1750 struct tm *time_now;
1751
1752 time(&time_now_secs);
1753 time_now = localtime(&time_now_secs);
1754
1755 if ((fptr = fopen(filename,"w")) == NULL) {
1756 sprintf(str,"error: could not open file \"%s\"\n", filename);
1757 return 0;
1758 }
1759
1760 fprintf(fptr,"\n********** HV settings written by hvutil %s, %04d %02d %02d, %02d:%02d:%02d **********\n\n",
1761 HV_CONTROL_VERSION,
1762 1900 + time_now->tm_year,
1763 1 + time_now->tm_mon,
1764 time_now->tm_mday,
1765 time_now->tm_hour,
1766 time_now->tm_min,
1767 time_now->tm_sec);
1768
1769 for (int i=0;i<status->NumHVBoards;i++) {
1770 fprintf(fptr,"Device: %s\n\n",status->fUSBDevice[i]);
1771
1772 for (int j=0;j<MAX_NUM_CHAINS;j++) {
1773 for (int k=0;k<4;k++) {
1774 for (int l=0;l<8;l++)
1775 fprintf(fptr,"%5d ",status->HV[i][j][k*8+l]);
1776 fprintf(fptr,"\n");
1777 }
1778 fprintf(fptr,"\n");
1779 }
1780 }
1781
1782 fclose(fptr);
1783
1784 return 1;
1785}
1786
1787
1788int ProcessIO::IsBoard(int board) {
1789
1790 for (int i=0;i<MAX_NUM_HVBOARDS;i++)
1791 if (board == status->USBDeviceNumber[i])
1792 return 1;
1793
1794 return 0;
1795}
1796
1797
1798int ProcessIO::GetBoardIdx(int board) {
1799
1800 for (int i=0;i<MAX_NUM_HVBOARDS;i++)
1801 if (board == status->USBDeviceNumber[i])
1802 return i;
1803
1804 return -1;
1805}
Note: See TracBrowser for help on using the repository browser.