source: hvcontrol/src/HV.cc@ 229

Last change on this file since 229 was 229, checked in by ogrimm, 15 years ago
Config requests non-blocking if not made from main thread, adapted all servers to GetConfig() returning std::string, workaround for erroneous SERVICE_LIST
File size: 5.7 KB
Line 
1
2/********************************************************************\
3
4 Name: HV.cc
5
6 Created by: Sebastian Commichau, November 2008
7 commichau@phys.ethz.ch
8
9 Contents: Main class for HV supply
10
11\********************************************************************/
12
13#include "HV.h"
14#include "ProcessIO.h" // Must be not in HV.h to avoid problem with declaring class ProcessIO
15
16using namespace std;
17
18// Constructor
19HVBoard::HVBoard(int DeviceNumber, string Name, class ProcessIO *PIO) {
20
21 struct termios tio;
22
23 m = PIO;
24 SetTimeOut(m->fTimeOut);
25 BoardNumber = DeviceNumber;
26 BoardName = new char [Name.size()+1];
27 strcpy(BoardName, Name.c_str());
28
29 stringstream Buf;
30 Buf << setw(2) << BoardNumber;
31 string Result =Buf.str();
32
33 // Create DIM services
34 NameService = new DimService ((SERVER_NAME"/NAME/ID"+Result).c_str(), BoardName);
35 BiasVolt = new DimService ((char *) (SERVER_NAME"/VOLT/ID"+Result).c_str(), (char *) "D", HVV, NUM_CHAINS*NUM_CHANNELS*sizeof(double));
36
37 for (int i=0; i<NUM_CHAINS; i++) Overcurrent[i] = false;
38 ResetButton = false;
39 WrapOK = true;
40 LastWrapCount = -1;
41 ErrorCount = 0;
42
43 ClearVoltageArrays();
44
45 if ((fDescriptor = open(("/dev/"+Name).c_str(), O_RDWR|O_NOCTTY|O_NDELAY)) == -1) {
46 if(errno != 2) m->PrintMessage(All, "Error: Could not open device %d/%s (%s)\n", DeviceNumber, Name.c_str(), strerror(errno));
47 return;
48 }
49
50 // Get current serial port settings
51 if (tcgetattr(fDescriptor, &tio) == -1) {
52 m->PrintMessage(All, "Error: tcgetattr() failed (%d/%s)\n", errno, strerror(errno));
53 return;
54 }
55
56 // Set baudrate and raw mode
57 if (cfsetspeed(&tio, BAUDRATE) == -1) m->PrintMessage(All, "Error: Could not set baud rate (%s)\n", strerror(errno));
58 cfmakeraw(&tio);
59 if (tcsetattr(fDescriptor, TCSANOW, &tio ) == -1) m->PrintMessage(All, "Error: tcsetattr() failed (%s)\n", strerror(errno));
60
61 // Synchronize HV board (if fails, closes device and sets fDescriptor to -2)
62 unsigned char wbuf = REG_STATUS;
63 int trial = 0, ret;
64
65 while(++trial<=3) {
66 if((ret = Communicate(&wbuf, 1)) == 1) {
67 Reset();
68 return;
69 }
70 if (ret==0) break;
71 wbuf = 0;
72 }
73 close(fDescriptor);
74 fDescriptor = -2;
75 return;
76}
77
78// Destructor (Resetting board)
79HVBoard::~HVBoard() {
80 if(fDescriptor >= 0) {
81 Reset();
82 close(fDescriptor);
83 }
84
85 delete NameService;
86 delete BiasVolt;
87 delete[] BoardName;
88}
89
90
91// Communicate: Write and read from HV Board until fTimeOut has been reached
92//
93// Returns: 0 error, 1 success, -1 fTimeOut exceeded
94int HVBoard::Communicate(unsigned char* wbuf, int Bytes) {
95
96 unsigned char rbuf;
97 int ret;
98 fd_set SelectDescriptor;
99
100 // === Write data ===
101 if ((ret=write(fDescriptor, wbuf, Bytes)) < Bytes) {
102 if (ret == -1) m->PrintMessage(All, "Could not write data to HV board (%s)\n", strerror(errno));
103 else m->PrintMessage(All, "Could write only %d of %d bytes to HV board\n", ret, Bytes);
104 ErrorCount++;
105 return 0;
106 }
107
108 // === Try to read until time-out ===
109 FD_ZERO(&SelectDescriptor); FD_SET(fDescriptor, &SelectDescriptor);
110 struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
111 if (select(fDescriptor+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
112 m->PrintMessage(All, "Error with select() (%s)\n", strerror(errno));
113 return 0;
114 }
115 // Time-out expired?
116 if (!FD_ISSET(fDescriptor, &SelectDescriptor)) return -1;
117
118 // Read error?
119 if ((ret = read(fDescriptor, &rbuf, 1)) == -1) {
120 m->PrintMessage(All, "Read error (%s)\n", strerror(errno));
121 ErrorCount++;
122 return 0;
123 }
124
125 // === Update status information ===
126 if (LastWrapCount != -1) {
127 if ((LastWrapCount+1)%8 == (rbuf & 0X07)) WrapOK = true;
128 else WrapOK = false;
129 }
130 LastWrapCount = rbuf & 0X07;
131 for (int i=0; i<NUM_CHAINS; i++) Overcurrent[i] = (rbuf & (0X08 << i));
132 ResetButton = (bool) (rbuf & 0X80);
133
134 return 1;
135}
136
137
138/* Reset HV board - uses TRead() and has same return values */
139int HVBoard::Reset() {
140
141 unsigned char wbuf[] = {REG_RESET,0,0};
142 int ret;
143
144 if((ret = Communicate(wbuf, 3)) == 1) {
145 ClearVoltageArrays();
146 ErrorCount = 0;
147 }
148 return ret;
149}
150
151
152/* Read status register - uses TRead() and has same return values */
153int HVBoard::GetStatus() {
154
155 unsigned char wbuf[] = {REG_STATUS,0,0};
156
157 return Communicate(wbuf, 3);
158}
159
160
161/* Set high voltage - uses TRead() and has same return values */
162int HVBoard::SetHV(int chain, unsigned int channel, int hv) {
163
164 unsigned char wbuf[] = {0,0,0};
165 int ret;
166
167 if (hv<0 || hv > 0x3FFF) {
168 m->PrintMessage("Error: Voltage out of range 0-0x3FFF\n");
169 return 0;
170 }
171
172 switch (chain) {
173 case 0: wbuf[0] = REG_HV0; break;
174 case 1: wbuf[0] = REG_HV1; break;
175 case 2: wbuf[0] = REG_HV2; break;
176 case 3: wbuf[0] = REG_HV3; break;
177
178 default : m->PrintMessage(" Error: Chain %d does not exist\n",chain); return 0;
179 }
180
181 // Assemble bytes
182 wbuf[0] |= (unsigned char)((channel >> 2) & 0X00000007); // Add address [A4-A3]
183 wbuf[1] |= (unsigned char)((hv >> 8) & 0X000000FF); // Add [D13-D8]
184 wbuf[1] |= (unsigned char)((channel << 6) & 0X000000C0); // Add [A1-A0]
185 wbuf[2] |= (unsigned char)(hv & 0X000000FF); // Add [D7-D0]
186
187 if ((ret = Communicate(wbuf, 3)) == 1) HV[chain][channel] = hv;
188 return ret;
189}
190
191
192// Set all voltages of board to zero
193void HVBoard::ClearVoltageArrays() {
194
195 for (int j=0; j<NUM_CHAINS; j++) {
196 for (int k=0; k<NUM_CHANNELS; k++){
197 HV[j][k] = 0;
198 HVV[j][k] = 0.0;
199 }
200 }
201 // Update DIM services
202 BiasVolt->updateService();
203}
204
205
206// Set time-out to wait for read
207void HVBoard::SetTimeOut(double t) {
208
209 if (t >= MIN_TIMEOUT && t <= MAX_TIMEOUT) fTimeOut = t;
210 else fTimeOut = 1.0;
211}
Note: See TracBrowser for help on using the repository browser.