]> git.zerfleddert.de Git - rigol/blob - rigol.c
5dfec41cea9e586491cd021a272983d625331a7e
[rigol] / rigol.c
1 /*
2 Sprite_tms hack to communicate with a Rigol DS1000-series scope using Linux.
3 This code is licensed under the GPL V3.
4
5 Warning: This code can in theory fubar the communications with the scope to a
6 point where the Linux USB-stack seems to get confused. Do a
7 rmmod 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
31 inline 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
39 void 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
63 char *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
82 void add_history (char *buf)
83 {
84 }
85 #endif
86
87 void 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
97 int 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 usbtmc_claim(sc);
108 buff = malloc (1024*1024);
109 if (buff == NULL) {
110 perror("malloc");
111 exit(EXIT_FAILURE);
112 }
113
114 bzero(&act, sizeof(act));
115 act.sa_handler = child_reaper;
116 act.sa_flags = SA_NOCLDSTOP|SA_RESTART;
117 if (sigaction(SIGCHLD, &act, NULL) == -1) {
118 perror("sigaction");
119 exit(EXIT_FAILURE);
120 }
121
122 while (1) {
123 scpi = readline ("> ");
124
125 if (!scpi) break;
126 if (strlen (scpi) == 0) {
127 free (scpi);
128 continue;
129 }
130
131 add_history (scpi);
132
133 if (strncmp (scpi, "quit", 4) == 0) break;
134 if (strncmp (scpi, "plot", 4) == 0) {
135 do_plot (sc);
136 continue;
137 }
138 if (strncmp (scpi, "databuf", 7) == 0) {
139 do_get_buf(sc);
140 continue;
141 }
142 if (strncmp (scpi, "screen", 6) == 0) {
143 do_get_screen(sc);
144 continue;
145 }
146 if (strncmp (scpi, "display", 7) == 0) {
147 do_display_screen(sc);
148 continue;
149 }
150
151 l = strlen (scpi);
152 //printf ("got buf(%d): ", l);
153 //printb (scpi, l+2);
154 if (strchr (scpi, '?')) {
155 //printf ("Expecting reply\n");
156 l = usbtmc_sendscpi(sc, scpi, buff, 1024*1024);
157 //printf ("Got replylen = %d.\n", l);
158 buff[l] = 0; //zero-terminate
159 printb (buff, l);
160 } else {
161 //printf ("No reply expected\n");
162 l=usbtmc_sendscpi(sc,scpi,NULL,0);
163 }
164 free (scpi);
165 }
166 //Disable keylock, so the user doesn't have to press the 'force'-button
167 l=usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0);
168
169 usbtmc_release(sc);
170 usbtmc_close(sc);
171 return 0;
172 }
Impressum, Datenschutz