]>
git.zerfleddert.de Git - rigol/blob - usbtmc.c
835b6c8b84b2a6449f13f4e98e16052248f5dc9d
9 #define USB_TIMEOUT 50000
11 #if BYTE_ORDER == LITTLE_ENDIAN
13 #elif BYTE_ORDER == BIG_ENDIAN
14 #define LE32(x) ((uint32_t)((((uint32_t)x)>>24) | ((((uint32_t)x)>>8) & 0xff00) | ((((uint32_t)x)<<8) & 0xff0000) | (((uint32_t)x)<<24)))
16 #error BYTE_ORDER not defined/known!
19 //Helper-routine: Convert a little-endian 4-byte word to an int
20 static void int2chars(unsigned char *buff
,unsigned int a
) {
27 //Helper-routine: Convert an int to little-endian 4-byte word
28 unsigned int chars2int(unsigned char *buff
) {
37 //This routine locates a scope by VID/PID and returns an opened handle to it.
38 static usb_dev_handle
*usbtmc_find_scope() {
40 struct usb_device
*dev
=NULL
;
43 for (bus
=usb_busses
; bus
; bus
=bus
->next
) {
44 for (dev
=bus
->devices
; dev
; dev
=dev
->next
) {
45 //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
46 if (dev
->descriptor
.idVendor
==0x400 && dev
->descriptor
.idProduct
==0x5dc) {
54 //Send a scpi-command to the scope. The response goes into the buffer
55 //called resp, with a size of resplen. If resp==NULL, no response
57 int usbtmc_sendscpi(usb_dev_handle
*dev
, char* cmd
,
58 unsigned char *resp
, int resplen
) {
61 int cmdlen
= strlen(cmd
);
62 static unsigned char seq
=0;
68 buff
[1]=seq
; buff
[2]=~seq
; //nseq
70 int2chars(buff
+4, cmdlen
);
75 //fprintf(stderr,"Writing header len=%d\n", cmdlen);
77 r
=usb_bulk_write(dev
, 1, (char*)buff
, 12, USB_TIMEOUT
);
78 //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
79 //printb(cmd, cmdlen);
80 r
=usb_bulk_write(dev
, 1, cmd
, cmdlen
, USB_TIMEOUT
);
81 //fprintf(stderr,"%i bytes written.\n",r);
82 if (resp
!= NULL
&& resplen
!= 0) {
86 buff
[1]=seq
; buff
[2]=~seq
; //nseq
87 int2chars(buff
+4,0x40);
92 //fprintf(stderr,"Writing resp req header\n");
94 r
=usb_bulk_write(dev
, 1, (char*)buff
, 12, USB_TIMEOUT
);
95 //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
96 r
=usb_bulk_read(dev
, 2, (char*)buff
, 0x40, USB_TIMEOUT
);
98 len
=chars2int(buff
+4);
99 //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
100 for (i
=0; i
<(r
-12); i
++) {
101 if (i
<resplen
) resp
[i
] = buff
[i
+12];
105 //fprintf(stderr," Reading response:\n");
106 if (resplen
<len
) len
=resplen
;
107 r
=usb_bulk_read(dev
, 2, (char*)resp
+(0x40-12), len
-(0x40-12), USB_TIMEOUT
);
108 //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
116 void usbtmc_claim(usb_dev_handle
*sc
)
118 usb_claim_interface(sc
, 0);
121 void usbtmc_release(usb_dev_handle
*sc
)
123 usb_release_interface(sc
, 0);
126 //Initialize the scope.
127 usb_dev_handle
* usbtmc_initscope(void) {
129 unsigned char buff
[10];
134 //Locate and open the scope
135 dev
= usbtmc_find_scope();
140 //The following code isn't really necessary, the program works
142 r
=usb_control_msg(dev
, 0xC8, 9, 0, 0, (char*)buff
, 4, USB_TIMEOUT
);
145 fprintf (stderr
, "Error %d sending init message: %s\n",
147 fprintf (stderr
, "Do you have permission on the USB device?\n");
150 if (chars2int(buff
)!=0x40005dc) {
151 fprintf(stderr
,"Init: buff[%i]=%x\n",r
,chars2int(buff
));
156 void usbtmc_close(usb_dev_handle
*sc
)