FACT++  1.0
void ConnectionBias::HandleReceivedData ( const vector< uint8_t > &  buf,
size_t  bytes_received,
int  command,
int  send_counter 
)
inlineprivate

Definition at line 325 of file biasctrl.cc.

References Error(), i, kBold, BIAS::kCmdChannelSet, BIAS::kCmdGlobalSet, BIAS::kCmdRead, BIAS::kCmdReset, BIAS::kExpertChannelSet, kNumChannels, BIAS::kNumChannelsPerBoard, BIAS::kResetChannels, BIAS::kSynchronize, BIAS::kUpdate, and Dim::SendCommandNB().

326  {
327 #ifdef DEBUG
328  ofstream fout("received.txt", ios::app);
329  fout << Time() << ": ";
330  for (unsigned int i=0; i<bytes_received; i++)
331  fout << hex << setfill('0') << setw(2) << (uint16_t)buf[i];
332  fout << endl;
333 #endif
334 
335  // Now print the received message if requested by the user
336  if (fIsVerbose/* && command!=kUpdate*/)
337  {
338  Out() << endl << kBold << dec << "Data received (size=" << bytes_received << "):" << endl;
339  Out() << " Command=" << command << " fWrapCounter=" << fWrapCounter << " fSendCounter=" << fSendCounter << " fIsInitializing=" << fIsInitializing << " fIsRamping=" << fIsRamping;
340  Out() << hex << setfill('0');
341 
342  for (size_t i=0; i<bytes_received/3; i++)
343  {
344  if (i%8==0)
345  Out() << '\n' << setw(2) << bytes_received/24 << "| ";
346 
347  Out() << setw(2) << uint16_t(buf[i*3+2]);
348  Out() << setw(2) << uint16_t(buf[i*3+1]);
349  Out() << setw(2) << uint16_t(buf[i*3+0]) << " ";
350  }
351  Out() << endl;
352  }
353 
354  const int cmd = command&0xf;
355 
356  // Check the number of received_byted according to the answer expected
357  if ((cmd==kSynchronize && !CheckMessageLength(bytes_received, 3, "Synchronization")) ||
358  (cmd==kCmdReset && !CheckMessageLength(bytes_received, 3, "CmdReset")) ||
359  (cmd==kCmdRead && !CheckMessageLength(bytes_received, 3*kNumChannels, "CmdRead")) ||
360  (cmd==kCmdChannelSet && !CheckMessageLength(bytes_received, 3*kNumChannels, "CmdChannelSet")) ||
361  (cmd==kExpertChannelSet && !CheckMessageLength(bytes_received, 3, "CmdExpertChannelSet")))
362  {
363  CloseImp(-1);
364  return;
365  }
366 
367  // Now evaluate the whole bunch of messages
368  for (size_t i=0; i<bytes_received/3; i++)
369  {
370  if (!EvalAnswer(buf.data(), i, command))
371  {
373  return;
374  }
375  }
376 
377  if (command==kSynchronize)
378  {
379  Message("Stream successfully synchronized.");
380  fIsInitializing = 2;
381 
382  // Cancel sending of the next 0
383  fSyncTimer.cancel();
384  fCounter[0]++;
385 
386  // Start continous reading of all channels
387  ScheduleUpdate(100);
388  return;
389  }
390 
391  if (send_counter%8 != fWrapCounter)
392  {
393  ostringstream msg;
394  msg << "Corrupted answer: received wrap counter " << fWrapCounter << " is not send counter " << send_counter << "%8.";
395  Error(msg);
396 
398  }
399 
400 
401  // Check if new values have been received
402  if (cmd==kCmdRead || cmd==kCmdChannelSet || cmd==kExpertChannelSet)
403  UpdateVA();
404 
405  // ----- Take action depending on what is going on -----
406 
407  if (command==kCmdReset)
408  {
409  Message("Reset command successfully answered...");
410 
411  fCounter[1]++;
412 
413  // Re-start cyclic reading of values after a short time
414  // to allow the currents to become stable. This ensures that
415  // we get an update soon but wait long enough to get reasonable
416  // values
417  fUpdateTimer.cancel();
418 
419  if (fUpdateTime==0)
420  ReadAllChannels(true);
421  else
422  {
423  Message("...restarting automatic readout.");
424  ScheduleUpdate(100);
425  }
426  }
427 
428  if (command==kResetChannels)
429  {
430  ExpertReset(false);
431  fCounter[5]++;
432  }
433 
434  if (command==kUpdate)
435  {
437  fCounter[2]++;
438  }
439 
440  // If we are ramping, schedule a new ramp step
441  if (command==kCmdChannelSet && fIsRamping)
442  {
443  bool oc = false;
444  for (int ch=0; ch<kNumChannels; ch++)
445  if (fPresent[ch/kNumChannelsPerBoard] && fCurrent[ch]<0)
446  oc = true;
447 
448  if (oc)
449  {
450  if (!fEmergencyShutdown)
451  {
452  Warn("OverCurrent detected - emergency ramp down initiated.");
453  Dim::SendCommandNB("MCP/STOP");
454  RampAllDacs(0);
455  fEmergencyShutdown = true;
456  }
457  }
458  else
460 
461  fCounter[3]++;
462  }
463 
464  if (command==kCmdRead)
465  fCounter[4]++;
466 
467  if ((command&0xff)==kExpertChannelSet)
468  fCounter[6]++;
469 
470  if (command==kCmdGlobalSet)
471  fCounter[7]++;
472  }
vector< uint64_t > fCounter
Definition: biasctrl.cc:63
vector< int16_t > fCurrent
Definition: biasctrl.cc:72
virtual void UpdateVA()
Definition: biasctrl.cc:83
void ScheduleRampStep()
Definition: biasctrl.cc:846
void CloseImp(int64_t delay=0)
int64_t fWrapCounter
Definition: biasctrl.cc:47
int i
Definition: db_dim_client.c:21
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
bool fEmergencyShutdown
Definition: biasctrl.cc:68
bool RampAllDacs(uint16_t dac)
Definition: biasctrl.cc:970
int64_t fSendCounter
Definition: biasctrl.cc:48
bool fIsVerbose
Definition: biasctrl.cc:42
void DelayedReconnect()
Definition: biasctrl.cc:307
int Error(const std::string &str)
Definition: MessageImp.h:49
void SendCommandNB(const std::string &command)
Definition: Dim.h:30
int Warn(const std::string &str)
Definition: MessageImp.h:48
bool fIsRamping
Definition: biasctrl.cc:60
int fIsInitializing
Definition: biasctrl.cc:59
void ScheduleUpdate(int millisec)
Definition: biasctrl.cc:676
uint32_t fUpdateTime
Definition: biasctrl.cc:55
boost::asio::deadline_timer fUpdateTimer
Definition: biasctrl.cc:36
int Message(const std::string &str)
Definition: MessageImp.h:46
vector< bool > fPresent
Definition: biasctrl.cc:45
void ExpertReset(bool expert_mode=true)
Definition: biasctrl.cc:1226
std::ostream & Out()
Definition: ConnectionUSB.h:47
bool CheckMessageLength(int received, int expected, const string &msg)
Definition: biasctrl.cc:134
bool EvalAnswer(const uint8_t *answer, uint16_t id, int command)
Definition: biasctrl.cc:146
void ReadAllChannels(bool special=false)
Definition: biasctrl.cc:1124
boost::asio::deadline_timer fSyncTimer
Definition: biasctrl.cc:34
Set attribute Bold.
Definition: WindowLog.h:36

+ Here is the call graph for this function: