source: hvcontrol/src/ProcessIO.cc@ 89

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