]>
git.zerfleddert.de Git - rigol/blob - rigol.c
fff48466edebba36171fcadde1469748c4451281
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.
19 #include <readline/readline.h>
20 #include <readline/history.h>
25 //This routine locates a scope by VID/PID and returns an opened handle to it.
26 usb_dev_handle
*find_scope() {
28 struct usb_device
*dev
=NULL
;
31 for (bus
=usb_busses
; bus
; bus
=bus
->next
) {
32 for (dev
=bus
->devices
; dev
; dev
=dev
->next
) {
33 // fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
34 if (dev
->descriptor
.idVendor
==0x400 && dev
->descriptor
.idProduct
==0x5dc) {
42 //Helper-routine: Convert a little-endian 4-byte word to an int
43 void int2chars(unsigned char *buff
,unsigned int a
) {
50 //Helper-routine: Convert an int to little-endian 4-byte word
51 unsigned int chars2int(unsigned char *buff
) {
60 #define MIN(a,b) (((a)<(b))?(a):(b))
62 inline char printable (char ch
)
64 if (ch
< ' ') return '.';
65 if (ch
> '~') return '.';
69 //Debugging: Print a buffers contents in hex
70 void printb (unsigned char *pkt
, int len
)
74 for (i
=0;i
<len
;i
+= 16) {
76 for (j
=0;j
< MIN(len
-i
, 16);j
++) {
77 printf (" %02x", pkt
[i
+j
]);
78 if (j
== 7) printf (" ");
80 if (j
< 7) printf (" ");
84 for (j
=0;j
< MIN(len
-i
, 16);j
++) {
85 printf ("%c", printable (pkt
[i
+j
]));
91 //Send a scpi-command to the scope. The response goes into the buffer
92 //called resp, with a size of resplen. If resp==NULL, no response
94 int sendscpi(usb_dev_handle
*dev
, unsigned char* cmd
,
95 unsigned char *resp
, int resplen
) {
101 buff
[1]=seq
; buff
[2]=~seq
; //nseq
103 int2chars(buff
+4,strlen(cmd
));
108 // fprintf(stderr,"Writing header len=%d\n", strlen (cmd));
110 r
=usb_bulk_write(dev
,1,buff
,12,1000);
111 // fprintf(stderr,"%i bytes written. Writing cmd\n",r);
112 // printb(cmd,strlen(cmd));
113 r
=usb_bulk_write(dev
,1,cmd
,strlen(cmd
),1000);
114 // fprintf(stderr,"%i bytes written.\n",r);
115 if (resp
!= NULL
&& resplen
!= 0) {
119 buff
[1]=seq
; buff
[2]=~seq
; //nseq
120 int2chars(buff
+4,0x40);
125 // fprintf(stderr,"Writing resp req header\n");
127 r
=usb_bulk_write(dev
,1,buff
,12,1000);
128 // fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
129 r
=usb_bulk_read(dev
,2,buff
,0x40,1000);
131 len
=chars2int(buff
+4);
132 // fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
133 for (i
=0; i
<(r
-12); i
++) {
134 if (i
<resplen
) resp
[i
] = buff
[i
+12];
136 // printb(resp,r-12);
138 // fprintf(stderr," Reading response:\n");
139 if (resplen
<len
) len
=resplen
;
140 r
=usb_bulk_read(dev
,2,resp
+(0x40-12),len
-(0x40-12),1000);
141 // fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
149 //Initialize the scope.
150 void initscope(usb_dev_handle
*dev
) {
153 usb_claim_interface(dev
,0);
154 //The following code isn't really necessary, the program works
156 r
=usb_control_msg(dev
, 0xC8, 9, 0, 0, buff
, 4, 1000);
158 fprintf (stderr
, "Error %d sending init message: %s\n",
160 fprintf (stderr
, "Do you have permission on the USB device?\n");
163 if (chars2int(buff
)!=0x40004dc) {
164 fprintf(stderr
,"Init: buff[%i]=%x\n",r
,chars2int(buff
));
169 #define HAVE_READLINE
170 #ifndef HAVE_READLINE
172 char *readline (prompt
)
180 if (fgets (buf
, 1023, stdin
) == NULL
) {
185 if (buf
[l
-1] == '\n') {
191 void add_history (char *buf
)
197 void do_plot (struct usb_dev_handle
*sc
)
199 unsigned char ch1
[1024], ch2
[1024];
202 static FILE *gnuplot
=NULL
;
205 l
= sendscpi(sc
, ":WAV:DATA? CHANEL1", ch1
, 1024);
208 printf ("hmm. didnt' get 1024 bytes. \n");
211 l
= sendscpi(sc
, ":WAV:DATA? CHANNEL2", ch2
, 1024);
214 printf ("hmm. didnt' get 1024 bytes. \n");
218 gnuplot
= popen ("gnuplot", "w");
221 fp
= fopen ("temp.dat", "w");
222 for (i
=0xd4;i
<0x32c;i
++)
223 // for (i=0;i<0x400;i++)
224 fprintf (fp
, "%d %d\n", 255 - ch1
[i
], 255 - ch2
[i
]);
227 fprintf (gnuplot
, "plot 'temp.dat' using 1 with lines, 'temp.dat' using 2 with lines\n");
234 double get_float_from_scope (struct usb_dev_handle
*sc
, char *var
)
236 unsigned char buf
[1024];
240 l
= sendscpi(sc
, var
, buf
, 1024);
242 sscanf (buf
, "%lf", &temp
);
249 void do_get_buf (struct usb_dev_handle
*sc
)
253 unsigned char buf
[1024], ch1
[1024];
254 unsigned char data
[512*1024];
257 sendscpi (sc
, ":STOP", NULL
, 0);
259 sampfreq
= get_float_from_scope (sc
, ":ACQ:SAMP?");
261 printf ("Got sampling freq: %g\n", sampfreq
);
263 sprintf (buf
, ":TIM:SCAL %.15f", 50 / sampfreq
);
264 printf ("sending scale cmd: %s\n", buf
);
265 sendscpi (sc
, buf
, NULL
, 0);
270 for (i
=-254*1024;i
< 254*1024;i
+= 600) {
271 sprintf (buf
, ":TIM:OFFSET %.15f", i
/ sampfreq
);
272 printf ("Sending offset cmd: %s\n", buf
);
273 sendscpi (sc
, buf
, NULL
, 0);
275 l
= sendscpi(sc
, ":WAV:DATA? CHANEL1", ch1
, 1024);
278 printf ("hmm. didnt' get 1024 bytes. \n");
282 data
[bp
++] = ch1
[j
+0xd4];
284 printf ("Got %d bytes of data. \n", bp
);
286 fp
= fopen ("ch1.dump", "w");
287 fwrite (data
, bp
, 1, fp
);
290 sendscpi (sc
, ":TIM:OFFSET 0", NULL
, 0);
295 int main(int argc
, char **argv
)
297 struct usb_dev_handle
*sc
;
302 //Locate and open the scope
305 printf("No scope found.\n");
308 printf("Scope found.\n");
312 buff
= malloc (1024*1024);
314 scpi
= readline ("> ");
317 if (strlen (scpi
) == 0) {
324 if (strncmp (scpi
, "quit", 4) == 0) break;
325 if (strncmp (scpi
, "plot", 4) == 0) {
329 if (strncmp (scpi
, "databuf", 4) == 0) {
335 // printf ("got buf(%d): ", l);
336 // printb (scpi, l+2);
337 if (strchr (scpi
, '?')) {
338 // printf ("Expecting reply\n");
339 l
= sendscpi(sc
, scpi
, buff
, 1024*1024);
340 // printf ("Got replylen = %d.\n", l);
341 buff
[l
] = 0; //zero-terminate
344 // printf ("No reply expected\n");
345 l
=sendscpi(sc
,scpi
,NULL
,0);
349 //Disable keylock, so the user doesn't have to press the 'force'-button
350 l
=sendscpi(sc
, ":KEY:LOCK DISABLE",NULL
,0);
353 usb_release_interface(sc
,0);