source: fact/tools/vchvtest/HV.cc@ 17055

Last change on this file since 17055 was 202, checked in by ogrimm, 15 years ago
Improved test command
  • Property svn:executable set to *
File size: 5.0 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
15extern bool Verbose;
16
17//
18// Constructor
19//
20HVBoard::HVBoard(int DeviceNumber, char *DeviceName) {
21
22 char Buffer[MAX_COM_SIZE];
23 struct termios tio;
24
25 fTimeOut = 1;
26 BoardNumber = DeviceNumber;
27 BoardName = DeviceName;
28 LastWrapCount = -1;
29
30 // Open device
31 snprintf(Buffer, MAX_COM_SIZE, "/dev/%s",DeviceName);
32 if ((fDescriptor = open(Buffer, O_RDWR|O_NOCTTY|O_NDELAY)) == -1) {
33 printf("Error: Could not open device #%d: %s (%d/%s)\n", DeviceNumber, DeviceName, errno, strerror(errno));
34 return;
35 }
36
37 // Get current serial port settings
38 if (tcgetattr(fDescriptor, &tio) == -1) {
39 printf("Error: tcgetattr() failed on device #%d: %s (%d/%s)\n", DeviceNumber, DeviceName, errno, strerror(errno));
40 return;
41 }
42
43 // Set baudrate and raw mode
44 if (cfsetspeed(&tio, BAUDRATE) == -1) printf("Error: Could not set baud rate of device #%d: %s (%d/%s)\n", DeviceNumber, DeviceName, errno, strerror(errno));
45 cfmakeraw(&tio);
46 if (tcsetattr(fDescriptor, TCSANOW, &tio ) == -1) printf("Error: tcsetattr() failed on device #%d: %s (%d/%s)\n", DeviceNumber, DeviceName, errno, strerror(errno));
47
48 return;
49}
50
51//
52// Destructor
53//
54HVBoard::~HVBoard() {
55
56 if(fDescriptor != -1) close(fDescriptor);
57}
58
59
60// Communicate: Write and read from HV Board until fTimeOut has been reached
61//
62// Returns: 0 error, 1 success, -1 fTimeOut exceeded
63int HVBoard::Communicate(unsigned char* wbuf, int Bytes) {
64
65 unsigned char rbuf[RBUF_LEN];
66 int ret;
67 fd_set SelectDescriptor;
68
69 Status.BoardNumber = -1;
70
71 // === Write data ===
72 ret = write(fDescriptor, wbuf, Bytes);
73 if (ret == -1) {
74 printf("Error: Could not write data (%d/%s)\n", errno, strerror(errno));
75 return 0;
76 }
77 else if (ret < Bytes) {
78 printf("Error: Could write only %d of %d bytes\n", ret, Bytes);
79 return 0;
80 }
81 if (Verbose) {
82 printf(" %d byte(s) written: ", Bytes);
83 for (int i=0; i<Bytes; i++) printf(" %#.2x", wbuf[i]);
84 printf("\n");
85 }
86
87 // === Try to read until time-out ===
88 FD_ZERO(&SelectDescriptor); FD_SET(fDescriptor, &SelectDescriptor);
89 struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
90 if (select(fDescriptor+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
91 printf("Error with select() (%d/%s)\n", errno,strerror(errno));
92 return 0;
93 }
94
95 // Time-out expired?
96 if (!FD_ISSET(fDescriptor, &SelectDescriptor)) {
97 printf("Time-out of %.2f seconds expired while reading\n", fTimeOut);
98 return -1;
99 }
100
101 // Read error?
102 if ((ret = read(fDescriptor, rbuf, RBUF_LEN)) == -1) {
103 printf("Read error (%d/%s)\n", errno, strerror(errno));
104 return 0;
105 }
106 // Check wrap counter
107 if (LastWrapCount != -1) {
108 if ((LastWrapCount+1)%8 != (rbuf[0] & 0x70)>>4) {
109 printf("Warning: Wrap counter mismatch.\n");
110 }
111 }
112 LastWrapCount = (rbuf[0] & 0x70)>>4;
113
114 // Print result in hexadecimal and binary representation
115 if (Verbose) {
116 printf(" %d byte(s) read: ", ret);
117 for (int i=0; i<ret; i++) printf(" %#.2x ", rbuf[i]);
118 printf(" ");
119 for (int i=0; i<ret; i++) {
120 for (int j=7; j>=0; j--) {
121 printf("%c", (rbuf[i] & 1<<j)!=0 ? '1' : '0');
122 if (j == 4) printf(" ");
123 }
124 printf(" ");
125 }
126 printf("\n");
127 }
128
129 // Decode result if three bytes were returned
130 if (ret == 3) {
131 Status.BoardNumber = rbuf[2] & 0xf;
132 Status.Current = rbuf[1] + (rbuf[0]&15)*256;
133 Status.Overcurrent = rbuf[0] & 128;
134 Status.WrapCounter = (rbuf[0]>>4) & 7;
135 Status.Reset = rbuf[2] & 128;
136 Status.Acknowledge = !((rbuf[2] & 0x70) == 0x70);
137
138 if (Verbose) {
139 printf(" *** Board %d *** Current: %d\n"
140 "Overcurrent bit: %s Wrap counter: %d Reset bit: %s Status: %s\n",
141 Status.BoardNumber, Status.Current, Status.Overcurrent ? "set":"clear",
142 Status.WrapCounter, Status.Reset==false ? "clear": "set",
143 Status.Acknowledge ? "OK" : "No response");
144 }
145 }
146
147 return 1;
148}
149
150
151// System reset of HV board
152int HVBoard::SystemReset() {
153
154 unsigned char wbuf[] = {0,0,0};
155 return Communicate(wbuf, 3);
156}
157
158
159// Read channel status
160int HVBoard::ReadChannel(int Chain, int Channel) {
161
162 unsigned char wbuf[] = {1<<5 | Chain<<1 | (Channel&16)>>4, Channel<<4, 0};
163 return Communicate(wbuf, 3);
164}
165
166
167// Global set
168int HVBoard::GlobalSet(int Voltage) {
169
170 unsigned char wbuf[] = {1<<6 , Voltage>>8, Voltage};
171 return Communicate(wbuf, 3);
172}
173
174
175// Channel set
176int HVBoard::ChannelSet(int Chain, int Channel, int Voltage) {
177
178 unsigned char wbuf[] = {3<<5 | Chain<<1 | (Channel&16)>>4, Channel<<4 | Voltage>>8, Voltage};
179 return Communicate(wbuf, 3);
180}
181
182
183// Synchronize board
184bool HVBoard::SynchBoard() {
185
186 unsigned char wbuf = 0;
187 int trial = 0, ret;
188
189 while(++trial<=3) {
190 if((ret = Communicate(&wbuf, 1)) == 1) return true;
191 if (ret==0) break;
192 }
193 return false;
194}
Note: See TracBrowser for help on using the repository browser.