-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiseg.cpp
More file actions
301 lines (252 loc) · 7.81 KB
/
Copy pathiseg.cpp
File metadata and controls
301 lines (252 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Daniel Pitzl, DESY, 16.6.2014
// iseg SHQ 122M via USB and RS232
// ls -ltr /dev
// sudo chmod 666 /dev/ttyUSB0
// or
// sudo chmod 666 /dev/ttyUSB1
#include <iostream> // cout
#include <cstdlib> // atoi
#include <cmath> // pow
#include "iseg.h"
#include "rs232.h"
using namespace std;
//defining the comport number that is collected in a list in rs232.cpp (for linux) or rs232_mac.cpp (additional mac ports)
#ifdef __unix__ // all unices, not all compilers
#define COMPORT_NUMBER 16
#elif __linux__
#define COMPORT_NUMBER 16 // "/dev/ttyUSB0"
#elif __APPLE__
#define COMPORT_NUMBER 30 // "/dev/tty.UC-232AC"
#endif
//------------------------------------------------------------------------------
double outToDouble( char *word )
{
int sign = 1;
int orderSign = 1;
int order = 0;
if( word[0] == '-' ) {
sign = -1;
if( word[6] == '-' )
orderSign = -1;
}
else if( word[5] == '-' )
orderSign = -1;
char *istr;
istr = strtok( &word[0], " +-" );
double value = atof( istr );
while( istr != NULL ) {
order = atoi( istr );
istr = strtok( NULL, " +-" );
}
return ( sign * value * pow( 10, orderSign * order ) );
}
//------------------------------------------------------------------------------
// Constructor: Connects to the device, initializes communication
Iseg::Iseg( )
{
// "S1": Read status
// "D1": New set-voltage
// "G1": Apply new set-voltage
ldb = 1;
iseg_connected = 0;
hv_on = false;
supply_tripped = false;
voltage_set = 0;
const int comPortNumber = COMPORT_NUMBER; /* /dev/ttyUSB0 */
cout << "instatiating an iseg HV supply " << comPortNumber << endl;
if( !openComPort( comPortNumber, 9600 ) ) {
cout << " cannot open RS232 port" << endl;
cout << " CHECK: that comport is chosen in iseg.cpp from the list in rs232{_mac} appropriatly." << endl;
return;
}
cout << " Opened RS232 COM port to iseg device" << endl;
iseg_connected = 1;
char answer[256] = { 0 };
writeCommandAndReadAnswer( "S1", answer );
if( strcmp( answer, "S1=ON" ) != 0 ) {
// Try once more:
writeCommandAndReadAnswer( "S1", answer );
}
handleAnswers( answer );
cout << answer << endl;
if( strcmp( answer, "S1=ON" ) != 0 ) {
cout << " iseg device did not return proper status code!" << endl;
return;
}
cout << " iseg communication initialized" << endl;
hv_on = true;
}
//------------------------------------------------------------------------------
// Destructor: Will turn off the HV and terminate connection to the HV Power Supply device
Iseg::~Iseg( )
{
if( iseg_connected == 0 )
return;
setVoltage( 0 );
closeComPort( ); // Close the COM port
iseg_connected = 0;
}
//------------------------------------------------------------------------------
void Iseg::handleAnswers( char *answer )
{
if( strcmp( answer, "S1=ON" ) == 0 ) {
if( ldb )
cout << " voltage is set" << endl;
}
else if( strcmp( answer, "S1=OFF" ) == 0 ) {
cout << "HV Power Supply set to OFF!" << endl;
iseg_connected = 0;
}
else if( strcmp( answer, "S1=TRP" ) == 0 ) {
cout << "Power supply tripped!" << endl;
supply_tripped = true;
}
else if( strcmp( answer, "S1=ERR" ) == 0 ) {
cout << "Power supply tripped!" << endl;
supply_tripped = true;
}
else if( strcmp( answer, "S1=MAN" ) == 0 ) {
cout << "HV Power Supply set to MANUAL mode!" << endl;
iseg_connected = 0;
}
else if( strcmp( answer, "S1=L2H" ) == 0 ) {
if( ldb )
cout << "Voltage is rising" << endl;
}
else if( strcmp( answer, "S1=H2L" ) == 0 ) {
if( ldb )
cout << "Voltage is falling" << endl;
}
else {
if( ldb )
cout << "Unknown device return value";
supply_tripped = false;
}
}
//------------------------------------------------------------------------------
void Iseg::status( )
{
if( iseg_connected == 0 ) {
cout << "iseg not connected" << endl;
return;
}
char answer[256] = { 0 };
writeCommandAndReadAnswer( "#", answer );
cout << "iseg module " << answer << endl;
writeCommandAndReadAnswer( "S1", answer );
cout << "status " << answer << endl;
writeCommandAndReadAnswer( "W", answer );
cout << "break time " << answer << " ms" << endl;
writeCommandAndReadAnswer( "A1", answer );
cout << "auto start " << answer << endl;
writeCommandAndReadAnswer( "M1", answer );
cout << "voltage limit " << answer << " % of 2000 V" << endl;
writeCommandAndReadAnswer( "V1", answer );
cout << "ramp speed " << answer << " V/s" << endl;
writeCommandAndReadAnswer( "D1", answer );
cout << "voltage set " << answer << " = " << outToDouble( answer ) << " V"
<< endl;
writeCommandAndReadAnswer( "U1", answer );
cout << "voltage meas " << answer << " = " << outToDouble( answer ) << " V"
<< endl;
writeCommandAndReadAnswer( "N1", answer );
cout << "current limit " << answer << " % of range" << endl;
writeCommandAndReadAnswer( "L1", answer );
cout << "current trip " << answer << endl;
writeCommandAndReadAnswer( "LS1", answer );
cout << "current trip " << answer << " = " << outToDouble( answer ) <<
endl;
writeCommandAndReadAnswer( "I1", answer );
cout << "current meas " << answer << " = " << outToDouble( answer ) << " A"
<< endl;
}
//------------------------------------------------------------------------------
// Sets the desired voltage
bool Iseg::setVoltage( double volts )
{
if( iseg_connected == 0 ) {
cout << "iseg not connected" << endl;
return 1; // error
}
// Internally store voltage:
voltage_set = double ( volts );
if( hv_on ) {
if( ldb )
cout << "Setting HV to " << voltage_set << " V" << endl;
char command[256] = { 0 };
char answer[256] = { 0 };
sprintf( &command[0], "D1=%.f", voltage_set );
writeCommandAndReadAnswer( command, answer );
writeCommandAndReadAnswer( "G1", answer );
handleAnswers( answer );
}
else {
cout << "HV not activated" << endl;
}
// FIXME not checking for return codes yet!
return true;
}
//------------------------------------------------------------------------------
// Reads back the configured voltage
double Iseg::getVoltage( )
{
if( iseg_connected == 0 ) {
cout << "iseg not connected" << endl;
return 0;
}
char answer[256] = { 0 };
writeCommandAndReadAnswer( "U1", answer );
return outToDouble( answer );
}
//------------------------------------------------------------------------------
// Reads back the current drawn
double Iseg::getCurrent( )
{
if( iseg_connected == 0 ) {
cout << "iseg not connected" << endl;
return 0;
}
char answer[256] = { 0 };
writeCommandAndReadAnswer( "I1", answer );
return outToDouble( answer );
}
//------------------------------------------------------------------------------
// current limit, in % of range
bool Iseg::setCurrentLimit( int limit )
{
if( iseg_connected == 0 ) {
cout << "iseg not connected" << endl;
return 0;
}
if( limit > 100 )
limit = 100;
cout << "Setting current limit to " << limit << " uA" << endl;
char command[256] = { 0 };
char answer[256] = { 0 };
// Factor 100 is required since this value is the sensitive region,
// not milliamps!
sprintf( &command[0], "LS1=%i", limit );
writeCommandAndReadAnswer( command, answer );
writeCommandAndReadAnswer( "G1", answer );
handleAnswers( answer );
return true;
}
//------------------------------------------------------------------------------
// Read the current limit in compliance mode [uA]
double Iseg::getCurrentLimit( )
{
if( iseg_connected == 0 ) {
cout << "iseg not connected" << endl;
return 0;
}
char answer[256] = { 0 };
writeCommandAndReadAnswer( "LS1", answer );
return outToDouble( answer ) * 1E6; // A to uA
}
//------------------------------------------------------------------------------
bool Iseg::tripped( )
{
if( supply_tripped )
return true;
return false;
}