Changeset 93 for hvcontrol/src/HV.cc


Ignore:
Timestamp:
07/28/09 09:15:18 (15 years ago)
Author:
ogrimm
Message:
Added writing of slow data
File:
1 edited

Legend:

Unmodified
Added
Removed
  • hvcontrol/src/HV.cc

    r90 r93  
    1212
    1313#include "HV.h"
    14 
     14#include "ProcessIO.h" // Must be not in HV.h to avoid problem with declaring class ProcessIO
    1515
    1616// Constructor
    17 HVBoard::HVBoard(int DeviceNumber, char *DeviceName, bool TestMode, float TimeOut, bool verbose) {
     17HVBoard::HVBoard(int DeviceNumber, char *DeviceName, class ProcessIO *PIO) {
    1818   
    1919  char Buffer[BUFFER_LENGTH];
    2020  struct termios tio;
    2121 
    22   fTestMode = TestMode;
     22  m = PIO;
     23
     24  fTestMode = m->config->TestMode;
    2325  fTestModeWrap = 0;
    24 
    25   SetTimeOut(TimeOut);
     26 
     27  SetTimeOut(m->config->fTimeOut);
    2628  BoardNumber = DeviceNumber;
    2729  BoardName = DeviceName;
     
    4244  snprintf(Buffer, BUFFER_LENGTH, "/dev/%s",DeviceName);
    4345  if ((fDescriptor = open(Buffer, O_RDWR|O_NOCTTY|O_NDELAY)) == -1) {
    44     if(errno != 2) printf("Error: Could not open device %d/%s (%s)\n", DeviceNumber,DeviceName, strerror(errno));
     46    if(errno != 2) m->PrintMessage("Error: Could not open device %d/%s (%s)\n", DeviceNumber,DeviceName, strerror(errno));
    4547    return;
    4648  }
     
    4850  // Get current serial port settings
    4951  if (tcgetattr(fDescriptor, &tio) == -1) {
    50     printf("tcgetattr(...) failed (%d/%s)\n", errno, strerror(errno));
     52    m->PrintMessage("Error: tcgetattr() failed (%d/%s)\n", errno, strerror(errno));
    5153    return;   
    5254  }
    5355
    5456  // Set baudrate and raw mode
    55   if (cfsetspeed(&tio, BAUDRATE) == -1) printf("Error: Could not set baud rate (%s)\n", strerror(errno));
     57  if (cfsetspeed(&tio, BAUDRATE) == -1) m->PrintMessage("Error: Could not set baud rate (%s)\n", strerror(errno));
    5658  cfmakeraw(&tio);
    57   if (tcsetattr(fDescriptor, TCSANOW, &tio ) == -1) printf("Errsor with tcsetattr(...) (%s)\n", strerror(errno));
     59  if (tcsetattr(fDescriptor, TCSANOW, &tio ) == -1) m->PrintMessage("Error: tcsetattr() failed (%s)\n", strerror(errno));
    5860
    5961  //  Synchronize HV board (if fails, closes device and sets fDescriptor to -2)
     
    6264 
    6365  while(++trial<=3) {
    64     if((ret = Communicate(stdout, &wbuf, 1, verbose)) == 1) {
    65       Reset(stdout, verbose);
     66    if((ret = Communicate(&wbuf, 1)) == 1) {
     67      Reset();
    6668      return;
    6769    }
     
    7779HVBoard::~HVBoard() {
    7880  if(fDescriptor >= 0 && !fTestMode) {
    79     Reset(stdout, false);
     81    Reset();
    8082    close(fDescriptor);
    8183  }
     
    8688//
    8789// Returns: 0 read error, 1 success, -1 fTimeOut exceeded
    88 int HVBoard::Communicate(FILE* fptr, unsigned char* wbuf, int Bytes, bool verbose) {
     90int HVBoard::Communicate(unsigned char* wbuf, int Bytes) {
    8991
    9092  unsigned char rbuf;
     
    9496  // === Write data ===
    9597  if (write(fDescriptor, wbuf, Bytes) < Bytes) {
    96     fprintf(fptr," Error: could not write (all) data to HV board\n");
    97     return 0;
    98   }
    99   if (verbose) {
    100     fprintf(fptr,"%d bytes written:\n", Bytes);
    101     for (int i=0; i<Bytes; i++) fprintf(fptr," Byte %d: %#.2x\n",i,wbuf[i]);
     98    m->PrintMessage("Error: could not write (all) data to HV board\n");
     99    return 0;
     100  }
     101  if (m->Verbose) {
     102    m->PrintMessage("%d bytes written:\n", Bytes);
     103    for (int i=0; i<Bytes; i++) m->PrintMessage(" Byte %d: %#.2x\n",i,wbuf[i]);
    102104  }
    103105
     
    106108  struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
    107109  if (select(fDescriptor+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
    108     printf("Error with select() (%d/%s)\n", errno,strerror(errno));
     110    m->PrintMessage("Error with select() (%d/%s)\n", errno,strerror(errno));
    109111    return 0;
    110112  }
    111113  // Time-out expired?
    112114  if (!FD_ISSET(fDescriptor, &SelectDescriptor)) {
    113     if (verbose) printf("Time-out of %.2f seconds expired while reading\n", fTimeOut);
     115    if (m->Verbose) m->PrintMessage("Time-out of %.2f seconds expired while reading\n", fTimeOut);
    114116    return -1;
    115117  }
    116118  // Read error?   
    117119  if ((ret = read(fDescriptor, &rbuf, 1)) == -1) {
    118     fprintf(stderr, " Read error (%s)\n", strerror(errno));
     120    m->PrintMessage("Read error (%s)\n", strerror(errno));
    119121    return 0;
    120122  }
     
    129131  ResetButton = (bool) (rbuf & 0X80);
    130132
    131   if (verbose && ret==1) fprintf(fptr," 1 byte read: %#.2x\n", rbuf); 
     133  if (m->Verbose && ret==1) m->PrintMessage(" 1 byte read: %#.2x\n", rbuf); 
    132134 
    133135  return 1;
     
    136138
    137139/* Reset HV board - uses TRead() and has same return values */
    138 int HVBoard::Reset(FILE* fptr, bool verbose) {
     140int HVBoard::Reset() {
    139141 
    140142  if (fTestMode) return 1;
     
    143145  int ret;
    144146 
    145   if((ret = Communicate(fptr, wbuf, 3, verbose)) == 1) ClearVoltageArrays(); 
     147  if((ret = Communicate(wbuf, 3)) == 1) ClearVoltageArrays(); 
    146148  return ret;
    147149}
     
    149151
    150152/* Read status register - uses TRead() and has same return values */
    151 int HVBoard::GetStatus(FILE* fptr, bool verbose) {
     153int HVBoard::GetStatus() {
    152154
    153155  if (fTestMode) return 1;
     
    155157  unsigned char wbuf[] = {REG_STATUS,0,0};
    156158 
    157   return Communicate(fptr, wbuf, 3, verbose);
     159  return Communicate(wbuf, 3);
    158160}
    159161
    160162
    161163/* Set high voltage - uses TRead() and has same return values */
    162 int HVBoard::SetHV(FILE* fptr, int chain, unsigned int channel, unsigned int hv, bool verbose) {
     164int HVBoard::SetHV(int chain, unsigned int channel, unsigned int hv) {
    163165
    164166  if (fTestMode){
    165     printf("Test mode: Nothing to be done. \n");
     167    m->PrintMessage("Test mode: Nothing to be done. \n");
    166168    return 1;
    167169  }
     
    170172
    171173  if (!(hv>=0 && hv<=0x3FFF)) {
    172     fprintf(fptr," Error: HV beyond limits [0 - 0x3FFF]\n");
     174    m->PrintMessage(" Error: HV beyond limits [0 - 0x3FFF]\n");
    173175    return 0;
    174176  }
     
    180182    case 3: wbuf[0] = REG_HV3; break;
    181183
    182     default : fprintf(fptr," Error: chain %d does not exist\n",chain); return 0;
     184    default : m->PrintMessage(" Error: chain %d does not exist\n",chain); return 0;
    183185  }
    184186 
     
    189191  wbuf[2] |= (unsigned char)(hv & 0X000000FF);              // Add [D7-D0]
    190192   
    191   return Communicate(fptr, wbuf, 3, verbose);
     193  return Communicate(wbuf, 3);
    192194}
    193195
Note: See TracChangeset for help on using the changeset viewer.