]>
Commit | Line | Data |
---|---|---|
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 | 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 | ||
146 | l = strlen (scpi); | |
147 | //printf ("got buf(%d): ", l); | |
148 | //printb (scpi, l+2); | |
149 | if (strchr (scpi, '?')) { | |
150 | //printf ("Expecting reply\n"); | |
151 | l = usbtmc_sendscpi(sc, scpi, buff, 1024*1024); | |
152 | //printf ("Got replylen = %d.\n", l); | |
153 | buff[l] = 0; //zero-terminate | |
154 | printb (buff, l); | |
155 | } else { | |
156 | //printf ("No reply expected\n"); | |
157 | l=usbtmc_sendscpi(sc,scpi,NULL,0); | |
158 | } | |
159 | free (scpi); | |
160 | } | |
161 | //Disable keylock, so the user doesn't have to press the 'force'-button | |
162 | l=usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); | |
163 | ||
164 | //Free up and exit | |
165 | usb_release_interface(sc,0); | |
166 | usb_close(sc); | |
167 | return 0; | |
168 | } |