forked from hollanderic/dp832
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp832.cpp
151 lines (134 loc) · 3.77 KB
/
dp832.cpp
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
#include <string.h>
#include <cassert>
#include "dp832.h"
#include <unistd.h>
using namespace std;
dp830::dp830(const char *devname) {
fhandle_.open(devname,std::fstream::in | std::fstream::out | std::fstream::app);
}
dp830::~dp830(){fhandle_.close();}
int dp830::write_(const char *command) {
assert(fhandle_);
int len = strlen(command);
fhandle_.write(command,len);
//printf("write: -->%s<--\n",command);
fhandle_.flush();
return len;
}
int dp830::read_(char *buf, int len){
assert(fhandle_);
if (!buf) return 0;
memset(buf,0,len);
fhandle_.getline(buf,len);
return fhandle_.gcount();
}
int dp830::GetState(int channel){
char buf[40];
char buf2[40];
snprintf(buf,sizeof(buf),":OUTP:STAT? CH%d",channel);
write_(buf);
read_(buf2,sizeof(buf2));
return strcmp(buf2,"OFF")?1:0;
}
int dp830::On(int channel) {
char buf[40];
snprintf(buf,sizeof(buf),":SOUR%d:CURR:PROT:CLEAR",channel);
write_(buf);
snprintf(buf,sizeof(buf),":OUTP:STAT CH%d,ON",channel);
return write_(buf);
}
int dp830::AllOn() {
On(1);
On(2);
On(3);
return 0;
}
int dp830::Off(int channel) {
char buf[40];
snprintf(buf,sizeof(buf),":OUTP CH%d,OFF",channel);
return write_(buf);
}
int dp830::AllOff() {
Off(1);
Off(2);
Off(3);
return 0;
}
// Routines for setting operating parameters. Note these are not
// overvoltage/overcurrent protections.
int dp830::SetLimit_(int channel, const char *tag, double limit){
char buf[40];
snprintf(buf,sizeof(buf),":SOUR%d:%s:IMM %6.3f",channel,tag,limit);
return write_(buf);
}
int dp830::SetVoltage(int channel, double limit){
return SetLimit_(channel,"VOLT",limit);
}
int dp830::SetCurrent(int channel, double limit){
return SetLimit_(channel,"CURR",limit);
}
int dp830::SetOCP(int channel, double limit) {
char buf[60];
snprintf(buf,sizeof(buf),":SOUR%d:CURR:PROT:STAT ON",channel);
write_(buf);
snprintf(buf,sizeof(buf),":SOUR%d:CURR:PROT %6.3f",channel,limit);
return write_(buf);
}
double dp830::GetOCP(int channel) {
char buf[40];
snprintf(buf,sizeof(buf),":SOUR%d:CURR:PROT?",channel);
write_(buf);
read_(buf,sizeof(buf));
return std::stod(buf,NULL);
}
int dp830::GetOCPTripped(int channel) {
char buf[40];
snprintf(buf,sizeof(buf),":SOUR%d:CURR:PROT:TRIP?",channel);
write_(buf);
read_(buf,sizeof(buf));
return (strcmp(buf,"YES")==0)?1:0;
}
double dp830::GetSetPoint_(int channel, const char *tag) {
char buf[40];
char buf2[40];
snprintf(buf,sizeof(buf),":SOUR%d:%s?",channel,tag);
write_(buf);
read_(buf2,sizeof(buf2));
return std::stod(buf2,NULL);
}
double dp830::GetVoltageSetPoint(int channel) {
return GetSetPoint_(channel, "VOLT");
}
double dp830::GetCurrentSetPoint(int channel) {
return GetSetPoint_(channel, "CURR");
}
// Measurement routines
double dp830::Measure_(int channel, const char *tag) {
char buf[40];
char buf2[40];
snprintf(buf,sizeof(buf),":MEAS:%s? CH%d",tag,channel);
write_(buf);
read_(buf2,sizeof(buf2));
return std::stod(buf2,NULL);
}
double dp830::MeasureVoltage(int channel) {
return Measure_(channel,"VOLT");
}
double dp830::MeasureCurrent(int channel) {
return Measure_(channel,"CURR");
}
double dp830::MeasurePower(int channel) {
return Measure_(channel,"POWE");
}
//Bounce will set the output to 0.0V for delay seconds
// Set to 0.0 instead of turning the output off so that
// it will present a low impendence and help discharge
// the target (if the circuit is designed in such a manner
// to make this useful)
int dp830::Bounce(int channel, double delay) {
double volt = GetVoltageSetPoint(channel);
SetVoltage(channel, 0.0);
usleep(delay*1e6);
SetVoltage(channel, volt);
return 0;
}