X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/blobdiff_plain/7d42ad7efe4c26e33498016c9f49acd214b5428e..78fb09842aa3a0b87a4fe84a1ab93bfc7252d934:/scope.c diff --git a/scope.c b/scope.c index c316ee5..30f175a 100644 --- a/scope.c +++ b/scope.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "scope.h" #include "usbtmc.h" @@ -58,3 +60,119 @@ char *scope_idn(struct scope *sc) { return sc->idn; } + +#define COPY_SCOPE_STRING(sc, cmd, dst) { \ + char *buf; \ + buf = scope_get_string(sc, cmd, sizeof(dst)); \ + if (buf) { \ + strcpy(dst, buf); \ + free(buf); \ + }\ + } + +char *scope_get_string(struct scope *sc, char *cmd, int maxlen) +{ + unsigned char *buf; + int res; + + buf = malloc(maxlen); + if (buf == NULL) { + perror("malloc(scope_get_strings)"); + exit(EXIT_FAILURE); + } + + res = sendscpi(sc, cmd, buf, maxlen); + if (res < 0) { + fprintf(stderr, "Command %s failed with %d\n", cmd, res); + free(buf); + return NULL; + } + + buf[res] = 0; + + return (char*)buf; +} + +int scope_get_truth_value(struct scope *sc, char *cmd) +{ + char buf[128]; + int res; + + bzero(buf, sizeof(buf)); + res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1); + if (res < 0) { + fprintf(stderr, "Command %s failed with %d\n", cmd, res); + return 0; + } + + printf("%s %s\n", cmd, buf); + + if (strcasecmp(buf, "on") == 0) { + return 1; + } else if (strcasecmp(buf, "enable") == 0) { + return 1; + } + + return 0; +} + +int scope_get_int(struct scope *sc, char *cmd) +{ + char buf[128]; + int res; + + bzero(buf, sizeof(buf)); + res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1); + if (res < 0) { + fprintf(stderr, "Command %s failed with %d\n", cmd, res); + return 0; + } + + return atoi(buf); +} + +double scope_get_double(struct scope *sc, char*cmd) +{ + char buf[128]; + int res; + double ret; + + bzero(buf, sizeof(buf)); + res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1); + if (res < 0) { + fprintf(stderr, "Command %s failed with %d\n", cmd, res); + return 0.0; + } + + ret = strtod(buf, NULL); + + return ret; +} + +int update_scope_status(struct scope *sc) +{ + bzero(&(sc->status), sizeof(sc->status)); + + COPY_SCOPE_STRING(sc, ":INFO:LANG?", sc->status.system.lang); + + sc->status.system.counter_enabled = scope_get_truth_value(sc, ":COUN:ENAB?"); + sc->status.system.beep_enabled = scope_get_truth_value(sc, ":BEEP:ENAB?"); + + sc->status.keyboard.key_lock = scope_get_truth_value(sc, ":KEY:LOCK?"); + + COPY_SCOPE_STRING(sc, ":ACQ:TYPE?", sc->status.acquire.type); + COPY_SCOPE_STRING(sc, ":ACQ:MODE?", sc->status.acquire.mode); + + sc->status.acquire.averages = scope_get_int(sc, ":ACQ:AVER?"); + sc->status.acquire.srate_chan1 = scope_get_double(sc, ":ACQ:SAMP? CHAN1"); + sc->status.acquire.srate_chan2 = scope_get_double(sc, ":ACQ:SAMP? CHAN2"); + sc->status.acquire.srate_digital = scope_get_double(sc, ":ACQ:SAMP? DIGITAL"); + + COPY_SCOPE_STRING(sc, ":TIM:MODE?", sc->status.timebase.mode); + sc->status.timebase.offset = scope_get_double(sc, ":TIM:OFFS?"); + sc->status.timebase.delayed_offset = scope_get_double(sc, ":TIM:DEL:OFFS?"); + sc->status.timebase.scale = scope_get_double(sc, ":TIM:SCAL?"); + COPY_SCOPE_STRING(sc, ":TIM:FORM?", sc->status.timebase.format); + + return 0; +}