png.o
usbtmc.o
commands.o
+scope.o
screen_*.png
-OFILES=png.o usbtmc.o commands.o
+OFILES=png.o usbtmc.o commands.o scope.o
CFLAGS=-O2 -Wall -I/opt/local/include
LDFLAGS=-L/opt/local/lib -lusb -lreadline -lz
CC=gcc
-#include <usb.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
#include "png.h"
-#include "usbtmc.h"
+#include "scope.h"
#include "commands.h"
-void do_plot (struct usb_dev_handle *sc)
+void do_plot (struct scope *sc)
{
unsigned char ch1[1024], ch2[1024];
int i, l;
static FILE *gnuplot=NULL;
FILE *fp;
- l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
+ l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
if (l != 1024) {
printf ("hmm. didnt' get 1024 bytes. \n");
}
- l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024);
+ l = sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024);
if (l != 1024) {
printf ("hmm. didnt' get 1024 bytes. \n");
#define ERROR -1e100
-static double get_float_from_scope (struct usb_dev_handle *sc, char *var)
+static double get_float_from_scope (struct scope *sc, char *var)
{
unsigned char buf[1024];
double temp;
int l;
- l = usbtmc_sendscpi(sc, var, buf, 1024);
+ l = sendscpi(sc, var, buf, 1024);
if (l > 0) {
sscanf ((char*)buf, "%lf", &temp);
return temp;
}
-void do_get_buf (struct usb_dev_handle *sc)
+void do_get_buf (struct scope *sc)
{
FILE *fp;
int i, j, l, bp;
unsigned char data[512*1024];
double sampfreq;
- usbtmc_sendscpi (sc, ":STOP", NULL, 0);
+ sendscpi (sc, ":STOP", NULL, 0);
sampfreq = get_float_from_scope (sc, ":ACQ:SAMP?");
sprintf (buf, ":TIM:SCAL %.15f", 50 / sampfreq);
printf ("sending scale cmd: %s\n", buf);
- usbtmc_sendscpi (sc, buf, NULL, 0);
+ sendscpi (sc, buf, NULL, 0);
sleep (1);
for (i=-254*1024;i< 254*1024;i += 600) {
sprintf (buf, ":TIM:OFFSET %.15f", i / sampfreq);
printf ("Sending offset cmd: %s\n", buf);
- usbtmc_sendscpi (sc, buf, NULL, 0);
+ sendscpi (sc, buf, NULL, 0);
- l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
+ l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
if (l != 1024) {
printf ("hmm. didnt' get 1024 bytes. \n");
fwrite (data, bp, 1, fp);
fclose (fp);
- usbtmc_sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
+ sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
}
-unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen, int keylock)
+unsigned char* get_lcd(struct scope *sc, int *imglen, int keylock)
{
unsigned char screen[320*234];
unsigned char *png;
if (keylock) {
/* Hide "RMT" from screen */
- l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0);
+ l = sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0);
usleep(30000);
}
- l = usbtmc_sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
+ l = sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
if (l != sizeof(screen)) {
printf ("hmm. didnt' get %d bytes, but %d\n\n", (int)sizeof(screen), l);
return png;
}
-void do_get_screen(struct usb_dev_handle *sc)
+void do_get_screen(struct scope *sc)
{
time_t lt;
char filename[256];
}
}
-void do_display_screen(struct usb_dev_handle *sc)
+void do_display_screen(struct scope *sc)
{
unsigned char *png;
int imglen;
-void do_plot (struct usb_dev_handle *sc);
-void do_get_buf (struct usb_dev_handle *sc);
-void do_get_screen(struct usb_dev_handle *sc);
-void do_display_screen(struct usb_dev_handle *sc);
-unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen, int keylock);
+void do_plot (struct scope *sc);
+void do_get_buf (struct scope *sc);
+void do_get_screen(struct scope *sc);
+void do_display_screen(struct scope *sc);
+unsigned char* get_lcd(struct scope *sc, int *imglen, int keylock);
(or alternately: use ohci_hcd) if that happens and you should be fine.
*/
-#include <usb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
-#include "usbtmc.h"
+#include "scope.h"
#include "commands.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
int main(int argc, char **argv)
{
- struct usb_dev_handle *sc;
+ struct scope *sc;
char *scpi;
unsigned char *buff;
int l;
struct sigaction act;
//Initialize scope
- sc = usbtmc_initscope();
- usbtmc_claim(sc);
+ sc = initscope();
+ claimscope(sc);
buff = malloc (1024*1024);
if (buff == NULL) {
perror("malloc");
//printb (scpi, l+2);
if (strchr (scpi, '?')) {
//printf ("Expecting reply\n");
- l = usbtmc_sendscpi(sc, scpi, buff, 1024*1024);
+ l = sendscpi(sc, scpi, buff, 1024*1024);
//printf ("Got replylen = %d.\n", l);
buff[l] = 0; //zero-terminate
printb (buff, l);
} else {
//printf ("No reply expected\n");
- l=usbtmc_sendscpi(sc,scpi,NULL,0);
+ l=sendscpi(sc,scpi,NULL,0);
}
free (scpi);
}
- usbtmc_release(sc);
- usbtmc_close(sc);
+ releasescope(sc);
+ closescope(sc);
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <strings.h>
-#include <usb.h>
+#include <unistd.h>
-#include "usbtmc.h"
+#include "scope.h"
#include "commands.h"
static int send_binary(int s, char *buf, int len)
return send_binary(s, buf, strlen(buf));
}
-static void serve_index(int s)
+static void serve_index(int s, struct scope *sc)
{
send_text(s, "HTTP/1.0 200 OK");
send_text(s, "Content-type: text/html\n\n");
- send_text(s, "<html><head><title>Rigol DS1000</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n");
+ send_text(s, "<html><head><title>");
+ send_text(s, scope_idn(sc));
+ send_text(s, "</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n");
send_text(s, "<img src=\"/lcd.png\" height=\"234\" width=\"320\">\n");
send_text(s, "</body></html>\n");
}
-static void serve_lcd(int s, struct usb_dev_handle *sc)
+static void serve_lcd(int s, struct scope *sc)
{
char buf[256];
int imglen;
unsigned char *png;
- usbtmc_claim(sc);
+ claimscope(sc);
png = get_lcd(sc, &imglen, 0);
- usbtmc_release(sc);
+ releasescope(sc);
if (png == NULL)
return;
free(png);
}
-static void parse_request(int s, struct usb_dev_handle *sc)
+static void parse_request(int s, struct scope *sc)
{
int ret;
char buf[1024];
} while(token != NULL);
if (strcmp("/", file) == 0) {
- serve_index(s);
+ serve_index(s, sc);
} else if (strcmp("/lcd.png", file) == 0) {
serve_lcd(s, sc);
}
int sock, csock;
int opt;
socklen_t slen;
- struct usb_dev_handle *sc;
+ struct scope *sc;
struct sockaddr_in sin, clientsin;
unsigned short port = 8088;
- sc = usbtmc_initscope();
+ sc = initscope();
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
close(csock);
}
- usbtmc_close(sc);
+ closescope(sc);
return 0;
}
--- /dev/null
+#include <usb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "usbtmc.h"
+#include "scope.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);
+}
+
+void closescope(struct scope* sc)
+{
+ return usbtmc_close(sc->usbdev);
+}
+
+void claimscope(struct scope* sc)
+{
+ return usbtmc_claim(sc->usbdev);
+}
+
+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);
+}
+
+struct scope* initscope(void)
+{
+ struct usb_dev_handle *usbdev;
+ struct scope *sc;
+
+ usbdev = usbtmc_initscope();
+
+ if (!usbdev)
+ return NULL;
+
+ 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);
+
+ printf("Scope found (%s)\n", sc->idn);
+
+ return sc;
+}
+
+char *scope_idn(struct scope *sc)
+{
+ return sc->idn;
+}
--- /dev/null
+struct scope {
+ struct usb_dev_handle *usbdev;
+ char idn[128];
+};
+
+int sendscpi(struct scope* sc, char* cmd, unsigned char *resp, int resplen);
+struct scope* initscope(void);
+void closescope(struct scope* sc);
+void claimscope(struct scope* sc);
+void releasescope(struct scope* sc);
+char *scope_idn(struct scope *sc);
void usbtmc_release(usb_dev_handle *sc)
{
- //Disable keylock, so the user doesn't have to press the 'force'-button
- usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0);
usb_release_interface(sc, 0);
}
if (!dev) {
printf("No scope found.\n");
exit(1);
- } else {
- printf("Scope found.\n");
}
usbtmc_claim(dev);
//The following code isn't really necessary, the program works
-int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, unsigned char *resp, int resplen);
-usb_dev_handle* usbtmc_initscope(void);
-void usbtmc_close(usb_dev_handle *sc);
-void usbtmc_claim(usb_dev_handle *sc);
-void usbtmc_release(usb_dev_handle *sc);
+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);