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 |
|
---|
15 | extern bool Verbose;
|
---|
16 |
|
---|
17 | //
|
---|
18 | // Constructor
|
---|
19 | //
|
---|
20 | HVBoard::HVBoard(int DeviceNumber, char *DeviceName) {
|
---|
21 |
|
---|
22 | char Buffer[MAX_COM_SIZE];
|
---|
23 | struct termios tio;
|
---|
24 |
|
---|
25 | fTimeOut = 10;
|
---|
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 | //
|
---|
54 | HVBoard::~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
|
---|
63 | int 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 | // TODO
|
---|
88 | // this is bad coding here. but if I do not wait here,
|
---|
89 | // the text output of the arduino is not completely send, and only
|
---|
90 | // part of the message is received ..
|
---|
91 | // I would be better to loop over select and red until select does
|
---|
92 | // return after the timeout, the it is clear, that the arduino send all
|
---|
93 | // it wanted to send, and the whole 'Communicate' is rally finished.
|
---|
94 | // but sleeping 2secs is okay for the moment .
|
---|
95 | // TODO
|
---|
96 | sleep (2);
|
---|
97 |
|
---|
98 | // === Try to read until time-out ===
|
---|
99 | FD_ZERO(&SelectDescriptor); FD_SET(fDescriptor, &SelectDescriptor);
|
---|
100 | struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
|
---|
101 | if (select(fDescriptor+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
|
---|
102 | printf("Error with select() (%d/%s)\n", errno,strerror(errno));
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 | // Time-out expired?
|
---|
107 | if (!FD_ISSET(fDescriptor, &SelectDescriptor)) {
|
---|
108 | printf("Time-out of %.2f seconds expired while reading\n", fTimeOut);
|
---|
109 | return -1;
|
---|
110 | }
|
---|
111 |
|
---|
112 | // Read error?
|
---|
113 | if ((ret = read(fDescriptor, rbuf, RBUF_LEN)) == -1) {
|
---|
114 | printf("Read error (%d/%s)\n", errno, strerror(errno));
|
---|
115 | return 0;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // Print result in hexadecimal and binary representation
|
---|
119 | if (Verbose) {
|
---|
120 | printf(" %d byte(s) read: \n", ret);
|
---|
121 | // in case rbuf has no '\0' at its last position, the printf would go berserk!
|
---|
122 | // I put an '\0' at the end of the received bytes
|
---|
123 | rbuf[ret]='\0';
|
---|
124 | // in case the sender has put an '\0' in the first ret bytes, the output with printf is
|
---|
125 | // truncated ... so I go and exchange '\0' for '0'
|
---|
126 | for ( int dom=0; dom<ret ; dom++){
|
---|
127 | if (rbuf[dom]=='\0')
|
---|
128 | rbuf[dom]='0';
|
---|
129 | }
|
---|
130 | printf("%s\n",rbuf);
|
---|
131 | }
|
---|
132 |
|
---|
133 | return 1;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | // System reset of HV board
|
---|
138 | int HVBoard::SystemReset() {
|
---|
139 |
|
---|
140 | unsigned char wbuf[] = {0,0,0};
|
---|
141 | return Communicate(wbuf, 3);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | // Read channel status
|
---|
146 | int HVBoard::ReadChannel(int Chain, int Channel) {
|
---|
147 |
|
---|
148 | unsigned char wbuf[] = {1<<5 | Chain<<1 | (Channel&16)>>4, Channel<<4, 0};
|
---|
149 | return Communicate(wbuf, 3);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | // Global set
|
---|
154 | int HVBoard::GlobalSet(int Voltage) {
|
---|
155 |
|
---|
156 | unsigned char wbuf[] = {1<<6 , Voltage>>8, Voltage};
|
---|
157 | return Communicate(wbuf, 3);
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | // Channel set
|
---|
162 | int HVBoard::ChannelSet(int Chain, int Channel, int Voltage) {
|
---|
163 |
|
---|
164 | unsigned char wbuf[] = {3<<5 | Chain<<1 | (Channel&16)>>4, Channel<<4 | Voltage>>8, Voltage};
|
---|
165 | return Communicate(wbuf, 3);
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | // Synchronize board
|
---|
170 | bool HVBoard::SynchBoard() {
|
---|
171 |
|
---|
172 | unsigned char wbuf = 0;
|
---|
173 | int trial = 0, ret;
|
---|
174 |
|
---|
175 | while(++trial<=3) {
|
---|
176 | if((ret = Communicate(&wbuf, 1)) == 1) return true;
|
---|
177 | if (ret==0) break;
|
---|
178 | }
|
---|
179 | return false;
|
---|
180 | }
|
---|