X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/blobdiff_plain/713be7a43ed11947c358fc67c11b9df7b0be204c..759a1836af8a88a8eff08c731a894e380c829f02:/scope.c diff --git a/scope.c b/scope.c index 3152243..eab7750 100644 --- a/scope.c +++ b/scope.c @@ -1,52 +1,52 @@ -#include #include #include #include +#include +#include +#include -#include "usbtmc.h" #include "scope.h" +#include "usbtmc.h" /* Just USB for now... */ int sendscpi(struct scope* sc, char* cmd, unsigned char *resp, int resplen) { - return usbtmc_sendscpi(sc->usbdev, cmd, resp, resplen); + return usbtmc_sendscpi(sc, cmd, resp, resplen); } void closescope(struct scope* sc) { - return usbtmc_close(sc->usbdev); + return usbtmc_close(sc); } void claimscope(struct scope* sc) { - return usbtmc_claim(sc->usbdev); + return usbtmc_claim(sc); } void releasescope(struct scope* sc) { - //Disable keylock, so the user doesn't have to press the 'force'-button + /* Disable keylock, so the user doesn't have to press the 'force'-button */ sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); - return usbtmc_release(sc->usbdev); + return usbtmc_release(sc); +} + +void resetscope(struct scope* sc) +{ + return usbtmc_reset(sc); } struct scope* initscope(void) { - struct usb_dev_handle *usbdev; struct scope *sc; - usbdev = usbtmc_initscope(); - - if (!usbdev) - return NULL; + sc = usbtmc_initscope(); - sc = calloc(1, sizeof(struct scope)); - if (sc == NULL) { - perror("malloc"); + if (!sc) { + printf("No scope found.\n"); exit(EXIT_FAILURE); } - sc->usbdev = usbdev; - claimscope(sc); sendscpi(sc, "*IDN?", (unsigned char*)sc->idn, sizeof(sc->idn)); releasescope(sc); @@ -60,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+1); + 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; +}