Changeset 93 for hvcontrol/src/HV.cc
- Timestamp:
- 07/28/09 09:15:18 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
hvcontrol/src/HV.cc
r90 r93 12 12 13 13 #include "HV.h" 14 14 #include "ProcessIO.h" // Must be not in HV.h to avoid problem with declaring class ProcessIO 15 15 16 16 // Constructor 17 HVBoard::HVBoard(int DeviceNumber, char *DeviceName, bool TestMode, float TimeOut, bool verbose) {17 HVBoard::HVBoard(int DeviceNumber, char *DeviceName, class ProcessIO *PIO) { 18 18 19 19 char Buffer[BUFFER_LENGTH]; 20 20 struct termios tio; 21 21 22 fTestMode = TestMode; 22 m = PIO; 23 24 fTestMode = m->config->TestMode; 23 25 fTestModeWrap = 0; 24 25 SetTimeOut( TimeOut);26 27 SetTimeOut(m->config->fTimeOut); 26 28 BoardNumber = DeviceNumber; 27 29 BoardName = DeviceName; … … 42 44 snprintf(Buffer, BUFFER_LENGTH, "/dev/%s",DeviceName); 43 45 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)); 45 47 return; 46 48 } … … 48 50 // Get current serial port settings 49 51 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)); 51 53 return; 52 54 } 53 55 54 56 // 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)); 56 58 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)); 58 60 59 61 // Synchronize HV board (if fails, closes device and sets fDescriptor to -2) … … 62 64 63 65 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(); 66 68 return; 67 69 } … … 77 79 HVBoard::~HVBoard() { 78 80 if(fDescriptor >= 0 && !fTestMode) { 79 Reset( stdout, false);81 Reset(); 80 82 close(fDescriptor); 81 83 } … … 86 88 // 87 89 // Returns: 0 read error, 1 success, -1 fTimeOut exceeded 88 int HVBoard::Communicate( FILE* fptr, unsigned char* wbuf, int Bytes, bool verbose) {90 int HVBoard::Communicate(unsigned char* wbuf, int Bytes) { 89 91 90 92 unsigned char rbuf; … … 94 96 // === Write data === 95 97 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]); 102 104 } 103 105 … … 106 108 struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)}; 107 109 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)); 109 111 return 0; 110 112 } 111 113 // Time-out expired? 112 114 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); 114 116 return -1; 115 117 } 116 118 // Read error? 117 119 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)); 119 121 return 0; 120 122 } … … 129 131 ResetButton = (bool) (rbuf & 0X80); 130 132 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); 132 134 133 135 return 1; … … 136 138 137 139 /* Reset HV board - uses TRead() and has same return values */ 138 int HVBoard::Reset( FILE* fptr, bool verbose) {140 int HVBoard::Reset() { 139 141 140 142 if (fTestMode) return 1; … … 143 145 int ret; 144 146 145 if((ret = Communicate( fptr, wbuf, 3, verbose)) == 1) ClearVoltageArrays();147 if((ret = Communicate(wbuf, 3)) == 1) ClearVoltageArrays(); 146 148 return ret; 147 149 } … … 149 151 150 152 /* Read status register - uses TRead() and has same return values */ 151 int HVBoard::GetStatus( FILE* fptr, bool verbose) {153 int HVBoard::GetStatus() { 152 154 153 155 if (fTestMode) return 1; … … 155 157 unsigned char wbuf[] = {REG_STATUS,0,0}; 156 158 157 return Communicate( fptr, wbuf, 3, verbose);159 return Communicate(wbuf, 3); 158 160 } 159 161 160 162 161 163 /* 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) {164 int HVBoard::SetHV(int chain, unsigned int channel, unsigned int hv) { 163 165 164 166 if (fTestMode){ 165 printf("Test mode: Nothing to be done. \n");167 m->PrintMessage("Test mode: Nothing to be done. \n"); 166 168 return 1; 167 169 } … … 170 172 171 173 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"); 173 175 return 0; 174 176 } … … 180 182 case 3: wbuf[0] = REG_HV3; break; 181 183 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; 183 185 } 184 186 … … 189 191 wbuf[2] |= (unsigned char)(hv & 0X000000FF); // Add [D7-D0] 190 192 191 return Communicate( fptr, wbuf, 3, verbose);193 return Communicate(wbuf, 3); 192 194 } 193 195
Note:
See TracChangeset
for help on using the changeset viewer.