source: hvcontrol/src/ProcessIO.cc@ 43

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