]> git.zerfleddert.de Git - rigol/blame_incremental - rigol.c
move usb_close to usbtmc, fix memory leak
[rigol] / rigol.c
... / ...
CommitLineData
1/*
2Sprite_tms hack to communicate with a Rigol DS1000-series scope using Linux.
3This code is licensed under the GPL V3.
4
5Warning: This code can in theory fubar the communications with the scope to a
6point where the Linux USB-stack seems to get confused. Do a
7rmmod uhci_hcd; modprobe uhci_hcd
8(or alternately: use ohci_hcd) if that happens and you should be fine.
9 */
10
11#include <usb.h>
12#include <stdlib.h>
13#include <stdio.h>
14#include <string.h>
15#include <sys/stat.h>
16#include <sys/types.h>
17#include <sys/wait.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <signal.h>
21#include <time.h>
22
23#include <readline/readline.h>
24#include <readline/history.h>
25
26#include "usbtmc.h"
27#include "commands.h"
28
29#define MIN(a,b) (((a)<(b))?(a):(b))
30
31inline char printable (char ch)
32{
33 if (ch < ' ') return '.';
34 if (ch > '~') return '.';
35 return ch;
36}
37
38//Debugging: Print a buffers contents in hex
39void printb (unsigned char *pkt, int len)
40{
41 int i, j;
42
43 for (i=0;i<len;i+= 16) {
44 printf ("%04x: ", i);
45 for (j=0;j < MIN(len-i, 16);j++) {
46 printf (" %02x", pkt[i+j]);
47 if (j == 7) printf (" ");
48 }
49 if (j < 7) printf (" ");
50 for (;j<17;j++)
51 printf (" ");
52
53 for (j=0;j < MIN(len-i, 16);j++) {
54 printf ("%c", printable (pkt[i+j]));
55 }
56 printf ("\n");
57 }
58}
59
60#define HAVE_READLINE
61#ifndef HAVE_READLINE
62
63char *readline (prompt)
64{
65 char *buf;
66
67 printf (prompt);
68 fflush (stdout);
69 buf = malloc (1024);
70
71 if (fgets (buf, 1023, stdin) == NULL) {
72 free (buf);
73 return NULL;
74 }
75 l = strlen (buf);
76 if (buf[l-1] == '\n') {
77 buf[l] = 0;
78 }
79 return buf;
80}
81
82void add_history (char *buf)
83{
84}
85#endif
86
87void child_reaper(int sig)
88{
89 pid_t child;
90
91 do {
92 child = waitpid(-1, NULL, WNOHANG);
93 } while(child > 0);
94
95}
96
97int main(int argc, char **argv)
98{
99 struct usb_dev_handle *sc;
100 char *scpi;
101 unsigned char *buff;
102 int l;
103 struct sigaction act;
104
105 //Initialize scope
106 sc = usbtmc_initscope();
107 buff = malloc (1024*1024);
108 if (buff == NULL) {
109 perror("malloc");
110 exit(EXIT_FAILURE);
111 }
112
113 bzero(&act, sizeof(act));
114 act.sa_handler = child_reaper;
115 act.sa_flags = SA_NOCLDSTOP|SA_RESTART;
116 if (sigaction(SIGCHLD, &act, NULL) == -1) {
117 perror("sigaction");
118 exit(EXIT_FAILURE);
119 }
120
121 while (1) {
122 scpi = readline ("> ");
123
124 if (!scpi) break;
125 if (strlen (scpi) == 0) {
126 free (scpi);
127 continue;
128 }
129
130 add_history (scpi);
131
132 if (strncmp (scpi, "quit", 4) == 0) break;
133 if (strncmp (scpi, "plot", 4) == 0) {
134 do_plot (sc);
135 continue;
136 }
137 if (strncmp (scpi, "databuf", 7) == 0) {
138 do_get_buf(sc);
139 continue;
140 }
141 if (strncmp (scpi, "screen", 6) == 0) {
142 do_get_screen(sc);
143 continue;
144 }
145 if (strncmp (scpi, "display", 7) == 0) {
146 do_display_screen(sc);
147 continue;
148 }
149
150 l = strlen (scpi);
151 //printf ("got buf(%d): ", l);
152 //printb (scpi, l+2);
153 if (strchr (scpi, '?')) {
154 //printf ("Expecting reply\n");
155 l = usbtmc_sendscpi(sc, scpi, buff, 1024*1024);
156 //printf ("Got replylen = %d.\n", l);
157 buff[l] = 0; //zero-terminate
158 printb (buff, l);
159 } else {
160 //printf ("No reply expected\n");
161 l=usbtmc_sendscpi(sc,scpi,NULL,0);
162 }
163 free (scpi);
164 }
165 //Disable keylock, so the user doesn't have to press the 'force'-button
166 l=usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0);
167
168 usbtmc_close(sc);
169 return 0;
170}
Impressum, Datenschutz