]>
git.zerfleddert.de Git - rigol/blob - usbtmc.c
8a35dd30c96e5a954bc231e57b250fbc2765691a
9 //Helper-routine: Convert a little-endian 4-byte word to an int
10 static void int2chars(unsigned char *buff
,unsigned int a
) {
17 //Helper-routine: Convert an int to little-endian 4-byte word
18 unsigned int chars2int(unsigned char *buff
) {
27 //This routine locates a scope by VID/PID and returns an opened handle to it.
28 static usb_dev_handle
*usbtmc_find_scope() {
30 struct usb_device
*dev
=NULL
;
33 for (bus
=usb_busses
; bus
; bus
=bus
->next
) {
34 for (dev
=bus
->devices
; dev
; dev
=dev
->next
) {
35 //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
36 if (dev
->descriptor
.idVendor
==0x400 && dev
->descriptor
.idProduct
==0x5dc) {
44 //Send a scpi-command to the scope. The response goes into the buffer
45 //called resp, with a size of resplen. If resp==NULL, no response
47 int usbtmc_sendscpi(usb_dev_handle
*dev
, char* cmd
,
48 unsigned char *resp
, int resplen
) {
51 int cmdlen
= strlen(cmd
);
52 static unsigned char seq
=0;
58 buff
[1]=seq
; buff
[2]=~seq
; //nseq
60 int2chars(buff
+4, cmdlen
);
65 //fprintf(stderr,"Writing header len=%d\n", cmdlen);
67 r
=usb_bulk_write(dev
, 1, (char*)buff
, 12, 1000);
68 //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
69 //printb(cmd, cmdlen);
70 r
=usb_bulk_write(dev
, 1, cmd
, cmdlen
, 1000);
71 //fprintf(stderr,"%i bytes written.\n",r);
72 if (resp
!= NULL
&& resplen
!= 0) {
76 buff
[1]=seq
; buff
[2]=~seq
; //nseq
77 int2chars(buff
+4,0x40);
82 //fprintf(stderr,"Writing resp req header\n");
84 r
=usb_bulk_write(dev
, 1, (char*)buff
, 12, 1000);
85 //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
86 r
=usb_bulk_read(dev
, 2, (char*)buff
, 0x40, 1000);
88 len
=chars2int(buff
+4);
89 //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
90 for (i
=0; i
<(r
-12); i
++) {
91 if (i
<resplen
) resp
[i
] = buff
[i
+12];
95 //fprintf(stderr," Reading response:\n");
96 if (resplen
<len
) len
=resplen
;
97 r
=usb_bulk_read(dev
, 2, (char*)resp
+(0x40-12), len
-(0x40-12),1000);
98 //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
106 void usbtmc_claim(usb_dev_handle
*sc
)
108 usb_claim_interface(sc
, 0);
111 void usbtmc_release(usb_dev_handle
*sc
)
113 usb_release_interface(sc
, 0);
116 //Initialize the scope.
117 usb_dev_handle
* usbtmc_initscope(void) {
119 unsigned char buff
[10];
124 //Locate and open the scope
125 dev
= usbtmc_find_scope();
127 printf("No scope found.\n");
131 //The following code isn't really necessary, the program works
133 r
=usb_control_msg(dev
, 0xC8, 9, 0, 0, (char*)buff
, 4, 1000);
136 fprintf (stderr
, "Error %d sending init message: %s\n",
138 fprintf (stderr
, "Do you have permission on the USB device?\n");
141 if (chars2int(buff
)!=0x40005dc) {
142 fprintf(stderr
,"Init: buff[%i]=%x\n",r
,chars2int(buff
));
147 void usbtmc_close(usb_dev_handle
*sc
)