Ignore:
Timestamp:
02/11/11 10:03:46 (14 years ago)
Author:
ogrimm
Message:
New command 'crate', changed signalling of program exit
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/BIASctrl/User.cc

    r10119 r10153  
    1515                                                                const char *Help;
    1616  } CommandList[] =
    17    {{"synch", &User::cmd_synch, 0, "", "Synchronize board"},
    18     {"pixel", &User::cmd_hv, 2, "<pixel id> <v>", "Change bias of pixel"},
    19         {"channel", &User::cmd_hv, 2, "<channel|all> <v>", "Change bias of (all) channels of active boards"},
    20     {"gs", &User::cmd_gs, 1, "[crate] <volt>", "Global voltage set"},
     17   {{"pixel", &User::cmd_hv, 2, "<pixel id> <v>", "Change bias of pixel"},
     18        {"channel", &User::cmd_hv, 2, "<channel range> <v>", "Change bias of (all) channels of active crate"},
     19    {"gs", &User::cmd_gs, 1, "[crate] <volt>", "Global voltage set active crate"},
     20        {"reset", &User::cmd_reset, 0, "", "Reset active crate"},
     21        {"synch", &User::cmd_synch, 0, "", "Synchronize active crate"}, {"crate", &User::cmd_crate, 1, "<number>", "Select active crate"},
    2122        {"status", &User::cmd_status, 0, "[dac|current]", "Show status information (DAC values if requested)"},
    22         {"mode", &User::cmd_mode, 1, "<static|dynamic>", "Set voltage stabilization mode"},
     23        {"mode", &User::cmd_mode, 1, "<static|dynamic>", "Set voltage stabilization mode (experimental)"},
    2324        {"load", &User::cmd_load, 1, "<file>", "Load and set bias settings from file"},
    2425        {"save", &User::cmd_save, 1, "<file>", "Save current bias settings to file"},
    2526        {"rate", &User::cmd_rate, 1, "<rate>", "Set refresh rate in Hz"},
    2627        {"timeout", &User::cmd_timeout, 1, "<time>", "Set timeout to return from read in seconds"},
    27         {"reset", &User::cmd_reset, 1, "<crates>", "Reset crates"},
    2828        {"help", &User::cmd_help, 0, "", "Print help"},
    2929        {"exit", &User::cmd_exit, 0, "", "Exit program"}};
     
    3434//
    3535User::User(): EvidenceServer(SERVER_NAME) {
     36
     37  MainThread = pthread_self();
    3638
    3739  // DIM console service used in PrintMessage()
     
    6365  }
    6466
     67  // Set active crate
     68  if (!Boards.empty()) ActiveCrate = 0;
     69  else ActiveCrate = -1;
     70
    6571  // Create PixelMap instance (map from config server)
    6672  DimRpcInfo RPC((char *) "ConfigRequest", (char *) "");
     
    7783}
    7884
    79 
    8085//
    8186// Destructor
     
    8489
    8590  // Wait for thread to quit
    86   if (pthread_join(Thread, NULL) != 0) {
    87     PrintMessage("pthread_join() failed");
    88   }
     91  if (pthread_cancel(Thread) != 0) Message(ERROR, "pthread_cancel() failed in ()");
     92  if (pthread_join(Thread, NULL) != 0) Message(ERROR, "pthread_join() failed in ()");
    8993
    9094  // Delete all crates
     
    144148 
    145149  PrintMessage(".<command>                  Execute shell command\n\n"
    146         "Items in <> are mandatory, in [] optional, | indicates mutual exclusive or.\n");
     150        "Items in <> are mandatory, in [] optional, | indicates mutual exclusive or.\n"
     151    "Channel ranges can be 'all', a single number or in the form 'a-b'.\n");
    147152}
    148153
     
    152157void User::cmd_synch() {
    153158
    154   for (unsigned int i=0; i<Crates.size(); i++) {
    155         if (Crates[i]->Synch()) PrintMessage("Synchronized crate %d\n", i);
    156         else PrintMessage("Failed to synchronize crate %d\n", i);
    157   }
     159  if (ActiveCrate == -1) return; 
     160
     161  if (Crates[ActiveCrate]->Synch()) PrintMessage("Synchronized crate %d\n", ActiveCrate);
     162  else PrintMessage("Failed to synchronize crate %d\n", ActiveCrate);
     163}
     164
     165//
     166// Select active crate
     167//
     168void User::cmd_crate() {
     169
     170  int Crate;
     171
     172  if (!ConvertToInt(Parameter[1], &Crate)) {
     173     PrintMessage("Error: Wrong number format\n");
     174     return;   
     175  }
     176
     177  // Check limits
     178  if (Crate<0 || Crate>=(int) Crates.size()) {
     179    PrintMessage("Crate number %d not existing\n", Crate);
     180    return;
     181  }
     182
     183  ActiveCrate = Crate;
    158184}
    159185
     
    183209        // Channel identification
    184210        else {
    185       vector<string> T = Tokenize(Parameter[n], "/");
    186           Crt.Min = 0;
    187           Crt.Max = Crates.size()-1;
     211          if (ActiveCrate == -1) continue;
     212          else  Crt.Min = Crt.Max = ActiveCrate;
     213
    188214          Chan.Min = 0;
    189           Chan.Max = MAX_NUM_BOARDS*NUM_CHANNELS-1;
    190          
    191           if (T.size() == 2) {
    192                 if(!ConvertToRange(T[0], Crt) || !ConvertToRange(T[1], Chan)) {
    193                   PrintMessage("Numeric conversion or out-of-range error for parameter %d, skipping channel\n", n);
    194                   continue;
    195                 }
    196           }
    197           else {
    198                 Crt.Min = Crt.Max = 0;
    199                 if (!ConvertToRange(T[0], Chan)) {
    200                   PrintMessage("Numeric conversion or out-of-range error for parameter %d, skipping channel\n", n);
    201                   continue;
    202                 }
     215          Chan.Max = MAX_NUM_BOARDS*NUM_CHANNELS-1;       
     216
     217          if (!ConvertToRange(Parameter[n], Chan)) {
     218                PrintMessage("Numeric conversion or out-of-range error for parameter %d, skipping channel\n", n);
     219                continue;
    203220          }
    204221        }
     
    246263  char Buffer[MAX_COM_SIZE];
    247264  int Errors = 0, Channel;
    248   unsigned int NBoards = 0;
    249265  double Value;
    250266  FILE *File;
     
    261277    for (unsigned int Crate=0; Crate<Crates.size(); Crate++) if (Match(Crates[Crate]->Name, Buffer)) {
    262278
    263           PrintMessage("Found bias settings for board %s (#%d)\n\r", Crates[Crate]->Name, Crate);
     279          if ((int) Crate != ActiveCrate) continue;
     280          PrintMessage("Found bias settings for crate %s (#%d)\n\r", Crates[Crate]->Name, Crate);
    264281
    265282          Voltages.clear();
     
    280297          }
    281298          else PrintMessage("\nFinished updating board\n");
    282       NBoards++;
    283299    } // Loop over boards
    284300  } // while()
    285301           
    286   if (NBoards != Crates.size()) PrintMessage("Warning: Could not load bias settings for all connected crates\n");
    287   else if (Errors == 0) PrintMessage("Success: Read bias settings for all connected crates\n");
    288 
    289302  if (Errors != 0) PrintMessage("Warning: %d error(s) occurred\n", Errors);
    290 
    291303  if (fclose(File) != 0) PrintMessage("Error: Could not close file '%s'\n", Parameter[1].c_str());
    292304}
     
    319331void User::cmd_reset() {
    320332
    321   struct Range R = {0, Crates.size()-1};
    322 
    323   // Check ranges 
    324   if(!ConvertToRange(Parameter[1], R)) {
    325         PrintMessage("Error, crate number out of range\n");
     333  if (ActiveCrate == -1) return; 
     334 
     335  if (Crates[ActiveCrate]->SystemReset() == 1) PrintMessage("System reset of crate %d\n", ActiveCrate);
     336  else PrintMessage("Error: Could not reset crate %d\n", ActiveCrate);
     337
     338}
     339
     340//
     341// Read channel
     342//
     343void User::cmd_gs() {
     344
     345  double Voltage;
     346
     347  if (ActiveCrate == -1) return; 
     348
     349  if (!ConvertToDouble(Parameter[1], &Voltage)) {
     350    PrintMessage("Error: Wrong number format\n");
    326351        return;
    327352  }
    328353 
    329   for (int i=R.Min; i<=R.Max; i++) {
    330         if (Crates[i]->SystemReset() == 1) PrintMessage("System reset of crate %s (#%d)\n", Crates[i]->Name, i);
    331         else PrintMessage("Error: Could not reset board %s (#%d)\n", Crates[i]->Name, i);
    332   }
    333 }
    334 
    335 //
    336 // Read channel
    337 //
    338 void User::cmd_gs() {
    339 
    340   double Voltage;
    341 
    342   if (!ConvertToDouble(Parameter[1], &Voltage)) return;
    343 
    344   for (unsigned int i=0; i<Crates.size(); i++) {
    345         if (Crates[i]->GlobalSet(Voltage) != 1) {
    346           PrintMessage("Error: Could not global set crate %d\n", i);
    347         }
    348   }   
     354  if (Crates[ActiveCrate]->GlobalSet(Voltage) != 1) {
     355        PrintMessage("Error: Could not global set crate %d\n", ActiveCrate);
     356  } 
    349357}
    350358
     
    396404
    397405  PrintMessage(" Number of crates:  %d\n", Crates.size());
     406  PrintMessage(" Active crate:     %d\n", ActiveCrate); 
    398407  PrintMessage(" Refresh rate:      %.2f Hz\n", fStatusRefreshRate);
    399408  PrintMessage(" Time out:          %.2f s\n\n", fTimeOut);
     
    437446   
    438447//
    439 // Exit program
     448// Exit program (Signal makes readline return and sets ExitRequest)
    440449//
    441450void User::cmd_exit() {
    442451
    443   ExitRequest = true;
    444   pthread_kill(Thread, SIGUSR1);  // Make tjread return from usleep()
     452  pthread_kill(MainThread, SIGTERM);
    445453}
    446454 
     
    512520
    513521  static bool Warned = false;
     522  int Ret;
    514523
    515524  while (!ExitRequest) {
     525
     526        // Wait (this is the only allowed cancelation point)
     527        if ((Ret=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) != 0) {
     528          Message(FATAL, "pthread_setcancelstate() failed (%s)", strerror(Ret));
     529        }
     530        if (usleep((unsigned long) floor(1000000./fStatusRefreshRate)) == -1) {
     531          Message(FATAL, "usleep() failed (%s)", strerror(errno));
     532        }
     533        if ((Ret=pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL)) != 0) {
     534          Message(FATAL, "pthread_setcancelstate() failed (%s)", strerror(Ret));
     535        }
     536
     537        // Check all crates
    516538        for (unsigned int i=0; i<Crates.size(); i++) {
    517539      if (Crates[i]->ErrorCount > MAX_ERR_COUNT) {
    518540        if (!Warned) {
    519541          Warned = true;
    520           Message(WARN, "Warning: Crate %d has many read/write errors, further error reporting disabled", i);
     542          Message(WARN, "Crate %d has many read/write errors, further error reporting disabled", i);
    521543        }
    522544        continue;
     
    553575          if (Mode == mode_dynamic) Crates[i]->AdaptVoltages();
    554576        } // for
    555 
    556         // Wait
    557         usleep((unsigned long) floor(1000000./fStatusRefreshRate));
    558577  } // while
    559578}
Note: See TracChangeset for help on using the changeset viewer.