]>
cvs.zerfleddert.de Git - rigol/blob - scope.c
11 #define DATASIZE 10240
13 #define COPY_SCOPE_STRING(sc, cmd, dst) { \
15 buf = scope_get_string(sc, cmd, sizeof(dst)); \
22 /* Just USB for now... */
23 int sendscpi(struct scope
* sc
, char* cmd
, unsigned char *resp
, int resplen
)
25 return usbtmc_sendscpi(sc
, cmd
, resp
, resplen
);
28 void closescope(struct scope
* sc
)
30 return usbtmc_close(sc
);
33 void claimscope(struct scope
* sc
)
35 return usbtmc_claim(sc
);
38 void releasescope(struct scope
* sc
)
40 /* Disable keylock, so the user doesn't have to press the 'force'-button */
41 sendscpi(sc
, ":KEY:LOCK DISABLE",NULL
,0);
42 return usbtmc_release(sc
);
45 void resetscope(struct scope
* sc
)
47 return usbtmc_reset(sc
);
50 struct scope
* initscope(void)
54 sc
= usbtmc_initscope();
57 printf("No scope found.\n");
62 COPY_SCOPE_STRING(sc
, "*IDN?", sc
->idn
);
65 printf("Scope found (%s)\n", sc
->idn
);
70 char *scope_idn(struct scope
*sc
)
75 char *scope_get_string(struct scope
*sc
, char *cmd
, int maxlen
)
80 buf
= malloc(maxlen
+1);
82 perror("malloc(scope_get_strings)");
86 res
= sendscpi(sc
, cmd
, buf
, maxlen
);
88 fprintf(stderr
, "Command %s failed with %d\n", cmd
, res
);
98 int scope_get_truth_value(struct scope
*sc
, char *cmd
)
103 bzero(buf
, sizeof(buf
));
104 res
= sendscpi(sc
, cmd
, (unsigned char*)buf
, sizeof(buf
)-1);
106 fprintf(stderr
, "Command %s failed with %d\n", cmd
, res
);
110 if (strcasecmp(buf
, "on") == 0) {
112 } else if (strcasecmp(buf
, "enable") == 0) {
119 int scope_get_int(struct scope
*sc
, char *cmd
)
124 bzero(buf
, sizeof(buf
));
125 res
= sendscpi(sc
, cmd
, (unsigned char*)buf
, sizeof(buf
)-1);
127 fprintf(stderr
, "Command %s failed with %d\n", cmd
, res
);
134 double scope_get_double(struct scope
*sc
, char*cmd
)
140 bzero(buf
, sizeof(buf
));
141 res
= sendscpi(sc
, cmd
, (unsigned char*)buf
, sizeof(buf
)-1);
143 fprintf(stderr
, "Command %s failed with %d\n", cmd
, res
);
147 ret
= strtod(buf
, NULL
);
152 char *scope_get_data(struct scope
*sc
, char *source
, int *len
)
157 if ((data
= malloc(DATASIZE
)) == NULL
) {
162 snprintf(cmd
, sizeof(cmd
), ":WAV:DATA? %s", source
);
163 *len
= sendscpi(sc
, cmd
, (unsigned char*)data
, DATASIZE
);
168 void update_scope_measurements(struct scope
*sc
)
170 sc
->status
.measure
.ch1
.vpp
= scope_get_double(sc
, ":MEAS:VPP? CHAN1");
171 sc
->status
.measure
.ch1
.vmax
= scope_get_double(sc
, ":MEAS:VMAX? CHAN1");
172 sc
->status
.measure
.ch1
.vmin
= scope_get_double(sc
, ":MEAS:VMIN? CHAN1");
173 sc
->status
.measure
.ch1
.vamplitude
= scope_get_double(sc
, ":MEAS:VAMP? CHAN1");
174 sc
->status
.measure
.ch1
.vtop
= scope_get_double(sc
, ":MEAS:VTOP? CHAN1");
175 sc
->status
.measure
.ch1
.vbase
= scope_get_double(sc
, ":MEAS:VBAS? CHAN1");
176 sc
->status
.measure
.ch1
.vaverage
= scope_get_double(sc
, ":MEAS:VAV? CHAN1");
177 sc
->status
.measure
.ch1
.vrms
= scope_get_double(sc
, ":MEAS:VRMS? CHAN1");
178 sc
->status
.measure
.ch1
.overshoot
= scope_get_double(sc
, ":MEAS:OVER? CHAN1");
179 sc
->status
.measure
.ch1
.preshoot
= scope_get_double(sc
, ":MEAS:PRES? CHAN1");
180 sc
->status
.measure
.ch1
.frequency
= scope_get_double(sc
, ":MEAS:FREQ? CHAN1");
181 sc
->status
.measure
.ch1
.risetime
= scope_get_double(sc
, ":MEAS:RIS? CHAN1");
182 sc
->status
.measure
.ch1
.falltime
= scope_get_double(sc
, ":MEAS:FALL? CHAN1");
183 sc
->status
.measure
.ch1
.period
= scope_get_double(sc
, ":MEAS:PER? CHAN1");
184 sc
->status
.measure
.ch1
.pwidth
= scope_get_double(sc
, ":MEAS:PWID? CHAN1");
185 sc
->status
.measure
.ch1
.nwidth
= scope_get_double(sc
, ":MEAS:NWID? CHAN1");
186 sc
->status
.measure
.ch1
.pdutycycle
= scope_get_double(sc
, ":MEAS:PDUT? CHAN1");
187 sc
->status
.measure
.ch1
.ndutycycle
= scope_get_double(sc
, ":MEAS:NDUT? CHAN1");
188 sc
->status
.measure
.ch1
.pdelay
= scope_get_double(sc
, ":MEAS:PDEL? CHAN1");
189 sc
->status
.measure
.ch1
.ndelay
= scope_get_double(sc
, ":MEAS:NDEL? CHAN1");
191 sc
->status
.measure
.ch2
.vpp
= scope_get_double(sc
, ":MEAS:VPP? CHAN2");
192 sc
->status
.measure
.ch2
.vmax
= scope_get_double(sc
, ":MEAS:VMAX? CHAN2");
193 sc
->status
.measure
.ch2
.vmin
= scope_get_double(sc
, ":MEAS:VMIN? CHAN2");
194 sc
->status
.measure
.ch2
.vamplitude
= scope_get_double(sc
, ":MEAS:VAMP? CHAN2");
195 sc
->status
.measure
.ch2
.vtop
= scope_get_double(sc
, ":MEAS:VTOP? CHAN2");
196 sc
->status
.measure
.ch2
.vbase
= scope_get_double(sc
, ":MEAS:VBAS? CHAN2");
197 sc
->status
.measure
.ch2
.vaverage
= scope_get_double(sc
, ":MEAS:VAV? CHAN2");
198 sc
->status
.measure
.ch2
.vrms
= scope_get_double(sc
, ":MEAS:VRMS? CHAN2");
199 sc
->status
.measure
.ch2
.overshoot
= scope_get_double(sc
, ":MEAS:OVER? CHAN2");
200 sc
->status
.measure
.ch2
.preshoot
= scope_get_double(sc
, ":MEAS:PRES? CHAN2");
201 sc
->status
.measure
.ch2
.frequency
= scope_get_double(sc
, ":MEAS:FREQ? CHAN2");
202 sc
->status
.measure
.ch2
.risetime
= scope_get_double(sc
, ":MEAS:RIS? CHAN2");
203 sc
->status
.measure
.ch2
.falltime
= scope_get_double(sc
, ":MEAS:FALL? CHAN2");
204 sc
->status
.measure
.ch2
.period
= scope_get_double(sc
, ":MEAS:PER? CHAN2");
205 sc
->status
.measure
.ch2
.pwidth
= scope_get_double(sc
, ":MEAS:PWID? CHAN2");
206 sc
->status
.measure
.ch2
.nwidth
= scope_get_double(sc
, ":MEAS:NWID? CHAN2");
207 sc
->status
.measure
.ch2
.pdutycycle
= scope_get_double(sc
, ":MEAS:PDUT? CHAN2");
208 sc
->status
.measure
.ch2
.ndutycycle
= scope_get_double(sc
, ":MEAS:NDUT? CHAN2");
209 sc
->status
.measure
.ch2
.pdelay
= scope_get_double(sc
, ":MEAS:PDEL? CHAN2");
210 sc
->status
.measure
.ch2
.ndelay
= scope_get_double(sc
, ":MEAS:NDEL? CHAN2");
212 sc
->status
.measure
.total
= scope_get_truth_value(sc
, ":MEAS:TOT?");
213 COPY_SCOPE_STRING(sc
, ":MEAS:SOUR?", sc
->status
.measure
.source
);
217 void update_scope_channel(struct scope
*sc
, int channel
)
219 struct channel_s
*ch
;
224 ch
= &(sc
->status
.channel
.ch1
);
225 strcpy(cmd
, ":CHAN1:");
226 } else if (channel
== 2) {
227 ch
= &(sc
->status
.channel
.ch2
);
228 strcpy(cmd
, ":CHAN2:");
230 fprintf(stderr
, "Unknown channel %d!\n", channel
);
236 strcpy(cmd
+ offs
, "BWL?"); ch
->bwlimit_enabled
= scope_get_truth_value(sc
, cmd
);
237 strcpy(cmd
+ offs
, "COUP?"); COPY_SCOPE_STRING(sc
, cmd
, ch
->coupling
);
238 strcpy(cmd
+ offs
, "DISP?"); ch
->displayed
= scope_get_truth_value(sc
, cmd
);
239 strcpy(cmd
+ offs
, "INV?"); ch
->inverted
= scope_get_truth_value(sc
, cmd
);
240 strcpy(cmd
+ offs
, "OFFS?"); ch
->offset
= scope_get_double(sc
, cmd
);
241 strcpy(cmd
+ offs
, "PROB?"); ch
->probe
= scope_get_double(sc
, cmd
);
242 strcpy(cmd
+ offs
, "SCAL?"); ch
->scale
= scope_get_double(sc
, cmd
);
243 strcpy(cmd
+ offs
, "FILT?"); ch
->filter_enabled
= scope_get_truth_value(sc
, cmd
);
244 strcpy(cmd
+ offs
, "MEMD?"); ch
->memory_depth
= scope_get_int(sc
, cmd
);
245 strcpy(cmd
+ offs
, "VERN?"); COPY_SCOPE_STRING(sc
, cmd
, ch
->vernier
);
248 int update_scope_status(struct scope
*sc
)
250 bzero(&(sc
->status
), sizeof(sc
->status
));
252 COPY_SCOPE_STRING(sc
, ":INFO:LANG?", sc
->status
.system
.lang
);
254 sc
->status
.system
.counter_enabled
= scope_get_truth_value(sc
, ":COUN:ENAB?");
255 sc
->status
.system
.beep_enabled
= scope_get_truth_value(sc
, ":BEEP:ENAB?");
257 sc
->status
.keyboard
.key_lock
= scope_get_truth_value(sc
, ":KEY:LOCK?");
259 update_scope_measurements(sc
);
261 COPY_SCOPE_STRING(sc
, ":DISP:TYPE?", sc
->status
.display
.type
);
262 COPY_SCOPE_STRING(sc
, ":DISP:GRID?", sc
->status
.display
.grid
);
263 sc
->status
.display
.persist
= scope_get_truth_value(sc
, ":DISP:PERS?");
264 COPY_SCOPE_STRING(sc
, ":DISP:MNUD?", sc
->status
.display
.mnudisplay
);
265 sc
->status
.display
.mnustatus
= scope_get_truth_value(sc
, ":DISP:MNUS?");
266 COPY_SCOPE_STRING(sc
, ":DISP:SCR?", sc
->status
.display
.screen
);
267 sc
->status
.display
.brightness
= scope_get_int(sc
, ":DISP:BRIG?");
268 sc
->status
.display
.intensity
= scope_get_int(sc
, ":DISP:INT?");
270 update_scope_channel(sc
, 1);
271 update_scope_channel(sc
, 2);
273 COPY_SCOPE_STRING(sc
, ":ACQ:TYPE?", sc
->status
.acquire
.type
);
274 COPY_SCOPE_STRING(sc
, ":ACQ:MODE?", sc
->status
.acquire
.mode
);
276 sc
->status
.acquire
.averages
= scope_get_int(sc
, ":ACQ:AVER?");
277 sc
->status
.acquire
.srate_ch1
= scope_get_double(sc
, ":ACQ:SAMP? CHAN1");
278 sc
->status
.acquire
.srate_ch2
= scope_get_double(sc
, ":ACQ:SAMP? CHAN2");
279 sc
->status
.acquire
.srate_digital
= scope_get_double(sc
, ":ACQ:SAMP? DIGITAL");
281 COPY_SCOPE_STRING(sc
, ":TIM:MODE?", sc
->status
.timebase
.mode
);
282 sc
->status
.timebase
.offset
= scope_get_double(sc
, ":TIM:OFFS?");
283 sc
->status
.timebase
.delayed_offset
= scope_get_double(sc
, ":TIM:DEL:OFFS?");
284 sc
->status
.timebase
.scale
= scope_get_double(sc
, ":TIM:SCAL?");
285 COPY_SCOPE_STRING(sc
, ":TIM:FORM?", sc
->status
.timebase
.format
);
287 sc
->status
.math
.displayed
= scope_get_truth_value(sc
, ":MATH:DISP?");
288 sc
->status
.fft
.displayed
= scope_get_truth_value(sc
, ":FFT:DISP?");