source: fact/tools/hvMCUtest/HV.cc@ 12139

Last change on this file since 12139 was 12139, checked in by neise, 13 years ago
initial commit
  • Property svn:executable set to *
File size: 4.1 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 = 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//
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 sleep (2);
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
107 // Print result in hexadecimal and binary representation
108 if (Verbose) {
109 printf(" %d byte(s) read: \n", ret);
110 // in case rbuf has no '\0' at its last position, the printf would go berserk!
111 // I put an '\0' at the end of the received bytes
112 rbuf[ret]='\0';
113 printf("%s\n",rbuf);
114 }
115
116 return 1;
117}
118
119
120// System reset of HV board
121int HVBoard::SystemReset() {
122
123 unsigned char wbuf[] = {0,0,0};
124 return Communicate(wbuf, 3);
125}
126
127
128// Read channel status
129int HVBoard::ReadChannel(int Chain, int Channel) {
130
131 unsigned char wbuf[] = {1<<5 | Chain<<1 | (Channel&16)>>4, Channel<<4, 0};
132 return Communicate(wbuf, 3);
133}
134
135
136// Global set
137int HVBoard::GlobalSet(int Voltage) {
138
139 unsigned char wbuf[] = {1<<6 , Voltage>>8, Voltage};
140 return Communicate(wbuf, 3);
141}
142
143
144// Channel set
145int HVBoard::ChannelSet(int Chain, int Channel, int Voltage) {
146
147 unsigned char wbuf[] = {3<<5 | Chain<<1 | (Channel&16)>>4, Channel<<4 | Voltage>>8, Voltage};
148 return Communicate(wbuf, 3);
149}
150
151
152// Synchronize board
153bool HVBoard::SynchBoard() {
154
155 unsigned char wbuf = 0;
156 int trial = 0, ret;
157
158 while(++trial<=3) {
159 if((ret = Communicate(&wbuf, 1)) == 1) return true;
160 if (ret==0) break;
161 }
162 return false;
163}
Note: See TracBrowser for help on using the repository browser.