]> git.zerfleddert.de Git - rigol/commitdiff
usbtmc now uses struct scope
authorMichael Gernoth <michael@gernoth.net>
Mon, 7 Jun 2010 12:24:57 +0000 (14:24 +0200)
committerMichael Gernoth <michael@gernoth.net>
Mon, 7 Jun 2010 12:24:57 +0000 (14:24 +0200)
scope.c
scope.h
usbtmc.c
usbtmc.h

diff --git a/scope.c b/scope.c
index 862f32b396e1d93007ec084b6718d445dd03ad4a..d2bae1324d3bfe42a67d46f491febc0c07ec0e49 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -4,52 +4,43 @@
 #include <unistd.h>
 #include <stdint.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
        sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); 
-       return usbtmc_release(sc->usbdev);
+       return usbtmc_release(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);
diff --git a/scope.h b/scope.h
index 119c1508212a7f04127b82843ad02a43e3f48bef..66ed6fa07997a27173dea3608d382bd8562db1c0 100644 (file)
--- a/scope.h
+++ b/scope.h
@@ -1,5 +1,7 @@
 struct scope {
-       struct usb_dev_handle *usbdev;
+       struct {
+               struct usb_dev_handle *dev;
+       } usb;
        char idn[128];
 };
 
index 835b6c8b84b2a6449f13f4e98e16052248f5dc9d..eb7737ad287efda71b1f49008d1835da374c6595 100644 (file)
--- a/usbtmc.c
+++ b/usbtmc.c
@@ -4,6 +4,7 @@
 #include <usb.h>
 #include <arpa/inet.h>
 
+#include "scope.h"
 #include "usbtmc.h"
 
 #define USB_TIMEOUT 50000
@@ -54,7 +55,7 @@ static usb_dev_handle *usbtmc_find_scope() {
 //Send a scpi-command to the scope. The response goes into the buffer
 //called resp, with a size of resplen. If resp==NULL, no response
 //is requested.
-int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, 
+int usbtmc_sendscpi(struct scope *sc, char* cmd, 
                unsigned char *resp, int resplen) {
        unsigned char *buff;
        int len,r,i;
@@ -74,10 +75,10 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd,
        buff[11]=0x00;
        //fprintf(stderr,"Writing header len=%d\n", cmdlen);
        //printb(buff,12);
-       r=usb_bulk_write(dev, 1, (char*)buff, 12, USB_TIMEOUT);
+       r=usb_bulk_write(sc->usb.dev, 1, (char*)buff, 12, USB_TIMEOUT);
        //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
        //printb(cmd, cmdlen);
-       r=usb_bulk_write(dev, 1, cmd, cmdlen, USB_TIMEOUT);
+       r=usb_bulk_write(sc->usb.dev, 1, cmd, cmdlen, USB_TIMEOUT);
        //fprintf(stderr,"%i bytes written.\n",r);
        if (resp != NULL && resplen != 0) {
                //send read command
@@ -91,9 +92,9 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd,
                buff[11]=0;
                //fprintf(stderr,"Writing resp req header\n");
                //printb(buff,12);
-               r=usb_bulk_write(dev, 1, (char*)buff, 12, USB_TIMEOUT);
+               r=usb_bulk_write(sc->usb.dev, 1, (char*)buff, 12, USB_TIMEOUT);
                //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
-               r=usb_bulk_read(dev, 2, (char*)buff, 0x40, USB_TIMEOUT);
+               r=usb_bulk_read(sc->usb.dev, 2, (char*)buff, 0x40, USB_TIMEOUT);
                //printb(buff,r);
                len=chars2int(buff+4);
                //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
@@ -104,7 +105,7 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd,
                if (len > 0x40-12) {
                        //fprintf(stderr," Reading response:\n");
                        if (resplen<len) len=resplen;
-                       r=usb_bulk_read(dev, 2, (char*)resp+(0x40-12), len-(0x40-12), USB_TIMEOUT);
+                       r=usb_bulk_read(sc->usb.dev, 2, (char*)resp+(0x40-12), len-(0x40-12), USB_TIMEOUT);
                        //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
                        return r+(0x40-12);
                }
@@ -113,21 +114,22 @@ int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd,
        return 0;
 }
 
-void usbtmc_claim(usb_dev_handle *sc)
+void usbtmc_claim(struct scope *sc)
 {
-       usb_claim_interface(sc, 0);
+       usb_claim_interface(sc->usb.dev, 0);
 }
 
-void usbtmc_release(usb_dev_handle *sc)
+void usbtmc_release(struct scope *sc)
 {
-       usb_release_interface(sc, 0);
+       usb_release_interface(sc->usb.dev, 0);
 }
 
 //Initialize the scope.
-usb_dev_handle* usbtmc_initscope(void) {
+struct scope* usbtmc_initscope(void) {
        int r;
        unsigned char buff[10];
        usb_dev_handle *dev;
+       struct scope *sc;
 
        //Init libusb
        usb_init();
@@ -136,11 +138,20 @@ usb_dev_handle* usbtmc_initscope(void) {
        if (!dev) {
                return NULL;
        }
-       usbtmc_claim(dev);
+
+       sc = calloc(1, sizeof(struct scope));
+       if (sc == NULL) {
+               perror("calloc");
+               exit(EXIT_FAILURE);
+       }
+
+       sc->usb.dev = dev;
+
+       usbtmc_claim(sc);
        //The following code isn't really necessary, the program works
        //OK without it too.
-       r=usb_control_msg(dev, 0xC8, 9, 0, 0, (char*)buff, 4, USB_TIMEOUT);
-       usbtmc_release(dev);
+       r=usb_control_msg(sc->usb.dev, 0xC8, 9, 0, 0, (char*)buff, 4, USB_TIMEOUT);
+       usbtmc_release(sc);
        if (r < 0) {
                fprintf (stderr, "Error %d sending init message: %s\n", 
                                r, strerror (-r));
@@ -150,11 +161,11 @@ usb_dev_handle* usbtmc_initscope(void) {
        if (chars2int(buff)!=0x40005dc) {
                fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
        }
-       return dev;
+       return sc;
 }
 
-void usbtmc_close(usb_dev_handle *sc)
+void usbtmc_close(struct scope *sc)
 {
        //Free up and exit
-       usb_close(sc);
+       usb_close(sc->usb.dev);
 }
index 34fb77d7da1c87aff4f18dd12eea548097cebb40..519012e04da9aeda12cd8a38cbd9286526be6dca 100644 (file)
--- a/usbtmc.h
+++ b/usbtmc.h
@@ -18,8 +18,8 @@ struct usbtmc_header {
 #define USBTMC_TRANSFERATTRIB_EOM      (1<<0)
 #define USBTMC_TRANSFERATTRIB_TERMCHAR (1<<1)
 
-int usbtmc_sendscpi(struct usb_dev_handle *dev, char* cmd, unsigned char *resp, int resplen);
-struct usb_dev_handle* usbtmc_initscope(void);
-void usbtmc_close(struct usb_dev_handle *sc);
-void usbtmc_claim(struct usb_dev_handle *sc);
-void usbtmc_release(struct usb_dev_handle *sc);
+int usbtmc_sendscpi(struct scope *sc, char* cmd, unsigned char *resp, int resplen);
+struct scope * usbtmc_initscope(void);
+void usbtmc_close(struct scope *sc);
+void usbtmc_claim(struct scope *sc);
+void usbtmc_release(struct scope *sc);
Impressum, Datenschutz