]>
git.zerfleddert.de Git - rigol/blob - usbtmc.c
0249e25712b766dd44c3679b25996c1f0c0084c9
9 #define USB_TIMEOUT 10000
11 //Helper-routine: Convert a little-endian 4-byte word to an int
12 static void int2chars(unsigned char *buff
,unsigned int a
) {
19 //Helper-routine: Convert an int to little-endian 4-byte word
20 unsigned int chars2int(unsigned char *buff
) {
29 //This routine locates a scope by VID/PID and returns an opened handle to it.
30 static usb_dev_handle
*usbtmc_find_scope() {
32 struct usb_device
*dev
=NULL
;
35 for (bus
=usb_busses
; bus
; bus
=bus
->next
) {
36 for (dev
=bus
->devices
; dev
; dev
=dev
->next
) {
37 //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
38 if (dev
->descriptor
.idVendor
==0x400 && dev
->descriptor
.idProduct
==0x5dc) {
46 //Send a scpi-command to the scope. The response goes into the buffer
47 //called resp, with a size of resplen. If resp==NULL, no response
49 int usbtmc_sendscpi(usb_dev_handle
*dev
, char* cmd
,
50 unsigned char *resp
, int resplen
) {
53 int cmdlen
= strlen(cmd
);
54 static unsigned char seq
=0;
60 buff
[1]=seq
; buff
[2]=~seq
; //nseq
62 int2chars(buff
+4, cmdlen
);
67 //fprintf(stderr,"Writing header len=%d\n", cmdlen);
69 r
=usb_bulk_write(dev
, 1, (char*)buff
, 12, USB_TIMEOUT
);
70 //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
71 //printb(cmd, cmdlen);
72 r
=usb_bulk_write(dev
, 1, cmd
, cmdlen
, USB_TIMEOUT
);
73 //fprintf(stderr,"%i bytes written.\n",r);
74 if (resp
!= NULL
&& resplen
!= 0) {
78 buff
[1]=seq
; buff
[2]=~seq
; //nseq
79 int2chars(buff
+4,0x40);
84 //fprintf(stderr,"Writing resp req header\n");
86 r
=usb_bulk_write(dev
, 1, (char*)buff
, 12, USB_TIMEOUT
);
87 //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
88 r
=usb_bulk_read(dev
, 2, (char*)buff
, 0x40, USB_TIMEOUT
);
90 len
=chars2int(buff
+4);
91 //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
92 for (i
=0; i
<(r
-12); i
++) {
93 if (i
<resplen
) resp
[i
] = buff
[i
+12];
97 //fprintf(stderr," Reading response:\n");
98 if (resplen
<len
) len
=resplen
;
99 r
=usb_bulk_read(dev
, 2, (char*)resp
+(0x40-12), len
-(0x40-12), USB_TIMEOUT
);
100 //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
108 void usbtmc_claim(usb_dev_handle
*sc
)
110 usb_claim_interface(sc
, 0);
113 void usbtmc_release(usb_dev_handle
*sc
)
115 usb_release_interface(sc
, 0);
118 //Initialize the scope.
119 usb_dev_handle
* usbtmc_initscope(void) {
121 unsigned char buff
[10];
126 //Locate and open the scope
127 dev
= usbtmc_find_scope();
129 printf("No scope found.\n");
133 //The following code isn't really necessary, the program works
135 r
=usb_control_msg(dev
, 0xC8, 9, 0, 0, (char*)buff
, 4, USB_TIMEOUT
);
138 fprintf (stderr
, "Error %d sending init message: %s\n",
140 fprintf (stderr
, "Do you have permission on the USB device?\n");
143 if (chars2int(buff
)!=0x40005dc) {
144 fprintf(stderr
,"Init: buff[%i]=%x\n",r
,chars2int(buff
));
149 void usbtmc_close(usb_dev_handle
*sc
)