source: hvcontrol/src/ProcessIO.cc@ 79

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