]>
git.zerfleddert.de Git - rigol/blob - scope.c
43439ff9219284b3299e7d903a262cd2f5b4ad63
11 /* Just USB for now... */
12 int sendscpi(struct scope
* sc
, char* cmd
, unsigned char *resp
, int resplen
)
14 return usbtmc_sendscpi(sc
, cmd
, resp
, resplen
);
17 void closescope(struct scope
* sc
)
19 return usbtmc_close(sc
);
22 void claimscope(struct scope
* sc
)
24 return usbtmc_claim(sc
);
27 void releasescope(struct scope
* sc
)
29 /* Disable keylock, so the user doesn't have to press the 'force'-button */
30 sendscpi(sc
, ":KEY:LOCK DISABLE",NULL
,0);
31 return usbtmc_release(sc
);
34 void resetscope(struct scope
* sc
)
36 return usbtmc_reset(sc
);
39 struct scope
* initscope(void)
43 sc
= usbtmc_initscope();
46 printf("No scope found.\n");
51 sendscpi(sc
, "*IDN?", (unsigned char*)sc
->idn
, sizeof(sc
->idn
));
54 printf("Scope found (%s)\n", sc
->idn
);
59 char *scope_idn(struct scope
*sc
)
64 #define COPY_SCOPE_STRING(sc, cmd, dst) { \
66 buf = scope_get_string(sc, cmd, sizeof(dst)); \
73 char *scope_get_string(struct scope
*sc
, char *cmd
, int maxlen
)
78 buf
= malloc(maxlen
+1);
80 perror("malloc(scope_get_strings)");
84 res
= sendscpi(sc
, cmd
, buf
, maxlen
);
86 fprintf(stderr
, "Command %s failed with %d\n", cmd
, res
);
96 int scope_get_truth_value(struct scope
*sc
, char *cmd
)
101 bzero(buf
, sizeof(buf
));
102 res
= sendscpi(sc
, cmd
, (unsigned char*)buf
, sizeof(buf
)-1);
104 fprintf(stderr
, "Command %s failed with %d\n", cmd
, res
);
108 if (strcasecmp(buf
, "on") == 0) {
110 } else if (strcasecmp(buf
, "enable") == 0) {
117 int scope_get_int(struct scope
*sc
, char *cmd
)
122 bzero(buf
, sizeof(buf
));
123 res
= sendscpi(sc
, cmd
, (unsigned char*)buf
, sizeof(buf
)-1);
125 fprintf(stderr
, "Command %s failed with %d\n", cmd
, res
);
132 double scope_get_double(struct scope
*sc
, char*cmd
)
138 bzero(buf
, sizeof(buf
));
139 res
= sendscpi(sc
, cmd
, (unsigned char*)buf
, sizeof(buf
)-1);
141 fprintf(stderr
, "Command %s failed with %d\n", cmd
, res
);
145 ret
= strtod(buf
, NULL
);
150 void update_scope_measurements(struct scope
*sc
)
152 sc
->status
.measure
.ch1
.vpp
= scope_get_double(sc
, ":MEAS:VPP? CHAN1");
153 sc
->status
.measure
.ch1
.vmax
= scope_get_double(sc
, ":MEAS:VMAX? CHAN1");
154 sc
->status
.measure
.ch1
.vmin
= scope_get_double(sc
, ":MEAS:VMIN? CHAN1");
155 sc
->status
.measure
.ch1
.vamplitude
= scope_get_double(sc
, ":MEAS:VAMP? CHAN1");
156 sc
->status
.measure
.ch1
.vtop
= scope_get_double(sc
, ":MEAS:VTOP? CHAN1");
157 sc
->status
.measure
.ch1
.vbase
= scope_get_double(sc
, ":MEAS:VBAS? CHAN1");
158 sc
->status
.measure
.ch1
.vaverage
= scope_get_double(sc
, ":MEAS:VAV? CHAN1");
159 sc
->status
.measure
.ch1
.vrms
= scope_get_double(sc
, ":MEAS:VRMS? CHAN1");
160 sc
->status
.measure
.ch1
.overshoot
= scope_get_double(sc
, ":MEAS:OVER? CHAN1");
161 sc
->status
.measure
.ch1
.preshoot
= scope_get_double(sc
, ":MEAS:PRES? CHAN1");
162 sc
->status
.measure
.ch1
.frequency
= scope_get_double(sc
, ":MEAS:FREQ? CHAN1");
163 sc
->status
.measure
.ch1
.risetime
= scope_get_double(sc
, ":MEAS:RIS? CHAN1");
164 sc
->status
.measure
.ch1
.falltime
= scope_get_double(sc
, ":MEAS:FALL? CHAN1");
165 sc
->status
.measure
.ch1
.period
= scope_get_double(sc
, ":MEAS:PER? CHAN1");
166 sc
->status
.measure
.ch1
.pwidth
= scope_get_double(sc
, ":MEAS:PWID? CHAN1");
167 sc
->status
.measure
.ch1
.nwidth
= scope_get_double(sc
, ":MEAS:NWID? CHAN1");
168 sc
->status
.measure
.ch1
.pdutycycle
= scope_get_double(sc
, ":MEAS:PDUT? CHAN1");
169 sc
->status
.measure
.ch1
.ndutycycle
= scope_get_double(sc
, ":MEAS:NDUT? CHAN1");
170 sc
->status
.measure
.ch1
.pdelay
= scope_get_double(sc
, ":MEAS:PDEL? CHAN1");
171 sc
->status
.measure
.ch1
.ndelay
= scope_get_double(sc
, ":MEAS:NDEL? CHAN1");
173 sc
->status
.measure
.ch2
.vpp
= scope_get_double(sc
, ":MEAS:VPP? CHAN2");
174 sc
->status
.measure
.ch2
.vmax
= scope_get_double(sc
, ":MEAS:VMAX? CHAN2");
175 sc
->status
.measure
.ch2
.vmin
= scope_get_double(sc
, ":MEAS:VMIN? CHAN2");
176 sc
->status
.measure
.ch2
.vamplitude
= scope_get_double(sc
, ":MEAS:VAMP? CHAN2");
177 sc
->status
.measure
.ch2
.vtop
= scope_get_double(sc
, ":MEAS:VTOP? CHAN2");
178 sc
->status
.measure
.ch2
.vbase
= scope_get_double(sc
, ":MEAS:VBAS? CHAN2");
179 sc
->status
.measure
.ch2
.vaverage
= scope_get_double(sc
, ":MEAS:VAV? CHAN2");
180 sc
->status
.measure
.ch2
.vrms
= scope_get_double(sc
, ":MEAS:VRMS? CHAN2");
181 sc
->status
.measure
.ch2
.overshoot
= scope_get_double(sc
, ":MEAS:OVER? CHAN2");
182 sc
->status
.measure
.ch2
.preshoot
= scope_get_double(sc
, ":MEAS:PRES? CHAN2");
183 sc
->status
.measure
.ch2
.frequency
= scope_get_double(sc
, ":MEAS:FREQ? CHAN2");
184 sc
->status
.measure
.ch2
.risetime
= scope_get_double(sc
, ":MEAS:RIS? CHAN2");
185 sc
->status
.measure
.ch2
.falltime
= scope_get_double(sc
, ":MEAS:FALL? CHAN2");
186 sc
->status
.measure
.ch2
.period
= scope_get_double(sc
, ":MEAS:PER? CHAN2");
187 sc
->status
.measure
.ch2
.pwidth
= scope_get_double(sc
, ":MEAS:PWID? CHAN2");
188 sc
->status
.measure
.ch2
.nwidth
= scope_get_double(sc
, ":MEAS:NWID? CHAN2");
189 sc
->status
.measure
.ch2
.pdutycycle
= scope_get_double(sc
, ":MEAS:PDUT? CHAN2");
190 sc
->status
.measure
.ch2
.ndutycycle
= scope_get_double(sc
, ":MEAS:NDUT? CHAN2");
191 sc
->status
.measure
.ch2
.pdelay
= scope_get_double(sc
, ":MEAS:PDEL? CHAN2");
192 sc
->status
.measure
.ch2
.ndelay
= scope_get_double(sc
, ":MEAS:NDEL? CHAN2");
194 sc
->status
.measure
.total
= scope_get_truth_value(sc
, ":MEAS:TOT?");
195 COPY_SCOPE_STRING(sc
, ":MEAS:SOUR?", sc
->status
.measure
.source
);
199 void update_scope_channel(struct scope
*sc
, int channel
)
201 struct channel_s
*ch
;
206 ch
= &(sc
->status
.channel
.ch1
);
207 strcpy(cmd
, ":CHAN1:");
208 } else if (channel
== 2) {
209 ch
= &(sc
->status
.channel
.ch2
);
210 strcpy(cmd
, ":CHAN2:");
212 fprintf(stderr
, "Unknown channel %d!\n", channel
);
218 strcpy(cmd
+ offs
, "BWL?"); ch
->bwlimit_enabled
= scope_get_truth_value(sc
, cmd
);
219 strcpy(cmd
+ offs
, "COUP?"); COPY_SCOPE_STRING(sc
, cmd
, ch
->coupling
);
220 strcpy(cmd
+ offs
, "DISP?"); ch
->displayed
= scope_get_truth_value(sc
, cmd
);
221 strcpy(cmd
+ offs
, "INV?"); ch
->inverted
= scope_get_truth_value(sc
, cmd
);
222 strcpy(cmd
+ offs
, "OFFS?"); ch
->offset
= scope_get_double(sc
, cmd
);
223 strcpy(cmd
+ offs
, "PROB?"); ch
->probe
= scope_get_double(sc
, cmd
);
224 strcpy(cmd
+ offs
, "SCAL?"); ch
->scale
= scope_get_double(sc
, cmd
);
225 strcpy(cmd
+ offs
, "FILT?"); ch
->filter_enabled
= scope_get_truth_value(sc
, cmd
);
226 strcpy(cmd
+ offs
, "MEMD?"); ch
->memory_depth
= scope_get_int(sc
, cmd
);
227 strcpy(cmd
+ offs
, "VERN?"); COPY_SCOPE_STRING(sc
, cmd
, ch
->vernier
);
230 int update_scope_status(struct scope
*sc
)
232 bzero(&(sc
->status
), sizeof(sc
->status
));
234 COPY_SCOPE_STRING(sc
, ":INFO:LANG?", sc
->status
.system
.lang
);
236 sc
->status
.system
.counter_enabled
= scope_get_truth_value(sc
, ":COUN:ENAB?");
237 sc
->status
.system
.beep_enabled
= scope_get_truth_value(sc
, ":BEEP:ENAB?");
239 sc
->status
.keyboard
.key_lock
= scope_get_truth_value(sc
, ":KEY:LOCK?");
241 update_scope_measurements(sc
);
243 COPY_SCOPE_STRING(sc
, ":DISP:TYPE?", sc
->status
.display
.type
);
244 COPY_SCOPE_STRING(sc
, ":DISP:GRID?", sc
->status
.display
.grid
);
245 sc
->status
.display
.persist
= scope_get_truth_value(sc
, ":DISP:PERS?");
246 COPY_SCOPE_STRING(sc
, ":DISP:MNUD?", sc
->status
.display
.mnudisplay
);
247 sc
->status
.display
.mnustatus
= scope_get_truth_value(sc
, ":DISP:MNUS?");
248 COPY_SCOPE_STRING(sc
, ":DISP:SCR?", sc
->status
.display
.screen
);
249 sc
->status
.display
.brightness
= scope_get_int(sc
, ":DISP:BRIG?");
250 sc
->status
.display
.intensity
= scope_get_int(sc
, ":DISP:INT?");
252 update_scope_channel(sc
, 1);
253 update_scope_channel(sc
, 2);
255 COPY_SCOPE_STRING(sc
, ":ACQ:TYPE?", sc
->status
.acquire
.type
);
256 COPY_SCOPE_STRING(sc
, ":ACQ:MODE?", sc
->status
.acquire
.mode
);
258 sc
->status
.acquire
.averages
= scope_get_int(sc
, ":ACQ:AVER?");
259 sc
->status
.acquire
.srate_ch1
= scope_get_double(sc
, ":ACQ:SAMP? CHAN1");
260 sc
->status
.acquire
.srate_ch2
= scope_get_double(sc
, ":ACQ:SAMP? CHAN2");
261 sc
->status
.acquire
.srate_digital
= scope_get_double(sc
, ":ACQ:SAMP? DIGITAL");
263 COPY_SCOPE_STRING(sc
, ":TIM:MODE?", sc
->status
.timebase
.mode
);
264 sc
->status
.timebase
.offset
= scope_get_double(sc
, ":TIM:OFFS?");
265 sc
->status
.timebase
.delayed_offset
= scope_get_double(sc
, ":TIM:DEL:OFFS?");
266 sc
->status
.timebase
.scale
= scope_get_double(sc
, ":TIM:SCAL?");
267 COPY_SCOPE_STRING(sc
, ":TIM:FORM?", sc
->status
.timebase
.format
);
269 sc
->status
.math
.displayed
= scope_get_truth_value(sc
, ":MATH:DISP?");
270 sc
->status
.fft
.displayed
= scope_get_truth_value(sc
, ":FFT:DISP?");