source: hvcontrol/src/ProcessIO.cc@ 63

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