]> git.zerfleddert.de Git - rigol/blobdiff - scope.c
fix off by one
[rigol] / scope.c
diff --git a/scope.c b/scope.c
index c316ee527e3f021eb52c6130d958fe6f6b8b85e8..eab7750c64c76fa8dd3dbb76354f2a8d03e74d64 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdint.h>
+#include <string.h>
+#include <strings.h>
 
 #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+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;
+}
Impressum, Datenschutz