]>
git.zerfleddert.de Git - rigol/blob - rigol.c
ebf01fb5986cc80beeb500b4244adfb0cab0b533
2 Sprite_tms hack to communicate with a Rigol DS1000-series scope using Linux.
3 This code is licensed under the GPL V3.
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.
16 #include <sys/types.h>
23 #include <readline/readline.h>
24 #include <readline/history.h>
30 #define MIN(a,b) (((a)<(b))?(a):(b))
32 inline char printable (char ch
)
34 if (ch
< ' ') return '.';
35 if (ch
> '~') return '.';
39 //Debugging: Print a buffers contents in hex
40 void printb (unsigned char *pkt
, int len
)
44 for (i
=0;i
<len
;i
+= 16) {
46 for (j
=0;j
< MIN(len
-i
, 16);j
++) {
47 printf (" %02x", pkt
[i
+j
]);
48 if (j
== 7) printf (" ");
50 if (j
< 7) printf (" ");
54 for (j
=0;j
< MIN(len
-i
, 16);j
++) {
55 printf ("%c", printable (pkt
[i
+j
]));
64 char *readline (prompt
)
72 if (fgets (buf
, 1023, stdin
) == NULL
) {
77 if (buf
[l
-1] == '\n') {
83 void add_history (char *buf
)
89 void do_plot (struct usb_dev_handle
*sc
)
91 unsigned char ch1
[1024], ch2
[1024];
94 static FILE *gnuplot
=NULL
;
97 l
= usbtmc_sendscpi(sc
, ":WAV:DATA? CHANEL1", ch1
, 1024);
100 printf ("hmm. didnt' get 1024 bytes. \n");
103 l
= usbtmc_sendscpi(sc
, ":WAV:DATA? CHANNEL2", ch2
, 1024);
106 printf ("hmm. didnt' get 1024 bytes. \n");
110 gnuplot
= popen ("gnuplot", "w");
113 fp
= fopen ("temp.dat", "w");
114 for (i
=0xd4;i
<0x32c;i
++)
115 //for (i=0;i<0x400;i++)
116 fprintf (fp
, "%d %d\n", 255 - ch1
[i
], 255 - ch2
[i
]);
119 fprintf (gnuplot
, "plot 'temp.dat' using 1 with lines, 'temp.dat' using 2 with lines\n");
126 double get_float_from_scope (struct usb_dev_handle
*sc
, char *var
)
128 unsigned char buf
[1024];
132 l
= usbtmc_sendscpi(sc
, var
, buf
, 1024);
134 sscanf ((char*)buf
, "%lf", &temp
);
141 void do_get_buf (struct usb_dev_handle
*sc
)
146 unsigned char ch1
[1024];
147 unsigned char data
[512*1024];
150 usbtmc_sendscpi (sc
, ":STOP", NULL
, 0);
152 sampfreq
= get_float_from_scope (sc
, ":ACQ:SAMP?");
154 printf ("Got sampling freq: %g\n", sampfreq
);
156 sprintf (buf
, ":TIM:SCAL %.15f", 50 / sampfreq
);
157 printf ("sending scale cmd: %s\n", buf
);
158 usbtmc_sendscpi (sc
, buf
, NULL
, 0);
163 for (i
=-254*1024;i
< 254*1024;i
+= 600) {
164 sprintf (buf
, ":TIM:OFFSET %.15f", i
/ sampfreq
);
165 printf ("Sending offset cmd: %s\n", buf
);
166 usbtmc_sendscpi (sc
, buf
, NULL
, 0);
168 l
= usbtmc_sendscpi(sc
, ":WAV:DATA? CHANEL1", ch1
, 1024);
171 printf ("hmm. didnt' get 1024 bytes. \n");
175 data
[bp
++] = ch1
[j
+0xd4];
177 printf ("Got %d bytes of data. \n", bp
);
179 fp
= fopen ("ch1.dump", "w");
180 fwrite (data
, bp
, 1, fp
);
183 usbtmc_sendscpi (sc
, ":TIM:OFFSET 0", NULL
, 0);
186 void do_get_screen(struct usb_dev_handle
*sc
)
188 unsigned char screen
[320*234];
198 /* Hide "RMT" from screen */
199 l
= usbtmc_sendscpi(sc
, ":KEY:LOCK DISABLE", NULL
, 0);
202 l
= usbtmc_sendscpi(sc
, ":LCD:DATA?", screen
, sizeof(screen
));
204 if (l
!= sizeof(screen
)) {
205 printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen
), l
);
208 png
= lcd2png(screen
, &imglen
);
211 strftime(filename
, sizeof(filename
), "screen_%Y%m%d-%H%M%S.png", localtime(<
));
212 fd
=open(filename
, O_CREAT
|O_WRONLY
, 0644);
219 ret
= write(fd
, png
, imglen
);
229 printf("Waveform saved as %s\n", filename
);
234 execlp("display", "display", filename
, NULL
);
245 void child_reaper(int sig
)
250 child
= waitpid(-1, NULL
, WNOHANG
);
255 int main(int argc
, char **argv
)
257 struct usb_dev_handle
*sc
;
261 struct sigaction act
;
264 sc
= usbtmc_initscope();
265 buff
= malloc (1024*1024);
271 bzero(&act
, sizeof(act
));
272 act
.sa_handler
= child_reaper
;
273 act
.sa_flags
= SA_NOCLDSTOP
|SA_RESTART
;
274 if (sigaction(SIGCHLD
, &act
, NULL
) == -1) {
280 scpi
= readline ("> ");
283 if (strlen (scpi
) == 0) {
290 if (strncmp (scpi
, "quit", 4) == 0) break;
291 if (strncmp (scpi
, "plot", 4) == 0) {
295 if (strncmp (scpi
, "databuf", 7) == 0) {
299 if (strncmp (scpi
, "screen", 6) == 0) {
305 //printf ("got buf(%d): ", l);
306 //printb (scpi, l+2);
307 if (strchr (scpi
, '?')) {
308 //printf ("Expecting reply\n");
309 l
= usbtmc_sendscpi(sc
, scpi
, buff
, 1024*1024);
310 //printf ("Got replylen = %d.\n", l);
311 buff
[l
] = 0; //zero-terminate
314 //printf ("No reply expected\n");
315 l
=usbtmc_sendscpi(sc
,scpi
,NULL
,0);
319 //Disable keylock, so the user doesn't have to press the 'force'-button
320 l
=usbtmc_sendscpi(sc
, ":KEY:LOCK DISABLE",NULL
,0);
323 usb_release_interface(sc
,0);