]> git.zerfleddert.de Git - rigol/blobdiff - scope.c
fix off by one
[rigol] / scope.c
diff --git a/scope.c b/scope.c
index 806ba3475ca2f417479df1272c6180d29a9e285d..eab7750c64c76fa8dd3dbb76354f2a8d03e74d64 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -1,54 +1,52 @@
-#include <usb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <stdint.h>
+#include <string.h>
+#include <strings.h>
 
-#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();
+       sc = usbtmc_initscope();
 
-       if (!usbdev) {
+       if (!sc) {
                printf("No scope found.\n");
                exit(EXIT_FAILURE);
        }
 
-       sc = calloc(1, sizeof(struct scope));
-       if (sc == NULL) {
-               perror("malloc");
-               exit(EXIT_FAILURE);
-       }
-
-       sc->usbdev = usbdev;
-
        claimscope(sc);
        sendscpi(sc, "*IDN?", (unsigned char*)sc->idn, sizeof(sc->idn)); 
        releasescope(sc);
@@ -62,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